Skip to content
Snippets Groups Projects
powerproc.cpp 2.99 KiB
Newer Older
#include "powerproc.hpp"

#include "mpod.hpp"

PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
    : ipAddr(ipAddr), stagesCount(STAGESCOUNT), currentStage(""), currentChannel(-1) {
    stages = new Stage[stagesCount];

    stages[0].name = "AON";
    stages[0].size = 4;
    stages[0].channels = new int[stages[0].size]{604, 705, 706, 707};
    stages[1].name = "ASICS";
    stages[1].size = 9;
    stages[1].channels = new int[stages[1].size]{508, 708, 502, 503, 504, 501, 507, 607, 608};
    stages[2].name = "HV";
    stages[2].size = 10;
    stages[2].channels = new int[stages[2].size]{1, 2, 101, 103, 102, 104, 105, 106, 107, 108};

    stages[3].name = "PLCSOURCE";
    stages[3].size = 9;
    stages[3].channels = new int[stages[3].size]{701, 801, 802, 803, 804, 301, 302, 303, 304};
}

PowerProcedure::~PowerProcedure() {
    for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
        delete[] stages[stageIdx].channels;
    }
    delete[] stages;
}

String PowerProcedure::toJSON() {
    String json = "{\n";
    json += "\"target\":\"" + ipAddr.toString() + "\",\n";
    json += "\"groups\": [\n";

    for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
        Stage* currentStage = &stages[stageIdx];

        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) {
    for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
        Stage* currentStage = &stages[stageIdx];
        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) {
    for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
        Stage* currentStage = &stages[stageIdx];
        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, 106));