From 86bd6a4716ebae737fbba301ee075b34566f8d8d Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cyril.danilevski@xfel.eu>
Date: Mon, 17 Feb 2025 18:52:03 +0100
Subject: [PATCH] Resend commands on power sequence when no feedback after N
 polls

---
 mpod.cpp | 23 ++++++++++++++++++++++-
 mpod.hpp |  1 -
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/mpod.cpp b/mpod.cpp
index 86030b7..1190c9c 100644
--- a/mpod.cpp
+++ b/mpod.cpp
@@ -221,13 +221,34 @@ void setChannelStateAndWait(const IPAddress *ipAddr, const uint16_t channel, con
     snmp.loop();
 
     // Poll channel until it's settled
+    uint8_t loopCount = 0;
+    bool ramping = true;
+    bool settingChannelState = true;
     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());
+        loopCount += 1;
+        ramping = (mpod.isRampingUp() || mpod.isRampingDown());
+        if (!ramping) {
+            if (mpod.isOn() == (bool)output) {
+                settingChannelState = false;
+            } else if (loopCount >= 5) {
+                // Resend set command
+                Serial.print("!Resend command to ");
+                Serial.println(channel);
+                SNMP::Message *snmp_msg = mpod.setChannelState(channel, output);
+                snmp.send(snmp_msg, *ipAddr, SNMP::Port::SNMP);
+                delete snmp_msg;
+                delay(MPOD_UPDATE_LATENCY);
+                snmp.loop();
+                loopCount = 0;
+            }
+        }
+    } while (settingChannelState);
 }
 
 void setChannelVoltageAndWait(const IPAddress *ipAddr, const uint16_t channel,
diff --git a/mpod.hpp b/mpod.hpp
index 639d993..8ecd78f 100644
--- a/mpod.hpp
+++ b/mpod.hpp
@@ -59,7 +59,6 @@ class MPOD {
             }
         };
     
-        // Create an SNMP SETREQUEST message to setup MPOD
         MPOD();  
         SNMP::Message* read(const uint16_t);
         SNMP::Message* setChannelState(const uint16_t, const bool);
-- 
GitLab