Explorar o código

configurable flush interval

Chris Mullins %!s(int64=8) %!d(string=hai) anos
pai
achega
7a2ee3e13c
Modificáronse 3 ficheiros con 16 adicións e 2 borrados
  1. 2 0
      lib/Settings/Settings.cpp
  2. 3 1
      lib/Settings/Settings.h
  3. 11 1
      src/main.cpp

+ 2 - 0
lib/Settings/Settings.cpp

@@ -80,6 +80,7 @@ void Settings::patch(JsonObject& parsedSettings) {
     this->setIfPresent(parsedSettings, "mqtt_state_topic_pattern", mqttStateTopicPattern);
     this->setIfPresent(parsedSettings, "discovery_port", discoveryPort);
     this->setIfPresent(parsedSettings, "listen_repeats", listenRepeats);
+    this->setIfPresent(parsedSettings, "state_flush_interval", stateFlushInterval);
 
     if (parsedSettings.containsKey("radio_interface_type")) {
       this->radioInterfaceType = Settings::typeFromString(parsedSettings["radio_interface_type"]);
@@ -147,6 +148,7 @@ void Settings::serialize(Stream& stream, const bool prettyPrint) {
   root["mqtt_state_topic_pattern"] = this->mqttStateTopicPattern;
   root["discovery_port"] = this->discoveryPort;
   root["listen_repeats"] = this->listenRepeats;
+  root["state_flush_interval"] = this->stateFlushInterval;
 
   if (this->deviceIds) {
     JsonArray& arr = jsonBuffer.createArray();

+ 3 - 1
lib/Settings/Settings.h

@@ -68,7 +68,8 @@ public:
     httpRepeatFactor(5),
     listenRepeats(3),
     _autoRestartPeriod(0),
-    discoveryPort(48899)
+    discoveryPort(48899),
+    stateFlushInterval(10)
   { }
 
   ~Settings() {
@@ -116,6 +117,7 @@ public:
   String mqttStateTopicPattern;
   uint16_t discoveryPort;
   uint8_t listenRepeats;
+  size_t stateFlushInterval;
 
 protected:
   size_t _autoRestartPeriod;

+ 11 - 1
src/main.cpp

@@ -30,6 +30,7 @@ MqttClient* mqttClient = NULL;
 MiLightDiscoveryServer* discoveryServer = NULL;
 uint8_t currentRadioType = 0;
 GroupStateStore stateStore(MILIGHT_MAX_STATE_ITEMS);
+size_t lastFlush = 0;
 
 int numUdpServers = 0;
 MiLightUdpServer** udpServers;
@@ -176,6 +177,11 @@ bool shouldRestart() {
   return settings.getAutoRestartPeriod()*60*1000 < millis();
 }
 
+bool shouldFlush() {
+  return ((lastFlush + (settings.stateFlushInterval*1000) < millis())
+    || lastFlush > millis()); // in case millis() wraps
+}
+
 void setup() {
   Serial.begin(9600);
   wifiManager.autoConnect();
@@ -223,7 +229,11 @@ void loop() {
   }
 
   handleListen();
-  stateStore.flush();
+
+  if (shouldFlush()) {
+    stateStore.flush();
+    lastFlush = millis();
+  }
 
   if (shouldRestart()) {
     Serial.println(F("Auto-restart triggered. Restarting..."));