GroupState.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. }
  64. bool GroupState::isSetState() const { return _isSetState; }
  65. MiLightStatus GroupState::getState() const { return _state ? ON : OFF; }
  66. void GroupState::setState(const MiLightStatus state) {
  67. _isSetState = 1;
  68. _state = state == ON ? 1 : 0;
  69. }
  70. bool GroupState::isSetBrightness() const {
  71. if (! _isSetBulbMode) {
  72. return _isSetBrightness;
  73. }
  74. switch (_bulbMode) {
  75. case BULB_MODE_WHITE:
  76. return _isSetBrightness;
  77. case BULB_MODE_COLOR:
  78. return _isSetBrightnessColor;
  79. case BULB_MODE_SCENE:
  80. return _isSetBrightnessMode;
  81. }
  82. return false;
  83. }
  84. uint8_t GroupState::getBrightness() const {
  85. switch (_bulbMode) {
  86. case BULB_MODE_WHITE:
  87. return _brightness;
  88. case BULB_MODE_COLOR:
  89. return _brightnessColor;
  90. case BULB_MODE_SCENE:
  91. return _brightnessMode;
  92. }
  93. return 0;
  94. }
  95. void GroupState::setBrightness(uint8_t brightness) {
  96. uint8_t bulbMode = _bulbMode;
  97. if (! _isSetBulbMode) {
  98. bulbMode = BULB_MODE_WHITE;
  99. }
  100. switch (bulbMode) {
  101. case BULB_MODE_WHITE:
  102. _isSetBrightness = 1;
  103. _brightness = brightness;
  104. break;
  105. case BULB_MODE_COLOR:
  106. _isSetBrightnessColor = 1;
  107. _brightnessColor = brightness;
  108. break;
  109. case BULB_MODE_SCENE:
  110. _isSetBrightnessMode = 1;
  111. _brightnessMode = brightness;
  112. }
  113. }
  114. bool GroupState::isSetHue() const { return _isSetHue; }
  115. uint8_t GroupState::getHue() const { return _hue; }
  116. void GroupState::setHue(uint8_t hue) {
  117. _isSetHue = 1;
  118. _hue = hue;
  119. }
  120. bool GroupState::isSetSaturation() const { return _isSetSaturation; }
  121. uint8_t GroupState::getSaturation() const { return _saturation; }
  122. void GroupState::setSaturation(uint8_t saturation) {
  123. _isSetSaturation = 1;
  124. _saturation = saturation;
  125. }
  126. bool GroupState::isSetMode() const { return _isSetMode; }
  127. uint8_t GroupState::getMode() const { return _mode; }
  128. void GroupState::setMode(uint8_t mode) {
  129. _isSetMode = 1;
  130. _mode = mode;
  131. }
  132. bool GroupState::isSetKelvin() const { return _isSetKelvin; }
  133. uint8_t GroupState::getKelvin() const { return _kelvin; }
  134. void GroupState::setKelvin(uint8_t kelvin) {
  135. _isSetKelvin = 1;
  136. _kelvin = kelvin;
  137. }
  138. bool GroupState::isSetBulbMode() const { return _isSetBulbMode; }
  139. BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(_bulbMode); }
  140. void GroupState::setBulbMode(BulbMode bulbMode) {
  141. _isSetBulbMode = 1;
  142. _bulbMode = bulbMode;
  143. }
  144. void GroupState::patch(const JsonObject& state) {
  145. if (state.containsKey("state")) {
  146. setState(state["state"] == "ON" ? ON : OFF);
  147. }
  148. if (state.containsKey("brightness")) {
  149. setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  150. }
  151. if (state.containsKey("hue")) {
  152. setHue(Units::rescale<uint8_t, uint16_t>(state["hue"], 255, 360));
  153. setBulbMode(BULB_MODE_COLOR);
  154. }
  155. if (state.containsKey("saturation")) {
  156. setSaturation(state["saturation"]);
  157. }
  158. if (state.containsKey("mode")) {
  159. setMode(state["mode"]);
  160. setBulbMode(BULB_MODE_SCENE);
  161. }
  162. if (state.containsKey("color_temp")) {
  163. setKelvin(Units::miredsToWhiteVal(state["color_temp"], 100));
  164. setBulbMode(BULB_MODE_WHITE);
  165. }
  166. if (state.containsKey("command")) {
  167. const String& command = state["command"];
  168. if (command == "white_mode") {
  169. setBulbMode(BULB_MODE_WHITE);
  170. } else if (command == "night_mode") {
  171. setBulbMode(BULB_MODE_NIGHT);
  172. }
  173. }
  174. }
  175. void GroupState::applyState(JsonObject& state) {
  176. if (_isSetState) {
  177. state["state"] = getState() == ON ? "ON" : "OFF";
  178. }
  179. if (_isSetBrightness) {
  180. state["brightness"] = Units::rescale(getBrightness(), 255, 100);
  181. }
  182. if (_isSetBulbMode) {
  183. state["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  184. if (getBulbMode() == BULB_MODE_COLOR) {
  185. if (_isSetHue) {
  186. state["hue"] = Units::rescale<uint8_t, uint16_t>(getHue(), 360, 255);
  187. }
  188. if (_isSetSaturation) {
  189. state["saturation"] = getSaturation();
  190. }
  191. } else if (getBulbMode() == BULB_MODE_SCENE) {
  192. if (_isSetMode) {
  193. state["mode"] = getMode();
  194. }
  195. } else if (getBulbMode() == BULB_MODE_WHITE) {
  196. if (_isSetKelvin) {
  197. state["color_temp"] = Units::whiteValToMireds(getKelvin(), 100);
  198. }
  199. }
  200. }
  201. }