📄 demo.java
字号:
/* * Copyright (c) 2007, 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. * * This file is part of the Contiki operating system. * * $Id: Demo.java,v 1.4 2008/07/02 14:12:48 adamdunkels Exp $ *//** * \file * Java program showing energy estimates from a Contiki app * \author * Fredrik 謘terlind <fros@sics.se> */import java.awt.BorderLayout;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.GridLayout;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.awt.image.BufferedImage;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Vector;import javax.swing.ImageIcon;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.time.Second;import org.jfree.data.time.TimeSeries;import org.jfree.data.time.TimeSeriesCollection;public class Demo extends JPanel { public static final boolean REPLAY_TEMP_DATA = false; public static final int TOTAL_HEIGHT = 300; public static final int TOTAL_WIDTH = 900; public static final String SERIALDUMP_WINDOWS = "./tools/sky/serialdump-windows.exe"; public static final String SERIALDUMP_LINUX = "./tools/sky/serialdump-linux"; public static final int HISTORY_MAX_SECONDS = 120; public static final int NODE_HEIGHT = 300; public static final int NODE_WIDTH = 450; public static final int NUMBER_NODES = 7; public static final int NODE_IDS[] = {42, 43, 44, 45, 46, 47, 48}; public static final String CATEGORIES[] = {"LPM", "CPU", "Radio listen", "Radio transmit"}; public static final int PARSE_NR_COMPONENTS = 8; public static final int PARSE_POS_SINK_ID = 0; public static final int PARSE_POS_SICS_ID = 1; public static final int PARSE_POS_COUNT = 2; public static final int PARSE_POS_SOURCE_ID = 3; public static final int PARSE_POS_TIME_CPU = 4; public static final int PARSE_POS_TIME_LPM = 5; public static final int PARSE_POS_TIME_TRANSMIT = 6; public static final int PARSE_POS_TIME_LISTEN = 7; public static final String PARSE_SICS_ID = "SICS"; public static final double TICKS_PER_SECOND = 4096; /* TODO Convert from TimerB ticks to seconds */ public static final double UPDATE_PERIOD = 1; /* TODO Set update period (1 second?) */ /* CC2420 has 8.5 (-25dBm), 9.9 (-15dBm), 11 (-10dBm), 14 (-5dBm) and 17.4 (0dBm) */ public static final int CHARTS_MAX_MILLIWATTS = 70; public static final double VOLTAGE = 3; public static final double POWER_CPU = 1.800 * VOLTAGE; /* mW */ public static final double POWER_LPM = 0.0545 * VOLTAGE; /* mW */ public static final double POWER_TRANSMIT = 17.7 * VOLTAGE; /* mW */ public static final double POWER_LISTEN = 20.0 * VOLTAGE; /* mW */ public static final int MA_HISTORY_LENGTH = 40; private Process serialDumpProcess; private Vector<Double> historyLPM = new Vector<Double>(); private Vector<Double> historyCPU = new Vector<Double>(); private Vector<Double> historyListen = new Vector<Double>(); private Vector<Double> historyTransmit = new Vector<Double>(); private int trackedNodeIndex = 0; /* Currently tracked node index */ private String comPort; private JFrame frame; private TimeSeries nodeHistorySerie; private JFreeChart nodeHistoryChart; private JLabel nodeHistoryLabel; private JFreeChart relativeChart; private JLabel relativeLabel; private DefaultPieDataset relativeDataset; private JFreeChart totalChart; private DefaultCategoryDataset totalDataset; private JLabel totalLabel; private int categoryOrder = 0; public Demo(String comPort) { this.comPort = comPort; System.out.println("Demo application listening on COM port: " + comPort); /* Make sure we have nice window decorations */ JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); Rectangle maxSize = GraphicsEnvironment.getLocalGraphicsEnvironment() .getMaximumWindowBounds(); /* Create and set up the window */ frame = new JFrame("Sensor Node Power Profiling with Contiki (ACM SenSys 2007)"); if (maxSize != null) { frame.setMaximizedBounds(maxSize); } frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowListener() { public void windowDeactivated(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowClosing(WindowEvent e) { /* TODO Clean up resources */ if (serialDumpProcess != null) { try { serialDumpProcess.destroy(); } catch (Exception ex) { System.err.println("Serialdump process exception: " + ex.getMessage()); } } System.exit(0); } }); frame.setContentPane(this); /* Create charts */ createAllCharts(); /* Add charts */ this.setLayout(new BorderLayout()); JPanel contentPanel = new JPanel(new GridLayout(2, 1)); add(contentPanel); JPanel upperPanel = new JPanel(new GridLayout()); totalLabel.setAlignmentX(JPanel.CENTER_ALIGNMENT); upperPanel.add(totalLabel); contentPanel.add(upperPanel); JPanel lowerPanel = new JPanel(new GridLayout(1, 2)); relativeLabel.setAlignmentX(JPanel.CENTER_ALIGNMENT); lowerPanel.add(relativeLabel); nodeHistoryLabel.setAlignmentX(JPanel.CENTER_ALIGNMENT); lowerPanel.add(nodeHistoryLabel); contentPanel.add(lowerPanel); JLabel advertisementLabel = new JLabel("Sensor Node Power Profiling with Contiki", JLabel.CENTER); advertisementLabel.setFont(new Font("Sans-serif", Font.BOLD, 40)); JLabel urlLabel = new JLabel("http://www.sics.se/contiki/", JLabel.CENTER); urlLabel.setFont(new Font("Monospace", Font.BOLD, 36)); add(advertisementLabel, BorderLayout.NORTH); add(urlLabel, BorderLayout.SOUTH); /* Display the window */ frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); if (!REPLAY_TEMP_DATA) { connectToCOMPort(); } Timer updateTimer = new Timer(500, null); updateTimer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// parseIncomingLine(TEMP_NODE_DATA[TEMP_COUNTER]);// TEMP_COUNTER = (TEMP_COUNTER + 1) % TEMP_NODE_DATA.length; try { updateCharts(); } catch(Exception eeeee) {} } }); updateTimer.start(); if (REPLAY_TEMP_DATA) { // Timer updateTimer = new Timer(1000, null); updateTimer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { parseIncomingLine(TEMP_NODE_DATA[TEMP_COUNTER]); TEMP_COUNTER = (TEMP_COUNTER + 1) % TEMP_NODE_DATA.length; updateCharts(); } }); updateTimer.start(); } } public void connectToCOMPort() { /* Connect to COM using external serialdump application */ String osName = System.getProperty("os.name").toLowerCase(); String fullCommand; if (osName.startsWith("win")) { fullCommand = SERIALDUMP_WINDOWS + " " + "-b115200" + " " + comPort; } else { fullCommand = SERIALDUMP_LINUX + " " + "-b115200" + " " + comPort; } try { String[] cmd = fullCommand.split(" "); serialDumpProcess = Runtime.getRuntime().exec(cmd); final BufferedReader input = new BufferedReader(new InputStreamReader(serialDumpProcess.getInputStream())); final BufferedReader err = new BufferedReader(new InputStreamReader(serialDumpProcess.getErrorStream())); /* Start thread listening on stdout */ Thread readInput = new Thread(new Runnable() { public void run() { String line; try { while ((line = input.readLine()) != null) { parseIncomingLine(line); } input.close(); System.out.println("Serialdump process shut down, exiting"); System.exit(1); } catch (IOException e) { System.err.println("Exception when reading from serialdump"); e.printStackTrace(); System.exit(1); } } }, "read input stream thread"); /* Start thread listening on stderr */ Thread readError = new Thread(new Runnable() { public void run() { String line; try { while ((line = err.readLine()) != null) { System.err.println("Serialdump error stream> " + line); } err.close(); } catch (IOException e) { System.err.println("Exception when reading from serialdump"); e.printStackTrace(); System.exit(1); } } }, "read error stream thread"); readInput.start(); readError.start(); } catch (Exception e) { System.err.println("Exception when executing '" + fullCommand + "'"); System.err.println("Exiting demo application"); e.printStackTrace(); System.exit(1); } } public void createAllCharts() { BufferedImage image; /* Create total power history chart for tracked node */ nodeHistoryLabel = new JLabel(); createHistoryChartForNode(nodeHistoryLabel, trackedNodeIndex); /* Create moving average relative power distribution chart */ relativeDataset = new DefaultPieDataset(); for (String category: CATEGORIES) { relativeDataset.setValue(category, 0); } relativeChart = ChartFactory.createPieChart("Moving Average: Relative power distribution", relativeDataset, false, false, false); image = relativeChart.createBufferedImage(NODE_WIDTH,NODE_HEIGHT); relativeLabel = new JLabel(); relativeLabel.setIcon(new ImageIcon(image)); /* Create chart with power of all nodes */ totalDataset = new DefaultCategoryDataset(); for (int i=0; i < NUMBER_NODES; i++) { for (String category: CATEGORIES) { totalDataset.addValue(0, category, getNodeNameFromIndex(i)); } } totalChart = ChartFactory.createStackedBarChart(null, null, "Power (mW)", totalDataset, PlotOrientation.VERTICAL, true, true, true); ValueAxis rangeAxis = totalChart.getCategoryPlot().getRangeAxis(); // rangeAxis.setRange(0, CHARTS_MAX_MILLIWATTS); image = totalChart.createBufferedImage(TOTAL_WIDTH,TOTAL_HEIGHT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -