diff --git a/icbm.ino b/icbm.ino index 5e4791b1f893d8497a911ea585b1f9e778bc4a71..885064f592a90f4040c444dc7568536478e23293 100644 --- a/icbm.ino +++ b/icbm.ino @@ -12,9 +12,7 @@ void setup() { initializeNetwork(); initializeRoutes(); - Serial.println("Started REST server"); initializeSNMP(); - Serial.println("Started SNMP server"); start = millis(); } diff --git a/mpod.cpp b/mpod.cpp index 6f65d2241e8f05c8cfe28e37432e31daa0d6a0a8..78c94e9a01d046b34345628a2ba65a678272b590 100644 --- a/mpod.cpp +++ b/mpod.cpp @@ -146,6 +146,21 @@ float MPOD::getFloatFromVarBind(const VarBind *varbind) { return static_cast<OpaqueFloatBER*>(static_cast<OpaqueBER*>(varbind->getValue())->getBER())->getValue(); } +String MPOD::toJSON() { + String json = "\"status\":{\n"; + json += "\"is_on\":" + String(isOn()) + ","; + json += "\"is_up\":" + String(isUp()) + ","; + json += "\"is_down\":" + String(isDown()) + ","; + json += "\"sense_voltage\":" + String(getMeasurementSenseVoltage()) + ","; + json += "\"set_voltage\":" + String(getVoltage()) + ","; + json += "\"sense_current\":" + String(getMeasurementCurrent()) + ","; + json += "\"set_current\":" + String(getCurrent()) + ","; + json += "\"voltage_rise_rate\":" + String(getVoltageRiseRate()); + json += "}"; + + return json; +} + WiFiUDP udp; SNMP::Manager snmp; @@ -183,4 +198,5 @@ void onMessage(const SNMP::Message *message, const IPAddress remote, const uint1 void initializeSNMP() { snmp.begin(&udp); snmp.onMessage(onMessage); + Serial.println("SNMP Server Started"); } diff --git a/mpod.hpp b/mpod.hpp index bc51a80a932c02c2fe7b11374f9fca608569b5cb..00dc72496b317ddc7097bce73a0d930ca5ea645b 100644 --- a/mpod.hpp +++ b/mpod.hpp @@ -62,6 +62,7 @@ class MPOD { float getVoltage() const; float getCurrent() const; float getVoltageRiseRate() const; + String toJSON(); private: unsigned int getIntegerFromVarBind(const VarBind*); diff --git a/rest.cpp b/rest.cpp index b0a53c106570b2bf84903a2905edbb5433773a6b..5f4e79d774d78944998c9918d7fa925baeb72fac 100644 --- a/rest.cpp +++ b/rest.cpp @@ -17,6 +17,7 @@ void initializeRoutes() { restServer.on("/restart", restart); restServer.onNotFound(notFound); restServer.on("/send", sendSNMP); + restServer.on("/poll", pollMPODChannel); restServer.begin(); Serial.println("REST Server Started"); @@ -102,18 +103,29 @@ void sendSNMP() { } } - auto ip_addr = IPAddress(10, 42, 0 ,1); + auto ipAddr = IPAddress(10, 42, 0 ,1); if (output != NONE) { SNMP::Message *snmp_msg = mpod.output(output); - snmp.send(snmp_msg, ip_addr, SNMP::PORT::SNMP); + snmp.send(snmp_msg, ipAddr, SNMP::PORT::SNMP); delete snmp_msg; success = true; } String http_msg = "{\n"; - http_msg += "\"target\":\"" + String(ip_addr) + "\","; - http_msg += "\"arguments\":{\"output\":\"" + String(output) + "\"},\n"; - http_msg += "\"success\":\"" + String(success) + "\""; + http_msg += "\"target\":\"" + ipAddr.toString() + "\","; + http_msg += "\"arguments\":{\"output\":" + String(output) + "},\n"; + http_msg += "\"success\":" + String(success); + http_msg += "\n}"; + + restServer.send(200, "text/json", http_msg); +} + +void pollMPODChannel() { + auto ret = mpod.toJSON(); + + String http_msg = "{\n"; + http_msg += ret; + http_msg += "\"success\": 1"; http_msg += "\n}"; restServer.send(200, "text/json", http_msg); diff --git a/rest.hpp b/rest.hpp index c77f5cae2ce3b4954fba331eec70368368f52a94..03f4383bbb8e2bd0e1f4f22085be0a8249568b17 100644 --- a/rest.hpp +++ b/rest.hpp @@ -9,3 +9,4 @@ void identify(); // /idn void notFound(); // 404 void restart(); // /restart void sendSNMP(); // /send?output=on +void pollMPODChannel(); //mock the hardcoded mpod channel 0