"README.md" did not exist on "e81d433490c1b5d507cf1749e5ebbd2025061359"
Newer
Older
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
#include "powerproc.hpp"
#include "mpod.hpp"
PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
: ipAddr(ipAddr), currentStage(""), currentChannel(-1) {
stages[0].name = "ASICS";
stages[0].size = 4;
stages[0].channels = new int[stages[0].size]{1, 2, 3, 8};
stages[1].name = "HV";
stages[1].size = 4;
stages[1].channels = new int[stages[1].size]{4, 5, 6, 7};
}
PowerProcedure::~PowerProcedure() {
size_t stagesCount = sizeof(stages) / sizeof(stages[0]);
for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
delete[] stages[stageIdx].channels;
}
}
String PowerProcedure::toJSON() {
String json = "{\n";
json += "\"target\":\"" + ipAddr.toString() + "\",\n";
json += "\"groups\": [\n";
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
json += "{\"name\":\"" + currentStage->name + "\",";
json += "\"channels\":[\n";
for (size_t ch = 0; ch < currentStage->size; ch++) {
SNMP::Message* snmp_msg = mpod.read(currentStage->channels[ch]);
snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP);
delete snmp_msg;
delay(MPOD_UPDATE_LATENCY);
snmp.loop(); // Force loop to update now
json += mpod.toJSON();
json += ",\n";
}
json.remove(json.lastIndexOf(','), 1); // Remove trailing comma of last entry
json += "]\n";
json += "},\n";
}
json.remove(json.lastIndexOf(','), 1); // Remove trailing comma of last entry
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
json += "]\n"; // groups body
json += "}"; // json body
return json;
}
bool PowerProcedure::powerOn(const String& stage) {
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
if (currentStage->name == stage) {
for (size_t chIdx = 0; chIdx < currentStage->size; chIdx++) {
setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 1); // 1 == ON
}
return true;
}
}
return false; // Invalid group name
}
bool PowerProcedure::powerOff(const String& stage) {
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
if (currentStage->name == stage) {
for (int chIdx = currentStage->size - 1; chIdx >= 0; chIdx--) {
setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 0); // 0 == OFF
}
return true;
}
}
return false; // Invalid group name
}
PowerProcedure pproc(IPAddress(192, 168, 140, 79));