GroupState.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. #include <MiLightRemoteConfig.h>
  4. #include <RGBConverter.h>
  5. const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
  6. static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
  7. GroupState& state = instances[remoteType];
  8. switch (remoteType) {
  9. case REMOTE_TYPE_RGB:
  10. state.setBulbMode(BULB_MODE_COLOR);
  11. break;
  12. case REMOTE_TYPE_CCT:
  13. state.setBulbMode(BULB_MODE_WHITE);
  14. break;
  15. }
  16. return state;
  17. }
  18. GroupId::GroupId()
  19. : deviceId(0),
  20. groupId(0),
  21. deviceType(REMOTE_TYPE_UNKNOWN)
  22. { }
  23. GroupId::GroupId(const GroupId &other)
  24. : deviceId(other.deviceId),
  25. groupId(other.groupId),
  26. deviceType(other.deviceType)
  27. { }
  28. GroupId::GroupId(
  29. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  30. )
  31. : deviceId(deviceId),
  32. groupId(groupId),
  33. deviceType(deviceType)
  34. { }
  35. void GroupId::operator=(const GroupId &other) {
  36. deviceId = other.deviceId;
  37. groupId = other.groupId;
  38. deviceType = other.deviceType;
  39. }
  40. bool GroupId::operator==(const GroupId &other) {
  41. return deviceId == other.deviceId
  42. && groupId == other.groupId
  43. && deviceType == other.deviceType;
  44. }
  45. GroupState::GroupState() {
  46. state.fields._state = 0;
  47. state.fields._brightness = 0;
  48. state.fields._brightnessColor = 0;
  49. state.fields._brightnessMode = 0;
  50. state.fields._hue = 0;
  51. state.fields._saturation = 0;
  52. state.fields._mode = 0;
  53. state.fields._bulbMode = 0;
  54. state.fields._kelvin = 0;
  55. state.fields._isSetState = 0;
  56. state.fields._isSetHue = 0;
  57. state.fields._isSetBrightness = 0;
  58. state.fields._isSetBrightnessColor = 0;
  59. state.fields._isSetBrightnessMode = 0;
  60. state.fields._isSetSaturation = 0;
  61. state.fields._isSetMode = 0;
  62. state.fields._isSetKelvin = 0;
  63. state.fields._isSetBulbMode = 0;
  64. state.fields._dirty = 1;
  65. }
  66. bool GroupState::isSetState() const { return state.fields._isSetState; }
  67. MiLightStatus GroupState::getState() const { return state.fields._state ? ON : OFF; }
  68. bool GroupState::setState(const MiLightStatus status) {
  69. if (isSetState() && getState() == status) {
  70. return false;
  71. }
  72. setDirty();
  73. state.fields._isSetState = 1;
  74. state.fields._state = status == ON ? 1 : 0;
  75. return true;
  76. }
  77. bool GroupState::isSetBrightness() const {
  78. if (! state.fields._isSetBulbMode) {
  79. return state.fields._isSetBrightness;
  80. }
  81. switch (state.fields._bulbMode) {
  82. case BULB_MODE_WHITE:
  83. return state.fields._isSetBrightness;
  84. case BULB_MODE_COLOR:
  85. return state.fields._isSetBrightnessColor;
  86. case BULB_MODE_SCENE:
  87. return state.fields._isSetBrightnessMode;
  88. }
  89. return false;
  90. }
  91. uint8_t GroupState::getBrightness() const {
  92. switch (state.fields._bulbMode) {
  93. case BULB_MODE_WHITE:
  94. return state.fields._brightness;
  95. case BULB_MODE_COLOR:
  96. return state.fields._brightnessColor;
  97. case BULB_MODE_SCENE:
  98. return state.fields._brightnessMode;
  99. }
  100. return 0;
  101. }
  102. bool GroupState::setBrightness(uint8_t brightness) {
  103. if (isSetBrightness() && getBrightness() == brightness) {
  104. return false;
  105. }
  106. setDirty();
  107. uint8_t bulbMode = state.fields._bulbMode;
  108. if (! state.fields._isSetBulbMode) {
  109. bulbMode = BULB_MODE_WHITE;
  110. }
  111. switch (bulbMode) {
  112. case BULB_MODE_WHITE:
  113. state.fields._isSetBrightness = 1;
  114. state.fields._brightness = brightness;
  115. break;
  116. case BULB_MODE_COLOR:
  117. state.fields._isSetBrightnessColor = 1;
  118. state.fields._brightnessColor = brightness;
  119. break;
  120. case BULB_MODE_SCENE:
  121. state.fields._isSetBrightnessMode = 1;
  122. state.fields._brightnessMode = brightness;
  123. default:
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool GroupState::isSetHue() const { return state.fields._isSetHue; }
  129. uint16_t GroupState::getHue() const {
  130. return Units::rescale<uint16_t, uint16_t>(state.fields._hue, 360, 255);
  131. }
  132. bool GroupState::setHue(uint16_t hue) {
  133. if (isSetHue() && getHue() == hue) {
  134. return false;
  135. }
  136. setDirty();
  137. state.fields._isSetHue = 1;
  138. state.fields._hue = Units::rescale<uint16_t, uint16_t>(hue, 255, 360);
  139. return true;
  140. }
  141. bool GroupState::isSetSaturation() const { return state.fields._isSetSaturation; }
  142. uint8_t GroupState::getSaturation() const { return state.fields._saturation; }
  143. bool GroupState::setSaturation(uint8_t saturation) {
  144. if (isSetSaturation() && getSaturation() == saturation) {
  145. return false;
  146. }
  147. setDirty();
  148. state.fields._isSetSaturation = 1;
  149. state.fields._saturation = saturation;
  150. return true;
  151. }
  152. bool GroupState::isSetMode() const { return state.fields._isSetMode; }
  153. uint8_t GroupState::getMode() const { return state.fields._mode; }
  154. bool GroupState::setMode(uint8_t mode) {
  155. if (isSetMode() && getMode() == mode) {
  156. return false;
  157. }
  158. setDirty();
  159. state.fields._isSetMode = 1;
  160. state.fields._mode = mode;
  161. return true;
  162. }
  163. bool GroupState::isSetKelvin() const { return state.fields._isSetKelvin; }
  164. uint8_t GroupState::getKelvin() const { return state.fields._kelvin; }
  165. uint16_t GroupState::getMireds() const {
  166. return Units::whiteValToMireds(getKelvin(), 100);
  167. }
  168. bool GroupState::setKelvin(uint8_t kelvin) {
  169. if (isSetKelvin() && getKelvin() == kelvin) {
  170. return false;
  171. }
  172. setDirty();
  173. state.fields._isSetKelvin = 1;
  174. state.fields._kelvin = kelvin;
  175. return true;
  176. }
  177. bool GroupState::setMireds(uint16_t mireds) {
  178. return setKelvin(Units::miredsToWhiteVal(mireds, 100));
  179. }
  180. bool GroupState::isSetBulbMode() const { return state.fields._isSetBulbMode; }
  181. BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(state.fields._bulbMode); }
  182. bool GroupState::setBulbMode(BulbMode bulbMode) {
  183. if (isSetBulbMode() && getBulbMode() == bulbMode) {
  184. return false;
  185. }
  186. setDirty();
  187. state.fields._isSetBulbMode = 1;
  188. state.fields._bulbMode = bulbMode;
  189. return true;
  190. }
  191. bool GroupState::isDirty() const { return state.fields._dirty; }
  192. inline bool GroupState::setDirty() { state.fields._dirty = 1; }
  193. bool GroupState::clearDirty() { state.fields._dirty = 0; }
  194. void GroupState::load(Stream& stream) {
  195. for (size_t i = 0; i < DATA_BYTES; i++) {
  196. stream.readBytes(reinterpret_cast<uint8_t*>(&state.data[i]), 4);
  197. }
  198. clearDirty();
  199. }
  200. void GroupState::dump(Stream& stream) const {
  201. for (size_t i = 0; i < DATA_BYTES; i++) {
  202. stream.write(reinterpret_cast<const uint8_t*>(&state.data[i]), 4);
  203. }
  204. }
  205. bool GroupState::patch(const JsonObject& state) {
  206. bool changes = false;
  207. if (state.containsKey("state")) {
  208. changes |= setState(state["state"] == "ON" ? ON : OFF);
  209. }
  210. if (state.containsKey("brightness")) {
  211. changes |= setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  212. }
  213. if (state.containsKey("hue")) {
  214. changes |= setHue(state["hue"]);
  215. changes |= setBulbMode(BULB_MODE_COLOR);
  216. }
  217. if (state.containsKey("saturation")) {
  218. changes |= setSaturation(state["saturation"]);
  219. }
  220. if (state.containsKey("mode")) {
  221. changes |= setMode(state["mode"]);
  222. changes |= setBulbMode(BULB_MODE_SCENE);
  223. }
  224. if (state.containsKey("color_temp")) {
  225. changes |= setMireds(state["color_temp"]);
  226. changes |= setBulbMode(BULB_MODE_WHITE);
  227. }
  228. if (state.containsKey("command")) {
  229. const String& command = state["command"];
  230. if (command == "white_mode") {
  231. changes |= setBulbMode(BULB_MODE_WHITE);
  232. } else if (command == "night_mode") {
  233. changes |= setBulbMode(BULB_MODE_NIGHT);
  234. }
  235. }
  236. return changes;
  237. }
  238. void GroupState::applyState(JsonObject& partialState) {
  239. if (isSetState()) {
  240. partialState["state"] = getState() == ON ? "ON" : "OFF";
  241. }
  242. if (isSetBrightness()) {
  243. partialState["brightness"] = Units::rescale(getBrightness(), 255, 100);
  244. }
  245. if (isSetBulbMode()) {
  246. partialState["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  247. if (getBulbMode() == BULB_MODE_COLOR) {
  248. if (isSetHue() && isSetSaturation()) {
  249. uint8_t rgb[3];
  250. RGBConverter converter;
  251. converter.hsvToRgb(getHue()/360.0, getSaturation()/100.0, 1, rgb);
  252. JsonObject& color = partialState.createNestedObject("color");
  253. color["r"] = rgb[0];
  254. color["g"] = rgb[1];
  255. color["b"] = rgb[2];
  256. } else if (isSetHue()) {
  257. partialState["hue"] = getHue();
  258. } else if (isSetSaturation()) {
  259. partialState["saturation"] = getSaturation();
  260. }
  261. } else if (getBulbMode() == BULB_MODE_SCENE) {
  262. if (isSetMode()) {
  263. partialState["mode"] = getMode();
  264. }
  265. } else if (getBulbMode() == BULB_MODE_WHITE) {
  266. if (isSetKelvin()) {
  267. partialState["color_temp"] = getMireds();
  268. }
  269. }
  270. }
  271. }