diff --git a/mpod.cpp b/mpod.cpp index 80a8280f23c6519311f41e13eade1cbb6578baed..160fd723cf62c22072695de6ff6eee032138f849 100644 --- a/mpod.cpp +++ b/mpod.cpp @@ -190,3 +190,26 @@ void initializeSNMP() { snmp.onMessage(onMessage); Serial.println("SNMP Server Started"); } + +/* setChannelAndWait + * Set a channel's parameters and wait until it's settled. + */ +void setChannelAndWait(IPAddress *ipAddr, uint16_t channel, uint8_t output) { + // Send set command + SNMP::Message *snmp_msg = mpod.output(channel, output); + snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP); + delete snmp_msg; + + // wait for channel reply + delay(MPOD_UPDATE_LATENCY); + snmp.loop(); + + // Poll channel until it's settled + do { + SNMP::Message *snmp_msg = mpod.read(channel); + snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP); + delete snmp_msg; + delay(MPOD_UPDATE_LATENCY); + snmp.loop(); + } while (mpod.isOn() != (bool)output || mpod.isRampingDown() || mpod.isRampingUp()); +} diff --git a/mpod.hpp b/mpod.hpp index ca0b8551cbcac6edf249944fdc1efba375f5b58b..0680e043bae0ac0a6d2ec1e33e6282a7a4cfc8ea 100644 --- a/mpod.hpp +++ b/mpod.hpp @@ -95,4 +95,5 @@ extern MPOD mpod; // Event handler to process SNMP messages void onSNMPMessage(const SNMP::Message *message, const IPAddress remote, const uint16_t port); void initializeSNMP(); +void setChannelAndWait(IPAddress*, uint16_t, uint8_t); diff --git a/rest.cpp b/rest.cpp index 9d5504053f1c0a5249c09b7441b08291163c2af3..f4f5226192b706d2199bbb993c57c8a196438fc5 100644 --- a/rest.cpp +++ b/rest.cpp @@ -117,9 +117,9 @@ void restart() { } enum { - OFF, - ON, - NONE, + OFF = 0, + ON = 1, + NONE = -1, }; void sendSNMP() { @@ -140,17 +140,10 @@ void sendSNMP() { } auto ipAddr = IPAddress(192, 168, 140, 79); - if (output != NONE && channel != 0) { - SNMP::Message *snmp_msg = mpod.output(channel, output); - snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP); - delete snmp_msg; - success = true; - } - if (success) { - SNMP::Message *snmp_msg = mpod.read(channel); - snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP); - delete snmp_msg; + if (output != NONE && channel != 0) { + setChannelAndWait(&ipAddr, channel, output); + success = true; // validates input parameters, not MPOD status, reported separately } String http_msg = "{\n"; @@ -186,7 +179,11 @@ void pollMPODChannel() { SNMP::Message *snmp_msg = mpod.read(channel); snmp.send(snmp_msg, ipAddr, SNMP::Port::SNMP); delete snmp_msg; - ret = mpod.toJSON(); // TODO: This has stale info at this stage! + + delay(MPOD_UPDATE_LATENCY); + snmp.loop(); // Force loop to update now + + ret = mpod.toJSON(); } String http_msg = "{\n";