Skip to content
Snippets Groups Projects
Commit 979a275c authored by Cyril Danilevski's avatar Cyril Danilevski
Browse files

WIP: poll snmp over rest

parent 365a0f0a
No related branches found
No related tags found
1 merge request!2Initial MPOD feature
Pipeline #131062 passed
...@@ -12,9 +12,7 @@ void setup() { ...@@ -12,9 +12,7 @@ void setup() {
initializeNetwork(); initializeNetwork();
initializeRoutes(); initializeRoutes();
Serial.println("Started REST server");
initializeSNMP(); initializeSNMP();
Serial.println("Started SNMP server");
start = millis(); start = millis();
} }
......
...@@ -146,6 +146,21 @@ float MPOD::getFloatFromVarBind(const VarBind *varbind) { ...@@ -146,6 +146,21 @@ float MPOD::getFloatFromVarBind(const VarBind *varbind) {
return static_cast<OpaqueFloatBER*>(static_cast<OpaqueBER*>(varbind->getValue())->getBER())->getValue(); 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; WiFiUDP udp;
SNMP::Manager snmp; SNMP::Manager snmp;
...@@ -183,4 +198,5 @@ void onMessage(const SNMP::Message *message, const IPAddress remote, const uint1 ...@@ -183,4 +198,5 @@ void onMessage(const SNMP::Message *message, const IPAddress remote, const uint1
void initializeSNMP() { void initializeSNMP() {
snmp.begin(&udp); snmp.begin(&udp);
snmp.onMessage(onMessage); snmp.onMessage(onMessage);
Serial.println("SNMP Server Started");
} }
...@@ -62,6 +62,7 @@ class MPOD { ...@@ -62,6 +62,7 @@ class MPOD {
float getVoltage() const; float getVoltage() const;
float getCurrent() const; float getCurrent() const;
float getVoltageRiseRate() const; float getVoltageRiseRate() const;
String toJSON();
private: private:
unsigned int getIntegerFromVarBind(const VarBind*); unsigned int getIntegerFromVarBind(const VarBind*);
......
...@@ -17,6 +17,7 @@ void initializeRoutes() { ...@@ -17,6 +17,7 @@ void initializeRoutes() {
restServer.on("/restart", restart); restServer.on("/restart", restart);
restServer.onNotFound(notFound); restServer.onNotFound(notFound);
restServer.on("/send", sendSNMP); restServer.on("/send", sendSNMP);
restServer.on("/poll", pollMPODChannel);
restServer.begin(); restServer.begin();
Serial.println("REST Server Started"); Serial.println("REST Server Started");
...@@ -102,18 +103,29 @@ void sendSNMP() { ...@@ -102,18 +103,29 @@ void sendSNMP() {
} }
} }
auto ip_addr = IPAddress(10, 42, 0 ,1); auto ipAddr = IPAddress(10, 42, 0 ,1);
if (output != NONE) { if (output != NONE) {
SNMP::Message *snmp_msg = mpod.output(output); 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; delete snmp_msg;
success = true; success = true;
} }
String http_msg = "{\n"; String http_msg = "{\n";
http_msg += "\"target\":\"" + String(ip_addr) + "\","; http_msg += "\"target\":\"" + ipAddr.toString() + "\",";
http_msg += "\"arguments\":{\"output\":\"" + String(output) + "\"},\n"; http_msg += "\"arguments\":{\"output\":" + String(output) + "},\n";
http_msg += "\"success\":\"" + String(success) + "\""; 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}"; http_msg += "\n}";
restServer.send(200, "text/json", http_msg); restServer.send(200, "text/json", http_msg);
......
...@@ -9,3 +9,4 @@ void identify(); // /idn ...@@ -9,3 +9,4 @@ void identify(); // /idn
void notFound(); // 404 void notFound(); // 404
void restart(); // /restart void restart(); // /restart
void sendSNMP(); // /send?output=on void sendSNMP(); // /send?output=on
void pollMPODChannel(); //mock the hardcoded mpod channel 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment