Newer
Older
#endif
#ifndef ICBM_GIT_TIMESTAMP
#define ICBM_GIT_TIMESTAMP "UNKNOWN"
#endif
WebServer restServer(80);
void initializeRoutes() {
restServer.on("/idn", identify);
restServer.on("/poll", pollMPODChannel);
restServer.on("/power", powerGroup);
restServer.begin();
Serial.println("REST Server Started");
}
void identify() {
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
int seconds = millis() / 1000;
int hours = seconds / 3600;
int minutes = (seconds / 60) % 60;
seconds = seconds % 60;
String message = "{\n";
message += "\"mac\":\"";
message += ETH.macAddress();
message += "\",\n";
message += "\"version\":\"";
message += ICBM_GIT_VERSION;
message += "\",\n";
message += "\"build date\":\"";
message += ICBM_GIT_TIMESTAMP;
message += "\",\n";
message += "\"uptime\": \"";
message += hours;
message += ":";
message += minutes;
message += ":";
message += seconds;
message += "\",\n";
message += "\"inputs\":{\n";
message += "\"sib\":";
message += PINS.sib;
message += ",\n";
message += "\"plc\":";
message += PINS.plc;
message += ",\n";
message += "\"ups\":";
message += PINS.ups;
message += ",\n";
unsigned long elapsed_seconds = (millis() - PINS.last_triggered) / 1000;
message += "\"sec_since_interrupt\":";
message += elapsed_seconds;
message += "\n}";
// TODO: Add here power sequence status
// is_ramping_down
// current step
// percentage
restServer.send(418, "text/json", message);
unsigned long elapsed_seconds = (millis() - PINS.last_triggered) / 1000;
String content = buildPanel(PINS.sib, PINS.plc, PINS.ups, elapsed_seconds);
restServer.send(200, "text/html", content);
}
String message = "{\n";
message += "\"uri\":\"";
message += (restServer.method() == HTTP_GET) ? "GET" : "POST";
message += "\",\n\"arguments\":{";
for (uint8_t i = 0; i < restServer.args(); i++) {
message += "\n\"" + restServer.argName(i) + "\":\"" + restServer.arg(i) + "\",";
if (restServer.args() != 0) {
message.remove(message.length() - 1); // remove trailing comma for valid json
}
restServer.send(404, "text/json", message);
for (uint8_t i = 0; i < restServer.args(); i++) {
argName = restServer.argName(i);
argName.toLowerCase();
argVal = restServer.arg(i);
if (argName == "output") {
argVal.toLowerCase();
if (argVal == "on") {
} else if (argName == "ch") {
channel = argVal.toInt();
if (output != NONE && channel != 0) {
setChannelAndWait(&ipAddr, channel, output);
success = true; // validates input parameters, not MPOD status, reported separately
String http_msg = "{\n";
http_msg += "\"target\":\"" + ipAddr.toString() + "\",\n";
http_msg += "\"arguments\":{";
http_msg += "\"channel\":" + String(channel) + ",";
http_msg += "\"output\":" + String(output) + "},\n";
restServer.send(success ? 200 : 406, "text/json", http_msg);
}
void powerGroup() {
String output;
String group;
String ret;
bool success = false;
for (uint8_t i = 0; i < restServer.args(); i++) {
argName = restServer.argName(i);
argName.toLowerCase();
argVal = restServer.arg(i);
argVal.toLowerCase();
if (argName == "output") {
if (argVal == "on" || argVal == "off") {
output = argVal;
} else if (argName == "group") {
argVal.toUpperCase(); // Uppercase to match Karabo definitions
group = argVal;
}
}
if (output && group) {
if (output == "on") {
success = pproc.powerOn(group);
} else if (output == "off") {
success = pproc.powerOff(group);
}
}
String http_msg = "{\n";
http_msg += "\"arguments\":{";
http_msg += "\"group\":\"" + group + "\",";
http_msg += "\"output\":\"" + output + "\"},\n";
http_msg += "\"success\":" + String(success);
http_msg += ",\n\"status\":\n";
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
http_msg += pproc.toJSON();
}
http_msg += "\n}";
restServer.send(success ? 200 : 406, "text/json", http_msg);
}
void powerAllOff() {
String group;
String groups;
bool success = false;
for (uint8_t i = 0; i < restServer.args(); i++) {
// Check for token to prevent accidental power down
if (restServer.argName(i) == "token" && restServer.arg(i) == "CWH") {
success = true;
}
}
if (success) {
for (uint8_t groupIdx = pproc.stagesCount; groupIdx >= 0; groupIdx--) {
group = pproc.stages[groupIdx].name;
groups += group;
groups += ",";
success = pproc.powerOff(group);
if (!success) {
break;
}
}
}
String http_msg = "{\n";
http_msg += "\"arguments\":{";
http_msg += "\"groups\":\"" + groups + "\",";
http_msg += "\"success\":" + String(success);
if (success) {
http_msg += ",\n\"status\":\n";
http_msg += pproc.toJSON();
}
http_msg += "\n}";
restServer.send(success ? 200 : 406, "text/json", http_msg);
for (uint8_t i = 0; i < restServer.args(); i++) {
if (restServer.argName(i) == "ch") {
channel = restServer.arg(i).toInt();
}
}
String ret;
ret = "\"reason\": \"Invalid channel\"";
} else {
SNMP::Message *snmp_msg = mpod.read(channel);
snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP);
delete snmp_msg;
delay(MPOD_UPDATE_LATENCY);
snmp.loop(); // Force loop to update now
ret = mpod.toJSON();
restServer.send(channel ? 200 : 406, "text/json", http_msg);