Java interface using the shell to collect and visualize sensor values from Sky nodes

This commit is contained in:
nifi 2008-07-09 23:18:05 +00:00
parent 5da504dcc7
commit 0fa1ae0fce
15 changed files with 3135 additions and 0 deletions

View file

@ -0,0 +1,164 @@
/*
* 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: BarChartPanel.java,v 1.1 2008/07/09 23:18:06 nifi Exp $
*
* -----------------------------------------------------------------
*
* PowerPanel
*
* Authors : Joakim Eriksson, Niclas Finne
* Created : 5 jul 2008
* Updated : $Date: 2008/07/09 23:18:06 $
* $Revision: 1.1 $
*/
package se.sics.contiki.collect.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import se.sics.contiki.collect.CollectServer;
import se.sics.contiki.collect.Node;
import se.sics.contiki.collect.SensorData;
import se.sics.contiki.collect.Visualizer;
/**
*
*/
public abstract class BarChartPanel extends JPanel implements Visualizer {
private static final long serialVersionUID = 7664283678708048061L;
protected final CollectServer server;
protected final String title;
protected final String[] categories;
protected final JFreeChart chart;
protected final ChartPanel chartPanel;
protected final DefaultCategoryDataset dataset;
private int categoryOrder = 0;
protected BarChartPanel(CollectServer server, String title, String chartTitle, String domainAxisLabel, String valueAxisLabel,
String[] categories) {
super(new BorderLayout());
this.server = server;
this.title = title;
this.categories = categories;
/* Create chart with power of all nodes */
dataset = new DefaultCategoryDataset();
this.chart = ChartFactory.createStackedBarChart(chartTitle, domainAxisLabel, valueAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false);
this.chartPanel = new ChartPanel(chart);
this.chartPanel.setPreferredSize(new Dimension(500, 270));
if (categories.length > 1) {
this.chartPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
categoryOrder++;
updateCharts();
}
});
}
add(chartPanel, BorderLayout.CENTER);
}
@Override
public String getTitle() {
return title;
}
@Override
public Component getPanel() {
return this;
}
@Override
public void nodeAdded(Node node) {
if (isVisible()) {
for (int j = 0, m = categories.length; j < m; j++) {
dataset.addValue(0, categories[(j + categoryOrder) % categories.length], node.getName());
}
int count = node.getSensorDataCount();
if (count > 0) {
addSensorData(node.getSensorData(count - 1));
}
}
}
@Override
public void nodesSelected(Node[] nodes) {
}
@Override
public void nodeDataReceived(SensorData data) {
if (isVisible()) {
addSensorData(data);
}
}
@Override
public void clearNodeData() {
if (isVisible()) {
updateCharts();
}
}
private void updateCharts() {
dataset.clear();
Node[] nodes = server.getNodes();
if (nodes != null) {
for (int i = 0, n = nodes.length; i < n; i++) {
for (int j = 0, m = categories.length; j < m; j++) {
dataset.addValue(0, categories[(j + categoryOrder) % categories.length], nodes[i].getName());
}
int count = nodes[i].getSensorDataCount();
if (count > 0) {
addSensorData(nodes[i].getSensorData(count - 1));
}
}
}
}
public void setVisible(boolean visible) {
if (visible) {
updateCharts();
} else {
dataset.clear();
}
super.setVisible(visible);
}
protected abstract void addSensorData(SensorData data);
}

View file

