#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 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));