From 979a275c75a2ec3c437bc1b399b95f62293a3740 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski <cydanil@gmail.com> Date: Fri, 9 Feb 2024 20:21:28 +0100 Subject: [PATCH] WIP: poll snmp over rest --- icbm.ino | 2 -- mpod.cpp | 16 ++++++++++++++++ mpod.hpp | 1 + rest.cpp | 22 +++++++++++++++++----- rest.hpp | 1 + 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/icbm.ino b/icbm.ino index 5e4791b..885064f 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 6f65d22..78c94e9 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 bc51a80..00dc724 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 b0a53c1..5f4e79d 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 c77f5ca..03f4383 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 -- GitLab