From 77c4396702754e10c8ab215ad52d9a7e00f82a39 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 14 Aug 2021 23:47:41 -0400 Subject: [PATCH] Initial implementation of adding event --- .../java/uorocketry/basestation/Main.java | 1 + .../java/uorocketry/basestation/Window.java | 8 ++- .../basestation/control/UserEventPanel.java | 69 +++++++++++++++++++ .../basestation/data/UserEvent.java | 31 +++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/main/java/uorocketry/basestation/control/UserEventPanel.java create mode 100644 src/main/java/uorocketry/basestation/data/UserEvent.java diff --git a/src/main/java/uorocketry/basestation/Main.java b/src/main/java/uorocketry/basestation/Main.java index e61a32b..4de8f9d 100644 --- a/src/main/java/uorocketry/basestation/Main.java +++ b/src/main/java/uorocketry/basestation/Main.java @@ -168,6 +168,7 @@ public Main() { private void setupData() { dataProcessor = new DataProcessor(config, window.dataTables); + window.userEventPanel.setDataPointHolder(dataProcessor.getDataPointHolder()); currentDataIndexes = new ArrayList<>(config.getDataSourceCount()); minDataIndexes = new ArrayList<>(config.getDataSourceCount()); diff --git a/src/main/java/uorocketry/basestation/Window.java b/src/main/java/uorocketry/basestation/Window.java index 1de954a..c010ac8 100644 --- a/src/main/java/uorocketry/basestation/Window.java +++ b/src/main/java/uorocketry/basestation/Window.java @@ -38,10 +38,10 @@ import uorocketry.basestation.connections.DeviceConnectionHolder; import uorocketry.basestation.connections.DataReceiver; import uorocketry.basestation.control.StateButton; +import uorocketry.basestation.control.UserEventPanel; import uorocketry.basestation.data.DataTableCellRenderer; import uorocketry.basestation.data.RssiProcessor; import uorocketry.basestation.panel.Chart; -import uorocketry.basestation.panel.DataChart; import uorocketry.basestation.panel.TableHolder; public class Window extends JFrame { @@ -77,6 +77,7 @@ public class Window extends JFrame { List maxSliders = new ArrayList(2); List minSliders = new ArrayList(2); public JTabbedPane sliderTabs; + UserEventPanel userEventPanel; JButton clearDataButton; JButton refreshComSelectorButton; JButton hideComSelectorButton; @@ -232,7 +233,10 @@ public Window(Main main, Config config) { eastSliderButtons = new JPanel(); sliderButtons.add(eastSliderButtons, BorderLayout.EAST); - + + userEventPanel = new UserEventPanel(config); + eastSliderButtons.add(userEventPanel.getPanel()); + clearDataButton = new JButton("Clear Data"); eastSliderButtons.add(clearDataButton); diff --git a/src/main/java/uorocketry/basestation/control/UserEventPanel.java b/src/main/java/uorocketry/basestation/control/UserEventPanel.java new file mode 100644 index 0000000..0ea4c3b --- /dev/null +++ b/src/main/java/uorocketry/basestation/control/UserEventPanel.java @@ -0,0 +1,69 @@ +package uorocketry.basestation.control; + +import uorocketry.basestation.config.Config; +import uorocketry.basestation.data.DataHolder; +import uorocketry.basestation.data.DataPointHolder; +import uorocketry.basestation.data.UserEvent; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class UserEventPanel implements ActionListener { + private JPanel panel; + private JButton recordEventButton; + + private List events = new ArrayList<>(); + + private Config mainConfig; + private DataPointHolder dataPointHolder; + + public UserEventPanel(Config mainConfig) { + this.mainConfig = mainConfig; + panel = new JPanel(); + + recordEventButton = new JButton(getButtonTitle(0)); + recordEventButton.addActionListener(this); + panel.add(recordEventButton); + } + + public JPanel getPanel() { + return panel; + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == recordEventButton) { + List> allData = dataPointHolder.getAllReceivedData(); + long[] datasetsTimestamp = new long[allData.size()]; + for (int i = 0; i < allData.size(); i++) { + Integer timestampIndex = mainConfig.getDataSet(i).getIndex("timestamp"); + List dataHolders = allData.get(i); + if (timestampIndex != null && dataHolders != null && dataHolders.size() > 0) { + DataHolder currentData = dataHolders.get(dataHolders.size() - 1); + datasetsTimestamp[i] = currentData != null ? + Optional.ofNullable(currentData.data[timestampIndex].getLongValue()).orElse(-1L) + : -1L; + } else { + datasetsTimestamp[i] = -1; + } + } + + events.add(new UserEvent("", events.size(), System.currentTimeMillis(), datasetsTimestamp)); + recordEventButton.setText(getButtonTitle(events.size())); + + System.out.println(events.size() + "\t" + System.currentTimeMillis() + "\t" + datasetsTimestamp); + } + } + + private String getButtonTitle(int index) { + return "Record Event (" + index + ")"; + } + + public void setDataPointHolder(DataPointHolder dataPointHolder) { + this.dataPointHolder = dataPointHolder; + } +} diff --git a/src/main/java/uorocketry/basestation/data/UserEvent.java b/src/main/java/uorocketry/basestation/data/UserEvent.java new file mode 100644 index 0000000..cbc13d7 --- /dev/null +++ b/src/main/java/uorocketry/basestation/data/UserEvent.java @@ -0,0 +1,31 @@ +package uorocketry.basestation.data; + +public class UserEvent { + private String name; + private int index; + private long epochTime; + private long[] datasetTime; + + public UserEvent(String name, int index, long epochTime, long[] datasetTime) { + this.name = name; + this.index = index; + this.epochTime = epochTime; + this.datasetTime = datasetTime; + } + + public String getName() { + return name; + } + + public int getIndex() { + return index; + } + + public long getEpochTime() { + return epochTime; + } + + public long[] getDatasetTime() { + return datasetTime; + } +}