Переглянути джерело

methods to patch/construct JSON

Chris Mullins 8 роки тому
батько
коміт
a54b87f768
2 змінених файлів з 112 додано та 17 видалено
  1. 95 11
      lib/MiLightState/GroupState.cpp
  2. 17 6
      lib/MiLightState/GroupState.h

+ 95 - 11
lib/MiLightState/GroupState.cpp

@@ -1,4 +1,5 @@
 #include <GroupState.h>
+#include <Units.h>
 
 const GroupState& GroupState::defaultState() {
   static GroupState instance;
@@ -38,14 +39,14 @@ bool GroupId::operator==(const GroupId &other) {
 }
 
 GroupState::GroupState() {
-  _on              = 0;
+  _state           = 0;
   _brightness      = 0;
   _hue             = 0;
   _saturation      = 0;
   _mode            = 0;
   _bulbMode        = 0;
   _kelvin          = 0;
-  _isSetOn         = 0;
+  _isSetState      = 0;
   _isSetHue        = 0;
   _isSetBrightness = 0;
   _isSetSaturation = 0;
@@ -54,30 +55,113 @@ GroupState::GroupState() {
   _isSetBulbMode   = 0;
 }
 
-bool GroupState::isSetOn() const { return _isSetOn; }
-bool GroupState::isOn() const { return _on; }
-void GroupState::setOn(bool on) { _on = on; }
+bool GroupState::isSetState() const { return _isSetState; }
+MiLightStatus GroupState::getState() const { return _state ? ON : OFF; }
+void GroupState::setState(const MiLightStatus& state) {
+  _isSetState = 1;
+  _state = state == ON ? 1 : 0;
+}
 
 bool GroupState::isSetBrightness() const { return _isSetBrightness; }
 uint8_t GroupState::getBrightness() const { return _brightness; }
-void GroupState::setBrightness(uint8_t brightness) { _brightness = brightness; }
+void GroupState::setBrightness(uint8_t brightness) {
+  _isSetBrightness = 1;
+  _brightness = brightness;
+}
 
 bool GroupState::isSetHue() const { return _isSetHue; }
 uint8_t GroupState::getHue() const { return _hue; }
-void GroupState::setHue(uint8_t hue) { _hue = hue; }
+void GroupState::setHue(uint8_t hue) {
+  _isSetHue = 1;
+  _hue = hue;
+}
 
 bool GroupState::isSetSaturation() const { return _isSetSaturation; }
 uint8_t GroupState::getSaturation() const { return _saturation; }
-void GroupState::setSaturation(uint8_t saturation) { _saturation = saturation; }
+void GroupState::setSaturation(uint8_t saturation) {
+  _isSetSaturation = 1;
+  _saturation = saturation;
+}
 
 bool GroupState::isSetMode() const { return _isSetMode; }
 uint8_t GroupState::getMode() const { return _mode; }
-void GroupState::setMode(uint8_t mode) { _mode = mode; }
+void GroupState::setMode(uint8_t mode) {
+  _isSetMode = 1;
+  _mode = mode;
+}
 
 bool GroupState::isSetKelvin() const { return _isSetKelvin; }
 uint8_t GroupState::getKelvin() const { return _kelvin; }
-void GroupState::setKelvin(uint8_t kelvin) { _kelvin = kelvin; }
+void GroupState::setKelvin(uint8_t kelvin) {
+  _isSetKelvin = 1;
+  _kelvin = kelvin;
+}
 
 bool GroupState::isSetBulbMode() const { return _isSetBulbMode; }
 BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(_bulbMode); }
-void GroupState::setBulbMode(BulbMode bulbMode) { _bulbMode = bulbMode; }
+void GroupState::setBulbMode(BulbMode bulbMode) {
+  _isSetBulbMode = 1;
+  _bulbMode = bulbMode;
+}
+
+void GroupState::patch(const JsonObject& state) {
+  if (state.containsKey("state")) {
+    setState(state["state"] == "ON" ? ON : OFF);
+  }
+  if (state.containsKey("brightness")) {
+    setBrightness(Units::rescale(state["brightness"], 100, 255));
+  }
+  if (state.containsKey("hue")) {
+    setHue(Units::rescale<uint8_t, uint16_t>(state["hue"], 255, 360));
+    setBulbMode(BULB_MODE_COLOR);
+  }
+  if (state.containsKey("saturation")) {
+    setSaturation(state["saturation"]);
+  }
+  if (state.containsKey("mode")) {
+    setMode(state["mode"]);
+    setBulbMode(BULB_MODE_SCENE);
+  }
+  if (state.containsKey("color_temp")) {
+    setKelvin(Units::miredsToWhiteVal(state["color_temp"], 100));
+    setBulbMode(BULB_MODE_WHITE);
+  }
+  if (state.containsKey("command")) {
+    const String& command = state["command"];
+
+    if (command == "white_mode") {
+      setBulbMode(BULB_MODE_WHITE);
+    } else if (command == "night_mode") {
+      setBulbMode(BULB_MODE_NIGHT);
+    }
+  }
+}
+
+void GroupState::applyState(JsonObject& state) {
+  if (_isSetState) {
+    state["state"] = getState() == ON ? "ON" : "OFF";
+  }
+  if (_isSetBrightness) {
+    state["brightness"] = Units::rescale(getBrightness(), 255, 100);
+  }
+  if (_isSetBulbMode) {
+    state["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
+
+    if (getBulbMode() == BULB_MODE_COLOR) {
+      if (_isSetHue) {
+        state["hue"] = Units::rescale<uint8_t, uint16_t>(getHue(), 360, 255);
+      }
+      if (_isSetSaturation) {
+        state["saturation"] = getSaturation();
+      }
+    } else if (getBulbMode() == BULB_MODE_SCENE) {
+      if (_isSetMode) {
+        state["mode"] = getMode();
+      }
+    } else if (getBulbMode() == BULB_MODE_WHITE) {
+      if (_isSetKelvin) {
+        state["color_temp"] = Units::whiteValToMireds(getKelvin(), 100);
+      }
+    }
+  }
+}

+ 17 - 6
lib/MiLightState/GroupState.h

@@ -1,6 +1,7 @@
 #include <inttypes.h>
 #include <Arduino.h>
 #include <MiLightButtons.h>
+#include <ArduinoJson.h>
 
 #ifndef _GROUP_STATE_H
 #define _GROUP_STATE_H
@@ -20,7 +21,14 @@ struct GroupId {
 enum BulbMode {
   BULB_MODE_WHITE,
   BULB_MODE_COLOR,
-  BULB_MODE_SCENE
+  BULB_MODE_SCENE,
+  BULB_MODE_NIGHT
+};
+static const char* BULB_MODE_NAMES[] = {
+  "white",
+  "color",
+  "scene",
+  "night"
 };
 
 class GroupState {
@@ -28,9 +36,9 @@ public:
   GroupState();
 
   // 1 bit
-  bool isSetOn() const;
-  bool isOn() const;
-  void setOn(bool on);
+  bool isSetState() const;
+  MiLightStatus getState() const;
+  void setState(const MiLightStatus& on);
 
   // 7 bits
   bool isSetBrightness() const;
@@ -62,17 +70,20 @@ public:
   BulbMode getBulbMode() const;
   void setBulbMode(BulbMode mode);
 
+  void patch(const JsonObject& state);
+  void applyState(JsonObject& state);
+
   static const GroupState& defaultState();
 
 private:
   uint32_t
-    _on         : 1,
+    _state      : 1,
     _brightness : 7,
     _hue        : 8,
     _saturation : 7,
     _mode       : 4,
     _bulbMode   : 3,
-    _isSetOn    : 1,
+    _isSetState : 1,
     _isSetHue   : 1;
 
   uint16_t