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("/mpodstatus", getMpodStatus);
restServer.on("/power", powerGroup);
restServer.begin();
Serial.println("REST Server Started");
}
void identify() {
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 += "\"status\":{\n";
message += "\"ramping\":";
message += PINS.ramping;
message += ",\n";
message += "\"stage\": \"";
message += "\"\n";
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, PINS.stage, PINS.ramping);
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);
if (PINS.ramping) {
restServer.send(403, "text/json", "{\"success\": 0, \"reason\": \"ramping ongoing\"}");
return;
}
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();
auto ipAddr = pproc.ipAddr;
setChannelStateAndWait(&ipAddr, channel, output);
success = mpod.getChannel() ? true : false;
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 getMpodStatus() {
if (PINS.ramping) {
restServer.send(403, "text/json", "{\"success\": 0, \"reason\": \"ramping ongoing\"}");
return;
}
restServer.send(200, "text/json", pproc.toJSON());
}
if (PINS.ramping) {
restServer.send(403, "text/json", "{\"success\": 0, \"reason\": \"ramping ongoing\"}");
return;
}
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) {
PINS.ramping = true;
PINS.stage = group;
PINS.stage += " going up";
success = pproc.powerOn(group);
} else if (output == "off") {
PINS.stage += " going down";
success = pproc.powerOff(group);
}
PINS.ramping = false;
PINS.stage = "";
}
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";
http_msg += pproc.toJSON();
}
http_msg += "\n}";
restServer.send(success ? 200 : 406, "text/json", http_msg);
}
void powerAllOff() {
if (PINS.ramping) {
restServer.send(403, "text/json", "{\"success\": 0, \"reason\": \"ramping ongoing\"}");
return;
}
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 (int8_t groupIdx = pproc.stagesCount - 1; groupIdx >= 0; groupIdx--) {
group = pproc.stages[groupIdx].name;
groups += group;
groups += ",";
success = pproc.powerOff(group);
if (!success) {
break;
}
}
}
String http_msg = "{\n";
http_msg += "\"groups\":\"" + groups + "\",";
http_msg += "\"success\":" + String(success);
// if (success) {
// http_msg += ",\n\"status\":\n";
// http_msg += pproc.toJSON();
// }
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 = "\"Invalid channel\"";
if (channel) {
SNMP::Message *snmp_msg = mpod.read(channel);
snmp.send(snmp_msg, pproc.ipAddr, SNMP::Port::SNMP);
delay(MPOD_UPDATE_LATENCY);
snmp.loop(); // Force loop to update now
if (mpod.getChannel()) { // Valid channel requested
http_msg += "\"success\": ";
http_msg += channel ? 1 : 0;
http_msg += ",\n";
http_msg += "\"arguments\": {";
http_msg += "\"channel\": ";
http_msg += channel;
http_msg += "},\n"; // arguments
restServer.send(channel ? 200 : 406, "text/json", http_msg);