📄 collectserver.java
字号:
/* * Copyright (c) 2008, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: CollectServer.java,v 1.9 2008/11/10 21:14:20 adamdunkels Exp $ * * ----------------------------------------------------------------- * * CollectServer * * Authors : Joakim Eriksson, Niclas Finne * Created : 3 jul 2008 * Updated : $Date: 2008/11/10 21:14:20 $ * $Revision: 1.9 $ */package se.sics.contiki.collect;import java.awt.BorderLayout;import java.awt.GraphicsEnvironment;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Hashtable;import java.util.Properties;import javax.swing.BorderFactory;import javax.swing.DefaultListModel;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.SwingUtilities;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import se.sics.contiki.collect.gui.BarChartPanel;import se.sics.contiki.collect.gui.MapPanel;import se.sics.contiki.collect.gui.SerialConsole;import se.sics.contiki.collect.gui.TimeChartPanel;/** * */public class CollectServer { public static final String WINDOW_TITLE = "Sensor Data Collect with Contiki"; public static final String CONFIG_FILE = "collect.conf"; public static final String SENSORDATA_FILE = "sensordata.log"; public static final String CONFIG_DATA_FILE = "collect-data.conf"; public static final String INIT_SCRIPT = "collect-init.script"; public static final String FIRMWARE_FILE = "sky-shell.ihex"; private Properties config = new Properties(); private String configFile; private Properties configTable = new Properties(); private ArrayList<SensorData> sensorDataList = new ArrayList<SensorData>(); private PrintWriter sensorDataOutput; private Hashtable<String,Node> nodeTable = new Hashtable<String,Node>(); private Node[] nodeCache; private JFrame window; private JTabbedPane mainPanel; private JMenuItem serialItem; private JMenuItem runInitScriptItem; private Visualizer[] visualizers; private MapPanel mapPanel; private SerialConsole serialConsole; private JFileChooser fileChooser; private JList nodeList; private DefaultListModel nodeModel; private Node[] selectedNodes; private SerialConnection serialConnection; private String initScript; @SuppressWarnings("serial") public CollectServer(String comPort) { loadConfig(config, CONFIG_FILE); this.configFile = config.getProperty("config.datafile", CONFIG_DATA_FILE); if (this.configFile != null) { loadConfig(configTable, this.configFile); } if (comPort == null) { comPort = configTable.getProperty("collect.serialport"); } this.initScript = config.getProperty("init.script", INIT_SCRIPT); /* Make sure we have nice window decorations */// JFrame.setDefaultLookAndFeelDecorated(true);// JDialog.setDefaultLookAndFeelDecorated(true); Rectangle maxSize = GraphicsEnvironment.getLocalGraphicsEnvironment() .getMaximumWindowBounds(); /* Create and set up the window */ window = new JFrame(WINDOW_TITLE + " (not connected)"); window.setLocationByPlatform(true); if (maxSize != null) { window.setMaximizedBounds(maxSize); } window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { exit(); } }); nodeModel = new DefaultListModel(); nodeList = new JList(nodeModel); nodeList.setPrototypeCellValue("Node 88888"); nodeList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting() && e.getSource() == nodeList) { Node[] selected; int iMin = nodeList.getMinSelectionIndex(); int iMax = nodeList.getMaxSelectionIndex(); if ((iMin < 0) || (iMax < 0)) { selected = null; } else { Node[] tmp = new Node[1 + (iMax - iMin)]; int n = 0; for(int i = iMin; i <= iMax; i++) { if (nodeList.isSelectedIndex(i)) { tmp[n++] = (Node) nodeModel.getElementAt(i); } } if (n != tmp.length) { Node[] t = new Node[n]; System.arraycopy(tmp, 0, t, 0, n); tmp = t; } selected = tmp; } selectNodes(selected, false); } }}); nodeList.setBorder(BorderFactory.createTitledBorder("Nodes")); window.getContentPane().add(new JScrollPane(nodeList), BorderLayout.WEST); mainPanel = new JTabbedPane(); mainPanel.setBackground(nodeList.getBackground()); mainPanel.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); serialConsole = new SerialConsole(this); mapPanel = new MapPanel(this); String image = getConfig("collect.mapimage"); if (image != null) { mapPanel.setMapBackground(image); } final int defaultMaxItemCount = 250; visualizers = new Visualizer[] { mapPanel, new BarChartPanel(this, "Average Power", "Average Power Consumption", null, "Power (mW)", new String[] { "LPM", "CPU", "Radio listen", "Radio transmit" }) { { ValueAxis axis = chart.getCategoryPlot().getRangeAxis(); axis.setLowerBound(0.0); axis.setUpperBound(75.0); } protected void addSensorData(SensorData data) { Node node = data.getNode(); String nodeName = node.getName(); SensorDataAggregator aggregator = node.getSensorDataAggregator(); dataset.addValue(aggregator.getLPMPower(), categories[0], nodeName); dataset.addValue(aggregator.getCPUPower(), categories[1], nodeName); dataset.addValue(aggregator.getListenPower(), categories[2], nodeName); dataset.addValue(aggregator.getTransmitPower(), categories[3], nodeName); } }, new BarChartPanel(this, "Instantaneous Power", "Instantaneous Power Consumption", null, "Power (mW)", new String[] { "LPM", "CPU", "Radio listen", "Radio transmit" }) { { ValueAxis axis = chart.getCategoryPlot().getRangeAxis(); axis.setLowerBound(0.0); axis.setUpperBound(75.0); } protected void addSensorData(SensorData data) { Node node = data.getNode(); String nodeName = node.getName(); dataset.addValue(data.getLPMPower(), categories[0], nodeName); dataset.addValue(data.getCPUPower(), categories[1], nodeName); dataset.addValue(data.getListenPower(), categories[2], nodeName); dataset.addValue(data.getTransmitPower(), categories[3], nodeName); } }, new TimeChartPanel(this, "Power History", "Historical Power Consumption", "Time", "mW") { { setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getAveragePower(); } }, new BarChartPanel(this, "Average Temperature", "Temperature", null, "Celsius", new String[] { "Celsius" }) { { chart.getCategoryPlot().getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } protected void addSensorData(SensorData data) { Node node = data.getNode(); String nodeName = node.getName(); SensorDataAggregator aggregator = node.getSensorDataAggregator(); dataset.addValue(aggregator.getAverageTemperature(), categories[0], nodeName); } }, new TimeChartPanel(this, "Temperature", "Temperature", "Time", "Celsius") { { chart.getXYPlot().getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); setRangeTick(5); setRangeMinimumSize(10.0); setGlobalRange(true); setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getTemperature(); } }, new TimeChartPanel(this, "Battery Voltage", "Battery Voltage", "Time", "Volt") { { setRangeTick(1); setRangeMinimumSize(4.0); setGlobalRange(true); setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getBatteryVoltage(); } }, new TimeChartPanel(this, "Battery Indicator", "Battery Indicator", "Time", "Indicator") { { chart.getXYPlot().getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); setRangeTick(5); setRangeMinimumSize(10.0); setGlobalRange(true); setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getBatteryIndicator(); } }, new TimeChartPanel(this, "Relative Humidity", "Humidity", "Time", "%") { { setMaxItemCount(defaultMaxItemCount); chart.getXYPlot().getRangeAxis().setRange(0.0, 100.0); } protected double getSensorDataValue(SensorData data) { return data.getHumidity(); } }, new TimeChartPanel(this, "Light 1", "Light 1", "Time", "-") { { setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getLight1(); } }, new TimeChartPanel(this, "Light 2", "Light 2", "Time", "-") { { setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getLight2(); } }, new TimeChartPanel(this, "Network Hops", "Network Hops", "Time", "Hops") { { ValueAxis axis = chart.getXYPlot().getRangeAxis(); axis.setLowerBound(0.0); axis.setUpperBound(4.0); axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getValue(SensorData.HOPS); } }, new BarChartPanel(this, "Network Hops", "Network Hops", null, "Hops", new String[] { "Hops" }) { { chart.getCategoryPlot().getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } protected void addSensorData(SensorData data) { dataset.addValue(data.getValue(SensorData.HOPS), categories[0], data.getNode().getName()); } }, new TimeChartPanel(this, "Latency", "Latency", "Time", "Seconds") { { setMaxItemCount(defaultMaxItemCount); } protected double getSensorDataValue(SensorData data) { return data.getLatency(); } }, serialConsole }; for (int i = 0, n = visualizers.length; i < n; i++) { mainPanel.add(visualizers[i].getTitle(), visualizers[i].getPanel()); } window.getContentPane().add(mainPanel, BorderLayout.CENTER);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -