-
Notifications
You must be signed in to change notification settings - Fork 0
/
c0a0475dc88300131636968a6a637f39
122 lines (111 loc) · 3.43 KB
/
c0a0475dc88300131636968a6a637f39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.apparatus_templi;
public class LedFlash extends Coordinator implements ControlerModule, Runnable {
private String moduleName = "LED_FLASH";
private volatile boolean running = true;
@Override
public String getModuleType() {
return "Controler";
}
@Override
public String getModuleName() {
return moduleName;
}
@Override
public void terminate() {
running = false;
}
/*
* Since every driver is exited to have intimate knowledge of
* the remote module that it corresponds with this can be a hard
* coded XML response.
*/
@Override
public String getControlerListXML() {
return new String( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<controlerList>" +
"<controler>" +
"<name>LED 1</name>" +
"</controler>" +
"<controler>" +
"<name>LED 2</name>" +
"</controler>" +
"<controler>" +
"<name>LED 1</name>" +
"</controler>" +
"</controlerList>");
}
/*
* Since our simple driver does not keep track of the status of the LEDs
* it can only respond with 'Unknown' for the status. If this were a real
* driver you would want to check that the controlerName is valid, and
* return a response based of of the controller's last known status.
*/
@Override
public String getControlerStatusXML(String controllerName) {
return new String( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<status>Unknown</status>");
}
/*
* This method will be called by the Coordinator on behalf
* of the front-ends. It is up to you do validate the incoming
* controllerName and command (if desired). In this case we will
* switch based off the controllerName. Since the only thing that
* this driver does is flash the LED we do not even have to check the
* command.
*/
@Override
public void tellController(String controllerName, String command) {
switch (controllerName) {
case "LED 1":
super.sendCommand(moduleName, "4");
break;
case "LED 2":
super.sendCommand(moduleName, "5");
break;
case "LED 3":
super.sendCommand(moduleName, "6");
break;
default:
super.logMessage(moduleName, "tellController() Given invalid LED name");
break;
}
}
/*
* Our starting point of execution for this driver.
* This simply runs a loop sending commands to flash LEDs attached to
* different pins. The driver does not care about responses from the
* remote module, so receiveMessage() is left unimplemented. If the
* Coordinator needs to shut down this thread it can do so through the
* terminate() method, which will make the while loop exit
*
* The startup procedure is very simple, ask the Coordinator if the
* remote module is active. If it isn't, then terminate, else begin sending
* commands
*/
@Override
public void run() {
if (super.isModulePresent(moduleName)) {
while (running) {
/*
* This is our main loop. All of the processing will happen here
* Our simple driver will repeatedly send three messages to the
* remote module, sleeping 5 seconds between each message.
*/
for (int i = 3; i < 6; i++) {
super.sendCommand(moduleName, String.valueOf(i));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/*
* We don't care about any response messages.
*/
@Override
public void receiveMessage(String message) {}
}