GroupState.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. #include <MiLightRemoteConfig.h>
  4. const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
  5. static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
  6. GroupState& state = instances[remoteType];
  7. switch (remoteType) {
  8. case REMOTE_TYPE_RGB:
  9. state.setBulbMode(BULB_MODE_COLOR);
  10. break;
  11. case REMOTE_TYPE_CCT:
  12. state.setBulbMode(BULB_MODE_WHITE);
  13. break;
  14. }
  15. return state;
  16. }
  17. GroupId::GroupId()
  18. : deviceId(0),
  19. groupId(0),
  20. deviceType(REMOTE_TYPE_UNKNOWN)
  21. { }
  22. GroupId::GroupId(const GroupId &other)
  23. : deviceId(other.deviceId),
  24. groupId(other.groupId),
  25. deviceType(other.deviceType)
  26. { }
  27. GroupId::GroupId(
  28. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  29. )
  30. : deviceId(deviceId),
  31. groupId(groupId),
  32. deviceType(deviceType)
  33. { }
  34. void GroupId::operator=(const GroupId &other) {
  35. deviceId = other.deviceId;
  36. groupId = other.groupId;
  37. deviceType = other.deviceType;
  38. }
  39. bool GroupId::operator==(const GroupId &other) {
  40. return deviceId == other.deviceId
  41. && groupId == other.groupId
  42. && deviceType == other.deviceType;
  43. }
  44. GroupState::GroupState() {
  45. _state = 0;
  46. _brightness = 0;
  47. _brightnessColor = 0;
  48. _brightnessMode = 0;
  49. _hue = 0;
  50. _saturation = 0;
  51. _mode = 0;
  52. _bulbMode = 0;
  53. _kelvin = 0;
  54. _isSetState = 0;
  55. _isSetHue = 0;
  56. _isSetBrightness = 0;
  57. _isSetBrightnessColor = 0;
  58. _isSetBrightnessMode = 0;
  59. _isSetSaturation = 0;
  60. _isSetMode = 0;
  61. _isSetKelvin = 0;
  62. _isSetBulbMode = 0;
  63. _dirty = 1;
  64. }
  65. bool GroupState::isSetState() const { return _isSetState; }
  66. MiLightStatus GroupState::getState() const { return _state ? ON : OFF; }
  67. void GroupState::setState(const MiLightStatus state) {
  68. _isSetState = 1;
  69. _state = state == ON ? 1 : 0;
  70. }
  71. bool GroupState::isSetBrightness() const {
  72. if (! _isSetBulbMode) {
  73. return _isSetBrightness;
  74. }
  75. switch (_bulbMode) {
  76. case BULB_MODE_WHITE:
  77. return _isSetBrightness;
  78. case BULB_MODE_COLOR:
  79. return _isSetBrightnessColor;
  80. case BULB_MODE_SCENE:
  81. return _isSetBrightnessMode;
  82. }
  83. return false;
  84. }
  85. uint8_t GroupState::getBrightness() const {
  86. switch (_bulbMode) {
  87. case BULB_MODE_WHITE:
  88. return _brightness;
  89. case BULB_MODE_COLOR:
  90. return _brightnessColor;
  91. case BULB_MODE_SCENE:
  92. return _brightnessMode;
  93. }
  94. return 0;
  95. }
  96. void GroupState::setBrightness(uint8_t brightness) {
  97. uint8_t bulbMode = _bulbMode;
  98. if (! _isSetBulbMode) {
  99. bulbMode = BULB_MODE_WHITE;
  100. }
  101. switch (bulbMode) {
  102. case BULB_MODE_WHITE:
  103. _isSetBrightness = 1;
  104. _brightness = brightness;
  105. break;
  106. case BULB_MODE_COLOR:
  107. _isSetBrightnessColor = 1;
  108. _brightnessColor = brightness;
  109. break;
  110. case BULB_MODE_SCENE:
  111. _isSetBrightnessMode = 1;
  112. _brightnessMode = brightness;
  113. }
  114. }
  115. bool GroupState::isSetHue() const { return _isSetHue; }
  116. uint8_t GroupState::getHue() const { return _hue; }
  117. void GroupState::setHue(uint8_t hue) {
  118. _isSetHue = 1;
  119. _hue = hue;
  120. }
  121. bool GroupState::isSetSaturation() const { return _isSetSaturation; }
  122. uint8_t GroupState::getSaturation() const { return _saturation; }
  123. void GroupState::setSaturation(uint8_t saturation) {
  124. _isSetSaturation = 1;
  125. _saturation = saturation;
  126. }
  127. bool GroupState::isSetMode() const { return _isSetMode; }
  128. uint8_t GroupState::getMode() const { return _mode; }
  129. void GroupState::setMode(uint8_t mode) {
  130. _isSetMode = 1;
  131. _mode = mode;
  132. }
  133. bool GroupState::isSetKelvin() const { return _isSetKelvin; }
  134. uint8_t GroupState::getKelvin() const { return _kelvin; }
  135. void GroupState::setKelvin(uint8_t kelvin) {
  136. _isSetKelvin = 1;
  137. _kelvin = kelvin;
  138. }
  139. bool GroupState::isSetBulbMode() const { return _isSetBulbMode; }
  140. BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(_bulbMode); }
  141. void GroupState::setBulbMode(BulbMode bulbMode) {
  142. _isSetBulbMode = 1;
  143. _bulbMode = bulbMode;
  144. }
  145. bool GroupState::isDirty() const { return _dirty; }
  146. bool GroupState::setDirty() { _dirty = 1; }
  147. bool GroupState::clearDirty() { _dirty = 0; }
  148. void GroupState::patch(const JsonObject& state) {
  149. if (state.containsKey("state")) {
  150. setState(state["state"] == "ON" ? ON : OFF);
  151. }
  152. if (state.containsKey("brightness")) {
  153. setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  154. }
  155. if (state.containsKey("hue")) {
  156. setHue(Units::rescale<uint8_t, uint16_t>(state["hue"], 255, 360));
  157. setBulbMode(BULB_MODE_COLOR);
  158. }
  159. if (state.containsKey("saturation")) {
  160. setSaturation(state["saturation"]);
  161. }
  162. if (state.containsKey("mode")) {
  163. setMode(state["mode"]);
  164. setBulbMode(BULB_MODE_SCENE);
  165. }
  166. if (state.containsKey("color_temp")) {
  167. setKelvin(Units::miredsToWhiteVal(state["color_temp"], 100));
  168. setBulbMode(BULB_MODE_WHITE);
  169. }
  170. if (state.containsKey("command")) {
  171. const String& command = state["command"];
  172. if (command == "white_mode") {
  173. setBulbMode(BULB_MODE_WHITE);
  174. } else if (command == "night_mode") {
  175. setBulbMode(BULB_MODE_NIGHT);
  176. }
  177. }
  178. }
  179. void GroupState::applyState(JsonObject& state) {
  180. if (_isSetState) {
  181. state["state"] = getState() == ON ? "ON" : "OFF";
  182. }
  183. if (_isSetBrightness) {
  184. state["brightness"] = Units::rescale(getBrightness(), 255, 100);
  185. }
  186. if (_isSetBulbMode) {
  187. state["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  188. if (getBulbMode() == BULB_MODE_COLOR) {
  189. if (_isSetHue) {
  190. state["hue"] = Units::rescale<uint8_t, uint16_t>(getHue(), 360, 255);
  191. }
  192. if (_isSetSaturation) {
  193. state["saturation"] = getSaturation();
  194. }
  195. } else if (getBulbMode() == BULB_MODE_SCENE) {
  196. if (_isSetMode) {
  197. state["mode"] = getMode();
  198. }
  199. } else if (getBulbMode() == BULB_MODE_WHITE) {
  200. if (_isSetKelvin) {
  201. state["color_temp"] = Units::whiteValToMireds(getKelvin(), 100);
  202. }
  203. }
  204. }
  205. }