Selaa lähdekoodia

always evaluate on first and off last

Chris Mullins 8 vuotta sitten
vanhempi
commit
e3e868368d
2 muutettua tiedostoa jossa 24 lisäystä ja 10 poistoa
  1. 23 10
      lib/MiLight/MiLightClient.cpp
  2. 1 0
      lib/MiLight/MiLightClient.h

+ 23 - 10
lib/MiLight/MiLightClient.cpp

@@ -222,17 +222,11 @@ void MiLightClient::command(uint8_t command, uint8_t arg) {
 }
 
 void MiLightClient::update(const JsonObject& request) {
-  if (request.containsKey("status") || request.containsKey("state")) {
-    String strStatus;
+  const uint8_t parsedStatus = this->parseStatus(request);
 
-    if (request.containsKey("status")) {
-      strStatus = request.get<char*>("status");
-    } else {
-      strStatus = request.get<char*>("state");
-    }
-
-    MiLightStatus status = (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
-    this->updateStatus(status);
+  // Always turn on first
+  if (parsedStatus == ON) {
+    this->updateStatus(ON);
   }
 
   if (request.containsKey("command")) {
@@ -308,6 +302,11 @@ void MiLightClient::update(const JsonObject& request) {
   if (request.containsKey("mode")) {
     this->updateMode(request["mode"]);
   }
+
+  // Always turn off last
+  if (parsedStatus == OFF) {
+    this->updateStatus(OFF);
+  }
 }
 
 void MiLightClient::handleCommand(const String& command) {
@@ -338,6 +337,20 @@ void MiLightClient::handleCommand(const String& command) {
   }
 }
 
+uint8_t MiLightClient::parseStatus(const JsonObject& object) {
+  String strStatus;
+
+  if (object.containsKey("status")) {
+    strStatus = object.get<char*>("status");
+  } else if (object.containsKey("state")) {
+    strStatus = object.get<char*>("state");
+  } else {
+    return 255;
+  }
+
+  return (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
+}
+
 void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
   formatter->format(packet, buffer);
 }

+ 1 - 0
lib/MiLight/MiLightClient.h

@@ -72,6 +72,7 @@ protected:
   unsigned int resendCount;
 
   MiLightRadio* switchRadio(const MiLightRadioType type);
+  uint8_t parseStatus(const JsonObject& object);
 
   void flushPacket();
 };