Skip to content
Snippets Groups Projects
powerproc.cpp 5.03 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 = "ASICS";
    stages[0].type = SWITCH;
    stages[0].size = 15;
    stages[0].channels = new int[stages[0].size]{
        508, 708,            // MBJT
        503, 504,            // AM, RB
        502, 506, 602, 606,  // RB
        501, 505, 601, 605,  // IOB
        507, 607, 608        // IOBA
    };

    stages[1].name = "HV";
    stages[1].type = SWITCH;
    stages[1].size = 40;
    stages[1].channels = new int[stages[1].size]{
        1,   2,   3,   4,   5, 6, 7, 8,  // IS
        101, 103, 102, 104,              // M1 RING1
        113, 114, 115, 116,              // M2 RING1
        213, 214, 215, 216,              // M3 RING1
        201, 203, 202, 204,              // M4 RING1
        105, 106, 107, 108,              // M1 BACK
        109, 110, 111, 112,              // M2 BACK
        209, 210, 211, 212,              // M3 BACK
        205, 206, 207, 208,              // M4 BACK
    };

    stages[2].name = "ISVOLTAGE";
    stages[2].type = VOLTAGE;
    stages[2].size = 8;
    stages[2].channels = new int[stages[2].size]{
        1, 2,  // M1
        3, 4,  // M2
        5, 6,  // M3
        7, 8   // M4
    };
    stages[2].onValue = new float[stages[2].size]{10, 10, 10, 10, 10, 10, 10, 10};
    stages[2].offValue = new float[stages[2].size]{2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5};

    stages[3].name = "PLCSOURCE";
    stages[3].type = SWITCH;
    stages[3].size = 36;
    stages[3].channels = new int[stages[3].size]{
        701, 801, 802, 803, 804,  // M1 PLC
        702, 805, 806, 807, 808,  // M2 PLC
        703, 901, 902, 903, 904,  // M3 PLC
        704, 905, 906, 907, 908,  // M4 PLC
        301, 302, 303, 304,       // M1 SG
        305, 306, 307, 308,       // M2 SG
        401, 402, 403, 404,       // M3 SG
        405, 406, 407, 408,       // M4 SG
    };
}

PowerProcedure::~PowerProcedure() {
    for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
        delete[] stages[stageIdx].channels;
        if (stages[stageIdx].type == VOLTAGE) {
            delete[] stages[stageIdx].onValue;
            delete[] stages[stageIdx].offValue;
        }
    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];
        if (currentStage->type == VOLTAGE) {
            continue;
        }
        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++) {
                if (currentStage->type == SWITCH) {
                    setChannelStateAndWait(&ipAddr, currentStage->channels[chIdx], 1);  // 1 == ON
                } else if (currentStage->type == VOLTAGE) {
                    setChannelVoltageAndWait(&ipAddr, currentStage->channels[chIdx],
                                             currentStage->onValue[chIdx]);
                }
            }
            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--) {
                if (currentStage->type == SWITCH) {
                    setChannelStateAndWait(&ipAddr, currentStage->channels[chIdx], 0);  // 0 == OFF
                } else if (currentStage->type == VOLTAGE) {
                    setChannelVoltageAndWait(&ipAddr, currentStage->channels[chIdx],
                                             currentStage->offValue[chIdx]);
                }
            }
            return true;
        }
    }
    return false;  // Invalid group name
}

PowerProcedure pproc(IPAddress(192, 168, 140, 106));