@ -0,0 +1,581 @@
/*
* 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: MapPanel.java,v 1.1 2008/07/09 23:18:07 nifi Exp $
*
* -----------------------------------------------------------------
*
* MapPanel
*
* Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008
* Updated : $Date: 2008/07/09 23:18:07 $
* $Revision: 1.1 $
*/
package se.sics.contiki.collect.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.Hashtable;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.Timer;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import se.sics.contiki.collect.CollectServer;
import se.sics.contiki.collect.Link;
import se.sics.contiki.collect.Node;
import se.sics.contiki.collect.SensorData;
import se.sics.contiki.collect.Visualizer;
/**
*
*/
public class MapPanel extends JPanel implements Visualizer, ActionListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = -8256619482599309425L;
private static final Logger log =
Logger.getLogger(MapPanel.class.getName());
private static final boolean VISUAL_DRAG = true;
private static final int FADE_COUNT = 20;
private static final int AGE_COUNT = 200;
private static final Color[] OTHER_COLOR = new Color[FADE_COUNT];
private static final Color[] LINK_COLOR = new Color[AGE_COUNT];
static {
for (int i = 0; i < FADE_COUNT; i++) {
OTHER_COLOR[i] = new Color(0xe0,0xe0,0x00,0xFF
- ((i * 255) / (FADE_COUNT - 1)));
}
for (int i = 0, n = AGE_COUNT; i < n; i++) {
LINK_COLOR[i] = new Color(0x40 + i / 2, 0x40 + i / 2, 0xf0, 0xff - i);
}
}
private static final int delta = 7;
public static final int SHOW_BLINK = 40;
public static final int TOTAL_SHOW = 600;
private Timer timer = new Timer(200, this);
private boolean hasPendingEvents = false;
private int ticker = 0;
private JPopupMenu popupMenu;
// private MapNode popupNode;
private JMenuItem hideItem;
private JMenuItem resetNetworkItem;
private Hashtable<String,MapNode> nodeTable = new Hashtable<String,MapNode>();
private MapNode[] nodeList;
private MapNode selectedNode;
private MapNode[] selectedMapNodes;
private Node[] selectedNodes;
private MapNode draggedNode;
private long draggedTime;
private ImageIcon mapImage;
private String mapName;
private final CollectServer server;
private boolean hideNetwork = false;
public MapPanel(CollectServer server) {
super(new BorderLayout());
this.server = server;
setPreferredSize(new Dimension(300, 200));
popupMenu = new JPopupMenu(getTitle());
// popupMenu.addSeparator();
hideItem = createMenuItem(popupMenu, "Hide Network");
resetNetworkItem = createMenuItem(popupMenu, "Reset Network");
addMouseListener(this);
if (VISUAL_DRAG) {
addMouseMotionListener(this);
}
setBackground(Color.white);
}
public String getMapBackground() {
return mapName;
}
public boolean setMapBackground(String image) {
if (image == null) {
mapImage = null;
mapName = null;
return true;
}
ImageIcon ii = new ImageIcon(image);
if (ii.getIconWidth() <= 0 || ii.getIconHeight() <= 0) {
log.warning("could not find image '" + image + '\'');
return false;
}
mapImage = ii;
mapName = image;
setPreferredSize(new Dimension(ii.getIconWidth(), ii.getIconHeight()));
repaint();
return true;
}
private JMenuItem createMenuItem(JPopupMenu menu, String title) {
JMenuItem item = new JMenuItem(title);
item.addActionListener(this);
menu.add(item);
return item;
}
public void setVisible(boolean visible) {
if (visible) {
clear();
timer.start();
} else {
timer.stop();
}
super.setVisible(visible);
}
public void clear() {
draggedNode = null;
hasPendingEvents = false;
}
// -------------------------------------------------------------------
// Node handling
// -------------------------------------------------------------------
public Node getNode(String id) {
MapNode node = nodeTable.get(id);
return node != null ? node.node : null;
}
public MapNode getMapNode(String id) {
return nodeTable.get(id);
}
private MapNode addMapNode(Node nd) {
MapNode node = nodeTable.get(nd.getID());
if (node == null) {
node = new MapNode(this, nd);
synchronized (this) {
nodeTable.put("" + nd.getID(), node);
nodeList = nodeTable.values().toArray(new MapNode[nodeTable.size()]);
}
}
return node;
}
// -------------------------------------------------------------------
// Visualizer
// -------------------------------------------------------------------
@Override
public String getTitle() {
return "Sensor Map";
}
@Override
public Component getPanel() {
return this;
}
@Override
public void nodesSelected(Node[] nodes) {
if (selectedNodes != nodes) {
if (selectedMapNodes != null) {
for(MapNode node : selectedMapNodes) {
node.isSelected = false;
}
}
selectedNodes = nodes;
if (nodes == null || nodes.length == 0) {
selectedNode = null;
selectedMapNodes = null;
} else {
selectedMapNodes = new MapNode[nodes.length];
for (int i = 0, n = nodes.length; i < n; i++) {
selectedMapNodes[i] = addMapNode(nodes[i]);
selectedMapNodes[i].isSelected = true;
}
selectedNode = selectedMapNodes[0];
}
repaint();
}
}
@Override
public void nodeAdded(Node nd) {
addMapNode(nd);
repaint();
}
@Override
public void nodeDataReceived(SensorData sensorData) {
repaint();
}
@Override
public void clearNodeData() {
// Ignore
}
// -------------------------------------------------------------------
// Graphics
// -------------------------------------------------------------------
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int lx = 10;
super.paint(g);
long time = System.currentTimeMillis();
if (mapImage != null) {
mapImage.paintIcon(this, g, 0, 0);
}
MapNode[] nodes = nodeList;
if (nodes != null) {
Line2D line = new Line2D.Double();
for (int i = 0, m = nodes.length; i < m; i++) {
MapNode n = nodes[i];
int x, y;
if (n.node.hasLocation()) {
x = n.node.getX();
y = n.node.getY();
} else {
x = lx;
y = 10;
lx += 30;
}
if (!hideNetwork) {
FontMetrics fm = g.getFontMetrics();
int fnHeight = fm.getHeight();
int fnDescent = fm.getDescent();
for (int j = 0, mu = n.node.getLinkCount(); j < mu; j++) {
Link link = n.node.getLink(j);
if (link.node.hasLocation()) {
long age = (time - link.getLastActive()) / 100;
int x2 = link.node.getX();
int y2 = link.node.getY();
if (n.isSelected) {
if (age > LINK_COLOR.length) {
age = 100;
} else {
age -= 50;
}
}
line.setLine(x, y, x2, y2);
if (age < LINK_COLOR.length) {
g.setColor(age < 0 ? LINK_COLOR[0] : LINK_COLOR[(int) age]);
} else {
g.setColor(LINK_COLOR[LINK_COLOR.length - 1]);
}
g2d.draw(line);
// g.setColor(Color.lightGray);
int xn1, xn2, yn1, yn2;
if (x <= x2) {
xn1 = x; xn2 = x2;
yn1 = y; yn2 = y2;
} else {
xn1 = x2; xn2 = x;
yn1 = y2; yn2 = y;
}
int dx = xn1 + (xn2 - xn1) / 2 + 4;
int dy = yn1 + (yn2 - yn1) / 2 - fnDescent;
if (yn2 < yn1) {
dy += fnHeight - fnDescent;
}
g.drawString("ETX:" + (((int)(link.getETX() * 100 + 0.5)) / 100.0), dx, dy);
}
}
}
n.paint(g, x, y);
g.setColor(Color.black);
if (n.isSelected) {
BasicGraphicsUtils.drawDashedRect(g, x - delta, y - delta, 2 * delta, 2 * delta);
}
if (selectedNode != null && selectedNode.message != null) {
g.drawString(selectedNode.message, 10, 10);
}
}
}
}
// -------------------------------------------------------------------
// ActionListener
// -------------------------------------------------------------------
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == timer) {
ticker++;
if (hasPendingEvents) {
boolean repaint = false;
hasPendingEvents = false;
MapNode[] nodes = nodeList;
if (nodes != null) {
long time = System.currentTimeMillis();
for (int i = 0, n = nodes.length; i < n; i++) {
if (nodes[i].tick(time)) {
repaint = true;
}
}
}
if (repaint) {
hasPendingEvents = true;
repaint();
}
} else if ((ticker % 10) == 0) {
repaint();
}
} else if (source == hideItem) {
hideNetwork = !hideNetwork;
if (!hideNetwork) hideItem.setText("Hide Network");
else hideItem.setText("Show Network");
repaint();
} else if (source == resetNetworkItem) {
MapNode[] nodes = nodeList;
if (nodes != null) {
for (int i = 0, m = nodes.length; i < m; i++) {
MapNode n = nodes[i];
n.node.clearLinks();
}
repaint();
}
}
}
// -------------------------------------------------------------------
// Mouselistener
// -------------------------------------------------------------------
private MapNode getNodeAt(int mx, int my) {
int lx = 10;
MapNode[] nodes = nodeList;
if (nodes != null) {
for (int i = 0, m = nodes.length; i < m; i++) {
MapNode n = nodes[i];
int x, y;
if (n.node.hasLocation()) {
x = n.node.getX();
y = n.node.getY();
} else {
x = lx;
y = 10;
lx += 30;
}
if (mx >= (x - delta)
&& mx <= (x + delta)
&& my >= (y - delta)
&& my <= (y + delta)) {
return n;
}
}
}
return null;
}
public void mouseClicked(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if (e.getButton() == MouseEvent.BUTTON1) {
MapNode node = getNodeAt(mx, my);
if (node != selectedNode) {
server.selectNodes(node == null ? null : new Node[] { node.node });
}
}
showPopup(e);
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
MapNode aNode = getNodeAt(e.getX(), e.getY());
if (aNode != selectedNode) {
server.selectNodes(aNode != null ? new Node[] { aNode.node } : null);
}
draggedNode = aNode;
draggedTime = System.currentTimeMillis();
} else if (selectedNode != null) {
selectedNode = draggedNode = null;
server.selectNodes(null);
}
showPopup(e);
}
public void mouseReleased(MouseEvent e) {
if (draggedNode != null && e.getButton() == MouseEvent.BUTTON1) {
if ((!VISUAL_DRAG || (draggedTime > 0)) &&
(System.currentTimeMillis() - draggedTime) < 300) {
// Do not drag if mouse is only moved during click
} else {
draggedNode.node.setLocation(e.getX(), e.getY());
server.updateNodeLocation(draggedNode.node);
draggedNode = null;
repaint();
}
}
showPopup(e);
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()
&& (e.getModifiers() & (MouseEvent.SHIFT_MASK|MouseEvent.CTRL_MASK)) == 0) {
// popupNode = getNodeAt(e.getX(), e.getY());
// nodeItem.setEnabled(popupNode != null);
popupMenu.show(this, e.getX(), e.getY());
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
// -------------------------------------------------------------------
// MouseMotion
// -------------------------------------------------------------------
public void mouseDragged(MouseEvent e) {
if (!VISUAL_DRAG || draggedNode == null) {
// Do nothing
} else if (draggedTime > 0) {
if ((System.currentTimeMillis() - draggedTime) > 300) {
// No mouse click, time to drag the node
draggedTime = -1;
}
} else {
draggedNode.node.setLocation(e.getX(), e.getY());
repaint();
}
}
public void mouseMoved(MouseEvent e) {
}
// -------------------------------------------------------------------
// MapNode
// -------------------------------------------------------------------
private static class MapNode {
public final Node node;
public boolean isSelected;
public String message = null;
private int tick = 0;
MapNode(MapPanel panel, Node node) {
this.node = node;
}
boolean tick(long time) {
boolean r = false;
if (tick > 0) {
tick--;
r = true;
}
for (int i = 0, n = node.getLinkCount(); i < n; i++) {
Link link = node.getLink(i);
long age = (time - link.getLastActive()) / 100;
if (age < 200) {
r = true;
break;
}
}
return r;
}
public void paint(Graphics g, int x, int y) {
if (tick > (TOTAL_SHOW - SHOW_BLINK)) {
if ((tick & 4) == 0) {
// Hide circle
} else {
int index = FADE_COUNT - tick - 1;
if (index < 0) {
index = 0;
}
final int d = 8;
g.setColor(OTHER_COLOR[index]);
g.fillOval(x - d, y - d, d * 2 + 1, d * 2 + 1);
}
}
if (tick < (TOTAL_SHOW - SHOW_BLINK) && tick > 0) {
g.setColor(Color.red);
int height = 13 * tick / TOTAL_SHOW;
g.fillRect(x - 6, 5 + y - height, 2, height);
}
g.setColor(Color.black);
final int od = 3;
g.drawString(node.getID(), x + od * 2 + 3, y + 4);
g.fillOval(x - od, y - od, od * 2 + 1, od * 2 + 1);
}
} // end of inner class MapNode
}

