taskvisualization.java
来自「tinyos最新版」· Java 代码 · 共 1,070 行 · 第 1/3 页
JAVA
1,070 行
s = (String)JOptionPane.showInputDialog(mainPanel,
"Specify a minimum value:",
"Minimum Value",
JOptionPane.PLAIN_MESSAGE,
null, null, new Integer((int)MIN_VAL).toString());
if ((s != null) && (s.length() > 0)) {
try {
MIN_VAL = (float)(new Integer(s).intValue());
} catch (NumberFormatException e) {
}
}
}
public Paint getPaintForPixel(int x, int y, int width, int height) {
//scan through each mote, computing it's weight on this point
// then compute a "reading" for the point
// the map that "reading" into a color
// then construct a paint with the color and certain level of transparency
long weights[] = new long[motes.size()];
Color colors[] = new Color[motes.size()];
if (x > width / 2) width -= (width-x);
else width -= x;
if (y > height / 2) height -= (height-y);
else height -= y;
long maxDist = (int)Math.sqrt((double)(width*width + height*height));
long totalWeight = 0;
float r=0f,g=0f,b=0f;
int curi = -1;
Enumeration e = motes.elements();
while (e.hasMoreElements()) {
SensorMote m = (SensorMote)e.nextElement();
curi++;
int xInt = (int)m.getX();
int yInt = (int)m.getY();
float sensorValue = (float)m.getSensorValue(m.getSensorToVisualize());
weights[curi] = maxDist-(int)distance(x,y,xInt,yInt);
weights[curi] *= weights[curi] * weights[curi];
if (sensorValue > MAX_VAL) sensorValue = MAX_VAL;
if (sensorValue < MIN_VAL) {
sensorValue = MIN_VAL;
weights[curi] = 0;
}
float curr=0,curg=0,curb=0;
curr = (float)((float)fromColor.getRed()/255.0 * ((float)sensorValue - MIN_VAL)/(MAX_VAL-MIN_VAL));
curr += (float)toColor.getRed()/255.0 * (float)(MAX_VAL - sensorValue)/(MAX_VAL-MIN_VAL);
curg = (float)((float)fromColor.getGreen()/255.0 * ((float)sensorValue - MIN_VAL)/(MAX_VAL-MIN_VAL));
curg += (float)toColor.getGreen()/255.0 * (float)(MAX_VAL - sensorValue)/(MAX_VAL-MIN_VAL);
curb = (float)((float)fromColor.getBlue()/255.0 * ((float)sensorValue - MIN_VAL)/(MAX_VAL-MIN_VAL));
curb += (float)toColor.getBlue()/255.0 * (float)(MAX_VAL - sensorValue)/(MAX_VAL-MIN_VAL);
colors[curi] = new Color(curr,curg,curb,1.0f);
totalWeight += weights[curi];
}
for (int i = 0; i < motes.size(); i++) {
if (weights[i] > 0) {
r += ((float)(((float)colors[i].getRed())/255f) * weights[i]) / (float)totalWeight;
g += ((float)(((float)colors[i].getGreen())/255f) * weights[i]) / (float)totalWeight;
b += ((float)(((float)colors[i].getBlue())/255f) * weights[i]) / (float)totalWeight;
}
}
//System.out.println("r = " + r + ", g = " + g + ", b = " + b);
if (r >= 1) r = .99f;
if (g >= 1) g = .99f;
if (b >= 1) b = .99f;
try {
return new Color(r,g,b,Math.min(.7f,Math.max(Math.max(r,g),b)));
} catch (IllegalArgumentException ex) {
System.out.println("Color out of bounds : " + r + ", " + g + ", " + b);
return Color.RED;
}
}
/**
* Retrieves the list of sensor motes from the given table
*
* @param table Table containing the sensor motes information
* @return List of sensor motes
*/
public SensorMotes getSensorMotes() {
SensorMotes sms = new SensorMotes();
Vector mInfos = client.getAllMoteClientInfo(config.getName());
for (int i=0; i<mInfos.size(); i++) {
Mote m = new Mote((TASKMoteClientInfo)mInfos.elementAt(i));
// AKDNEW storing pixels not real SensorMote sm = new SensorMote(new Integer(xConvertRealToPixel(config, m.getX())).doubleValue(),
// new Integer(yConvertRealToPixel(config, m.getY())).doubleValue(),
SensorMote sm = new SensorMote(m.getX(), m.getY(), m.getId());
sms.addMote(sm);
sm.setSensorToVisualize(sensorType);
if (sensorType % 4 == 0) {
sm.setColorScheme(SensorMote.GRAY, 1024);
}
else if (sensorType %4 == 1) {
sm.setColorScheme(SensorMote.RED, 1024);
}
else if (sensorType %4 == 2) {
sm.setColorScheme(SensorMote.BLUE, 1024);
}
else {
sm.setColorScheme(SensorMote.GREEN, 1024);
}
}
return sms;
}
/**
* This method sets the mode of the interface
*
* @param mode Mode to set the interface to
*/
public void setMode(int mode) {
if (currentEventHandler != null) {
currentEventHandler.setActive(false);
}
switch (mode) {
case PAN_MODE: currentEventHandler = panEventHandler;
canvas.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
break;
case SELECT_MODE: currentEventHandler = selectionHandler;
canvas.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
break;
case NO_MODE: currentEventHandler = null;
canvas.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
break;
}
if (currentEventHandler != null) {
currentEventHandler.setActive(true);
}
}
public static int xConvertRealToPixel(Configuration config, double x) {
// xP = (x - xmaxR) / ((xmaxR - xminR)/(xmaxP - xminP)) + xmaxP
return new Double((x - config.getMaximumRealX()) / ((config.getMaximumRealX() - config.getMinimumRealX())/(config.getMaximumPixelX() - config.getMinimumPixelX())) + config.getMaximumPixelX()).intValue();
}
public static int yConvertRealToPixel(Configuration config, double y) {
// yP = (y - ymaxR) / ((ymaxR - yminR)/(ymaxP - yminP)) + ymaxP
return new Double((y - config.getMaximumRealY()) / ((config.getMaximumRealY() - config.getMinimumRealY())/(config.getMaximumPixelY() - config.getMinimumPixelY())) + config.getMaximumPixelY()).intValue();
}
public static double xConvertPixelToReal(Configuration config, int x) {
// x = xmaxR + (xP - xmaxP)*(xmaxR - xminR)/(xmaxP - xminP)
return config.getMaximumRealX() + (x - config.getMaximumPixelX())*(config.getMaximumRealX() - config.getMinimumRealX())/(config.getMaximumPixelX() - config.getMinimumPixelX());
}
public static double yConvertPixelToReal(Configuration config, int y) {
// y = ymaxR + (yP - ymaxP)*(ymaxR - yminR)/(ymaxP - yminP)
return config.getMaximumRealY() + (y - config.getMaximumPixelY())*(config.getMaximumRealY() - config.getMinimumRealY())/(config.getMaximumPixelY() - config.getMinimumPixelY());
}
/**
* This method calculates the distance between 2 points
*
* @param x X coordinate of first point
* @param y Y coordinate of first point
* @param x X coordinate of second point
* @param y Y coordinate of second point
*
* @returns distance between 2 points
*/
public static double distance(int x, int y, int x1, int y1){
return Math.sqrt( (x-x1)*(x-x1)+(y-y1)*(y-y1));
}
public void addResult(TASKResult qr) {
if (qr.getQueryId() == healthQueryId) {
addHealthResult(qr);
}
else {
addSensorResult(qr);
}
}
private void addHealthResult(TASKResult qr) {
// AKDNEW - attribute names
Integer nd = (Integer)qr.getField("nodeId");
if (nd == null)
return;
int node = nd.intValue();
SensorMote mote = motes.getMote(node);
if (mote == null) {
String nodeString = Integer.toString(node);
if (!unknownNodesModel.contains(nodeString)) {
unknownNodesModel.addElement(nodeString);
}
return;
}
// get each data field and update in mote sensor value
for (int i=0; i<qr.getNumFields(); i++) {
int value = 0;
Object o = qr.getField(i);
if (o instanceof Integer) {
Integer tmp = (Integer)o;
value = tmp == null ? 0 : tmp.intValue();
}
else if (o instanceof Byte) {
Byte tmp = (Byte)o;
value = tmp == null ? 0 : tmp.intValue();
}
mote.setSensorValue(i+1, value, qr.getFieldInfo(i).name);
//System.out.println("HEALTH UPDATE: node "+node+", slot: "+i+", value: "+ value);
}
Integer tmp = (Integer)qr.getField("parent");
int parent = tmp == null ? 0 : tmp.intValue();
// routes: need to create SensorLine class, which subclass from ZLine
// add a ZLine as needed, setting time
double x = mote.getX();
double y = mote.getY();
SensorMote moteParent = motes.getMote(parent);
if (moteParent != null) {
// get rid of existing line to parent here
for (int i=0; i<routeGroup.getNumChildren(); i++) {
if (routeGroup.getChild(i) instanceof SensorLine) {
SensorLine sl = (SensorLine)routeGroup.getChild(i);
if ((sl.getX1() == x) && (sl.getY1() == y)) {
routeGroup.removeChild(i);
break;
}
}
}
double xn = moteParent.getX();
double yn = moteParent.getY();
SensorLine sl = new SensorLine(x,y,xn,yn);
sl.setSelectable(false);
routeGroup.addChild(sl);
}
//System.out.println("HEALTH UPDATE: route: "+node+" to "+parent);
}
long lastTime = 0;
private void recomputeGradient() {
if (gRects == null || !doGradient) return;
if (System.currentTimeMillis() - lastTime > 2000) {
int width = config.getMaximumPixelX() - config.getMinimumPixelX();
int height = config.getMaximumPixelY() - config.getMinimumPixelY();
int stepSize = STEP_SIZE;
int stepsX = width/stepSize;
int stepsY = height/stepSize;
lastTime = System.currentTimeMillis();
for (int i = 0; i < stepsX; i++) {
for (int j = 0; j < stepsY; j++) {
Paint p = getPaintForPixel(i * stepSize, j * stepSize, width, height);
ZRectangle r = gRects[i][j];
r.setFillPaint(p);
}
}
scrollPane.repaint();
}
}
private void addSensorResult(TASKResult qr) {
// AKDNEW - nodeId has to be last field
Integer nd = ((Integer)qr.getField("nodeId"));
if (nd == null)
return;
int node = nd.intValue();
SensorMote mote = motes.getMote(node);
if (mote == null) {
String nodeString = Integer.toString(node);
if (!unknownNodesModel.contains(nodeString)) {
unknownNodesModel.addElement(nodeString);
}
return;
}
// get each data field and update in mote sensor value
for (int i=0; i<qr.getNumFields(); i++) {
int value = 0;
Object o = qr.getField(i);
if (o instanceof Integer) {
Integer tmp = (Integer)o;
value = tmp == null ? 0 : tmp.intValue();
}
else if (o instanceof Byte) {
Byte tmp = (Byte)o;
value = tmp == null ? 0 : tmp.intValue();
}
mote.setSensorValue(i+sensorIndex+1, value, qr.getFieldInfo(i).name);
//System.out.println("SENSOR UPDATE ("+sensorIndex+"): node "+node+", slot: "+(i+sensorIndex+1)+", value: "+ ((Integer)qr.getField(i)).intValue());
}
recomputeGradient();
Integer tmp = (Integer)qr.getField("parent");
if (tmp == null)
return;
int parent = tmp == null ? 0 : tmp.intValue();
// routes: need to create SensorLine class, which subclass from ZLine
// add a ZLine as needed, setting time
double x = mote.getX();
double y = mote.getY();
SensorMote moteParent = motes.getMote(parent);
if (moteParent != null) {
// get rid of existing line to parent here
for (int i=0; i<routeGroup.getNumChildren(); i++) {
if (routeGroup.getChild(i) instanceof SensorLine) {
SensorLine sl = (SensorLine)routeGroup.getChild(i);
if ((sl.getX1() == x) && (sl.getY1() == y)) {
routeGroup.removeChild(i);
break;
}
}
}
double xn = moteParent.getX();
double yn = moteParent.getY();
SensorLine sl = new SensorLine(x,y,xn,yn);
sl.setSelectable(false);
routeGroup.addChild(sl);
}
}
/**
* Inner class containing the repeated task to run. This handles the removal of old parent information
*/
class Task extends TimerTask {
public void run() {
for (int i=0; i<routeGroup.getNumChildren(); i++) {
if (routeGroup.getChild(i) instanceof SensorLine) {
SensorLine sl = (SensorLine)routeGroup.getChild(i);
long time = new Date().getTime();
if (time - sl.getTime() > 4*healthSamplePeriod) {
routeGroup.removeChild(i);
i--;
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?