使用 Sun SPOT 作为构建监视器
2009-12-22 00:00:00 来源:WEB开发网该方法首先将所有 LED 变成白色,表示 SPOT 就绪。然后,与 CanaryHandler 类似,它打开一个 radiostream 连接以及数据输入和输出流。但是,在这里,它首先尝试从输入流读一条消息,并阻塞直到收到消息。
如清单 6 所示,当收到一条消息时,setColour() 方法产生另一个线程,以更新 SPOT 上的 LED 的颜色。然后,BuildCanary 回到 while 循环的顶端,等待下一条消息。
清单 6. 产生一个设置颜色的新线程/**
* Set a colour for the LEDs to display.
*
* @param colour an LEDColor value.
* @param buildStatus the current status if the build.
*/
private void setColour(final LEDColor colour, final String buildStatus) {
if (colourThread != null) {
colourThread.interrupt();
}
setColour = new SetColour(colour, buildStatus);
colourThread = new Thread(setColour);
colourThread.start();
}
与此同时,真正操作 LED 是在 SetColour 类中,如清单 7 所示:
清单 7. 操作 LED 的内部类/**
* An inner class to handle the colour and behaviour (flashing, solid,
* running) of the LEDs of a SPOT.
*/
public class SetColour implements Runnable {
/**
* A reference to the SPOTs LEDs so they can be set according to the
* state of the build.
*/
private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();
/**
* The RGB colour to set the LEDs to.
*/
private LEDColor colour;
/**
* The current status of the build. This will be used to set the
* behaviour of the LEDs (flashing, solid, running).
*/
private String buildStatus;
public SetColour(LEDColor colour, final String buildStatus) {
this.colour = colour;
this.buildStatus = buildStatus;
}
public void run() {
try {
if (buildStatus.equals("RUN")) {
// Display running LEDs.
while (true) {
for (int i = 0; i < 8; i++) {
leds[i].setColor(colour);
leds[i].setOn();
Thread.sleep(200);
leds[i].setOff();
}
}
} else if (buildStatus.equals("FAIL")) {
// Flash the LEDs on and off.
while (true) {
for (int i = 0; i < 8; i++ ) {
leds[i].setColor(colour);
leds[i].setOn();
}
Thread.sleep(250);
for (int i = 0; i < 8; i++ ) {
leds[i].setOff();
}
Thread.sleep(250);
}
} else {
// Display the LEDs as a solid bar.
for (int i = 0; i < 8; i++) {
leds[i].setColor(colour);
leds[i].setOn();
}
}
} catch (InterruptedException ie) {
// Do nothing. Just bail out so we can set the LEDs to another colour.
}
}
}
更多精彩
赞助商链接