View file

@ -0,0 +1,215 @@
/*
* 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: SerialConsole.java,v 1.1 2008/07/09 23:18:07 nifi Exp $
*
* -----------------------------------------------------------------
*
* SerialConsole
*
* Authors : Joakim Eriksson, Niclas Finne
* Created : 4 jul 2008
* Updated : $Date: 2008/07/09 23:18:07 $
* $Revision: 1.1 $
*/
package se.sics.contiki.collect.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import se.sics.contiki.collect.CollectServer;
import se.sics.contiki.collect.Node;
import se.sics.contiki.collect.SensorData;
import se.sics.contiki.collect.Visualizer;
/**
*
*/
public class SerialConsole implements Visualizer {
private final CollectServer server;
private JPanel panel;
private JTextArea logArea;
private JTextField commandField;
private String[] history = new String[50];
private int historyPos = 0;
private int historyCount = 0;
public SerialConsole(CollectServer server) {
this.server = server;
panel = new JPanel(new BorderLayout());
logArea = new JTextArea(4, 30);
logArea.setEditable(false);
panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem clearItem = new JMenuItem("Clear");
clearItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logArea.setText("");
}
});
popupMenu.add(clearItem);
logArea.setComponentPopupMenu(popupMenu);
commandField = new JTextField();
commandField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = trim(commandField.getText());
if (command != null) {
try {
int previous = historyCount - 1;
if (previous < 0) previous += history.length;
if (!command.equals(history[previous])) {
history[historyCount] = command;
historyCount = (historyCount + 1) % history.length;
}
historyPos = historyCount;
SerialConsole.this.server.sendToNode(command);
commandField.setText("");
} catch (Exception ex) {
System.err.println("could not send '" + command + "':");
ex.printStackTrace();
JOptionPane.showMessageDialog(panel,
"could not send '" + command + "':\n"
+ ex, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
} else {
commandField.getToolkit().beep();
}
}
});
commandField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: {
int nextPos = (historyPos + history.length - 1) % history.length;
if (nextPos == historyCount || history[nextPos] == null) {
commandField.getToolkit().beep();
} else {
String cmd = trim(commandField.getText());
if (cmd != null) {
history[historyPos] = cmd;
}
historyPos = nextPos;
commandField.setText(history[historyPos]);
}
break;
}
case KeyEvent.VK_DOWN: {
int nextPos = (historyPos + 1) % history.length;
if (nextPos == historyCount) {
historyPos = nextPos;
commandField.setText("");
} else if (historyPos == historyCount || history[nextPos] == null) {
commandField.getToolkit().beep();
} else {
String cmd = trim(commandField.getText());
if (cmd != null) {
history[historyPos] = cmd;
}
historyPos = nextPos;
commandField.setText(history[historyPos]);
}
break;
}
}
}
});
panel.add(commandField, BorderLayout.SOUTH);
}
@Override
public Component getPanel() {
return panel;
}
@Override
public String getTitle() {
return "Serial Console";
}
@Override
public void nodeAdded(Node node) {
// Ignore
}
@Override
public void nodeDataReceived(SensorData sensorData) {
// Ignore
}
@Override
public void clearNodeData() {
// Ignore
}
@Override
public void nodesSelected(Node[] node) {
// Ignore
}
public void addSerialData(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String current = logArea.getText();
int len = current.length();
if (len > 4096) {
current = current.substring(len - 4096);
}
current = len > 0 ? (current + '\n' + text) : text;
logArea.setText(current);
logArea.setCaretPosition(current.length());
}
});
}
private String trim(String text) {
return (text != null) && ((text = text.trim()).length() > 0) ? text : null;
}
}

View file

@ -0,0 +1,247 @@
/*
* 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: TimeChartPanel.java,v 1.1 2008/07/09 23:18:07 nifi Exp $
*
* -----------------------------------------------------------------
*
* PowerPanel
*
* Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008
* Updated : $Date: 2008/07/09 23:18:07 $
* $Revision: 1.1 $
*/
package se.sics.contiki.collect.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import se.sics.contiki.collect.CollectServer;
import se.sics.contiki.collect.Node;
import se.sics.contiki.collect.SensorData;
import se.sics.contiki.collect.Visualizer;
/**
*
*/
public abstract class TimeChartPanel extends JPanel implements Visualizer {
private static final long serialVersionUID = -607864439709540641L;
protected final CollectServer server;
protected final String title;
protected final TimeSeriesCollection timeSeries;
protected final JFreeChart chart;
protected final ChartPanel chartPanel;
private Node[] selectedNodes;
private double minValue;
private double maxValue;
private int rangeTick = 0;
private boolean hasGlobalRange;
public TimeChartPanel(CollectServer server, String title,
String chartTitle, String timeAxisLabel, String valueAxisLabel) {
super(new BorderLayout());
this.server = server;
this.title = title;
this.timeSeries = new TimeSeriesCollection();
this.chart = ChartFactory.createTimeSeriesChart(
chartTitle, timeAxisLabel, valueAxisLabel, timeSeries,
true, true, false
);
this.chartPanel = new ChartPanel(chart);
this.chartPanel.setPreferredSize(new Dimension(500, 270));
add(chartPanel, BorderLayout.CENTER);
}
@Override
public String getTitle() {
return title;
}
@Override
public Component getPanel() {
return this;
}
@Override
public void nodeAdded(Node node) {
// Ignore
}
@Override
public void nodesSelected(Node[] nodes) {
if (this.selectedNodes != nodes) {
this.selectedNodes = nodes;
if (isVisible()) {
updateCharts();
}
}
}
@Override
public void nodeDataReceived(SensorData data) {
if (hasGlobalRange) {
boolean update = false;
if (minValue > maxValue) {
update = true;
} else {
double value = getSensorDataValue(data);
if (value < minValue) {
minValue = value;
update = true;
}
if (value > maxValue) {
maxValue = value;
update = true;
}
}
if (update && isVisible()) {
updateGlobalRange();
}
}
if (isVisible() && selectedNodes != null && selectedNodes.length == timeSeries.getSeriesCount()) {
Node node = data.getNode();
for (int i = 0, n = selectedNodes.length; i < n; i++) {
if (node == selectedNodes[i]) {
TimeSeries series = timeSeries.getSeries(i);
series.addOrUpdate(new Second(new Date(data.getTime())), getSensorDataValue(data));
chartPanel.repaint();
break;
}
}
}
}
@Override
public void clearNodeData() {
if (isVisible()) {
updateCharts();
}
}
private void updateCharts() {
timeSeries.removeAllSeries();
if (this.selectedNodes != null) {
for(Node node: this.selectedNodes) {
TimeSeries series = new TimeSeries(node.getName(), Second.class);
for (int i = 0, n = node.getSensorDataCount(); i < n; i++) {
SensorData data = node.getSensorData(i);
series.addOrUpdate(new Second(new Date(data.getTime())), getSensorDataValue(data));
}
timeSeries.addSeries(series);
}
}
}
public int getRangeTick() {
return rangeTick;
}
public void setRangeTick(int rangeTick) {
this.rangeTick = rangeTick;
}
public double getRangeMinimumSize() {
return chart.getXYPlot().getRangeAxis().getAutoRangeMinimumSize();
}
public void setRangeMinimumSize(double size) {
chart.getXYPlot().getRangeAxis().setAutoRangeMinimumSize(size);
}
public boolean hasGlobalRange() {
return hasGlobalRange;
}
public void setGlobalRange(boolean hasGlobalRange) {
if (this.hasGlobalRange != hasGlobalRange) {
this.hasGlobalRange = hasGlobalRange;
if (hasGlobalRange) {
minValue = Double.MAX_VALUE;
maxValue = Double.MIN_NORMAL;
if (isVisible()) {
updateGlobalRange();
}
} else {
chart.getXYPlot().getRangeAxis().setAutoRange(true);
}
}
}
private void updateGlobalRange() {
if (hasGlobalRange) {
if (minValue > maxValue) {
for (int i = 0, n = server.getSensorDataCount(); i < n; i++) {
double value = getSensorDataValue(server.getSensorData(i));
if (value < minValue) minValue = value;
if (value > maxValue) maxValue = value;
}
}
if (minValue < maxValue) {
double minSize = getRangeMinimumSize();
double min = minValue;
double max = maxValue;
if (max - min < minSize) {
double d = (minSize - (max - min)) / 2;
min -= d;
max += d;
}
if (rangeTick > 0) {
min = ((int) (min - rangeTick / 2) / rangeTick) * rangeTick;
// max = ((int) (max + rangeTick / 2) / rangeTick) * rangeTick;
}
chart.getXYPlot().getRangeAxis().setRange(min, max);
}
}
}
protected abstract double getSensorDataValue(SensorData data);
public void setVisible(boolean visible) {
if (visible) {
updateGlobalRange();
updateCharts();
} else {
timeSeries.removeAllSeries();
}
super.setVisible(visible);
}
}