GroupState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. #include <MiLightRemoteConfig.h>
  4. #include <RGBConverter.h>
  5. const BulbId DEFAULT_BULB_ID;
  6. const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
  7. static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
  8. GroupState& state = instances[remoteType];
  9. switch (remoteType) {
  10. case REMOTE_TYPE_RGB:
  11. state.setBulbMode(BULB_MODE_COLOR);
  12. break;
  13. case REMOTE_TYPE_CCT:
  14. state.setBulbMode(BULB_MODE_WHITE);
  15. break;
  16. }
  17. return state;
  18. }
  19. BulbId::BulbId()
  20. : deviceId(0),
  21. groupId(0),
  22. deviceType(REMOTE_TYPE_UNKNOWN)
  23. { }
  24. BulbId::BulbId(const BulbId &other)
  25. : deviceId(other.deviceId),
  26. groupId(other.groupId),
  27. deviceType(other.deviceType)
  28. { }
  29. BulbId::BulbId(
  30. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  31. )
  32. : deviceId(deviceId),
  33. groupId(groupId),
  34. deviceType(deviceType)
  35. { }
  36. void BulbId::operator=(const BulbId &other) {
  37. deviceId = other.deviceId;
  38. groupId = other.groupId;
  39. deviceType = other.deviceType;
  40. }
  41. bool BulbId::operator==(const BulbId &other) {
  42. return deviceId == other.deviceId
  43. && groupId == other.groupId
  44. && deviceType == other.deviceType;
  45. }
  46. GroupState::GroupState() {
  47. state.fields._state = 0;
  48. state.fields._brightness = 0;
  49. state.fields._brightnessColor = 0;
  50. state.fields._brightnessMode = 0;
  51. state.fields._hue = 0;
  52. state.fields._saturation = 0;
  53. state.fields._mode = 0;
  54. state.fields._bulbMode = 0;
  55. state.fields._kelvin = 0;
  56. state.fields._isSetState = 0;
  57. state.fields._isSetHue = 0;
  58. state.fields._isSetBrightness = 0;
  59. state.fields._isSetBrightnessColor = 0;
  60. state.fields._isSetBrightnessMode = 0;
  61. state.fields._isSetSaturation = 0;
  62. state.fields._isSetMode = 0;
  63. state.fields._isSetKelvin = 0;
  64. state.fields._isSetBulbMode = 0;
  65. state.fields._dirty = 1;
  66. state.fields._mqttDirty = 0;
  67. state.fields._isSetNightMode = 0;
  68. state.fields._isNightMode = 0;
  69. }
  70. bool GroupState::isSetField(GroupStateField field) const {
  71. switch (field) {
  72. case GroupStateField::COMPUTED_COLOR:
  73. // Always set -- either send RGB color or white
  74. return true;
  75. case GroupStateField::STATE:
  76. case GroupStateField::STATUS:
  77. return isSetState();
  78. case GroupStateField::BRIGHTNESS:
  79. case GroupStateField::LEVEL:
  80. return isSetBrightness();
  81. case GroupStateField::COLOR:
  82. case GroupStateField::HUE:
  83. return isSetHue();
  84. case GroupStateField::SATURATION:
  85. return isSetSaturation();
  86. case GroupStateField::MODE:
  87. return isSetMode();
  88. case GroupStateField::KELVIN:
  89. case GroupStateField::COLOR_TEMP:
  90. return isSetKelvin();
  91. case GroupStateField::BULB_MODE:
  92. return isSetBulbMode();
  93. }
  94. Serial.print(F("WARNING: tried to check if unknown field was set: "));
  95. Serial.println(static_cast<unsigned int>(field));
  96. return false;
  97. }
  98. bool GroupState::isSetState() const { return state.fields._isSetState; }
  99. MiLightStatus GroupState::getState() const { return state.fields._state ? ON : OFF; }
  100. bool GroupState::setState(const MiLightStatus status) {
  101. if (isSetState() && getState() == status) {
  102. return false;
  103. }
  104. setDirty();
  105. state.fields._isSetState = 1;
  106. state.fields._state = status == ON ? 1 : 0;
  107. return true;
  108. }
  109. bool GroupState::isSetBrightness() const {
  110. if (! state.fields._isSetBulbMode) {
  111. return state.fields._isSetBrightness;
  112. }
  113. switch (state.fields._bulbMode) {
  114. case BULB_MODE_WHITE:
  115. return state.fields._isSetBrightness;
  116. case BULB_MODE_COLOR:
  117. return state.fields._isSetBrightnessColor;
  118. case BULB_MODE_SCENE:
  119. return state.fields._isSetBrightnessMode;
  120. }
  121. return false;
  122. }
  123. uint8_t GroupState::getBrightness() const {
  124. switch (state.fields._bulbMode) {
  125. case BULB_MODE_WHITE:
  126. return state.fields._brightness;
  127. case BULB_MODE_COLOR:
  128. return state.fields._brightnessColor;
  129. case BULB_MODE_SCENE:
  130. return state.fields._brightnessMode;
  131. }
  132. return 0;
  133. }
  134. bool GroupState::setBrightness(uint8_t brightness) {
  135. if (isSetBrightness() && getBrightness() == brightness) {
  136. return false;
  137. }
  138. setDirty();
  139. uint8_t bulbMode = state.fields._bulbMode;
  140. if (! state.fields._isSetBulbMode) {
  141. bulbMode = BULB_MODE_WHITE;
  142. }
  143. switch (bulbMode) {
  144. case BULB_MODE_WHITE:
  145. state.fields._isSetBrightness = 1;
  146. state.fields._brightness = brightness;
  147. break;
  148. case BULB_MODE_COLOR:
  149. state.fields._isSetBrightnessColor = 1;
  150. state.fields._brightnessColor = brightness;
  151. break;
  152. case BULB_MODE_SCENE:
  153. state.fields._isSetBrightnessMode = 1;
  154. state.fields._brightnessMode = brightness;
  155. default:
  156. return false;
  157. }
  158. return true;
  159. }
  160. bool GroupState::isSetHue() const { return state.fields._isSetHue; }
  161. uint16_t GroupState::getHue() const {
  162. return Units::rescale<uint16_t, uint16_t>(state.fields._hue, 360, 255);
  163. }
  164. bool GroupState::setHue(uint16_t hue) {
  165. if (isSetHue() && getHue() == hue) {
  166. return false;
  167. }
  168. setDirty();
  169. state.fields._isSetHue = 1;
  170. state.fields._hue = Units::rescale<uint16_t, uint16_t>(hue, 255, 360);
  171. return true;
  172. }
  173. bool GroupState::isSetSaturation() const { return state.fields._isSetSaturation; }
  174. uint8_t GroupState::getSaturation() const { return state.fields._saturation; }
  175. bool GroupState::setSaturation(uint8_t saturation) {
  176. if (isSetSaturation() && getSaturation() == saturation) {
  177. return false;
  178. }
  179. setDirty();
  180. state.fields._isSetSaturation = 1;
  181. state.fields._saturation = saturation;
  182. return true;
  183. }
  184. bool GroupState::isSetMode() const { return state.fields._isSetMode; }
  185. uint8_t GroupState::getMode() const { return state.fields._mode; }
  186. bool GroupState::setMode(uint8_t mode) {
  187. if (isSetMode() && getMode() == mode) {
  188. return false;
  189. }
  190. setDirty();
  191. state.fields._isSetMode = 1;
  192. state.fields._mode = mode;
  193. return true;
  194. }
  195. bool GroupState::isSetKelvin() const { return state.fields._isSetKelvin; }
  196. uint8_t GroupState::getKelvin() const { return state.fields._kelvin; }
  197. uint16_t GroupState::getMireds() const {
  198. return Units::whiteValToMireds(getKelvin(), 100);
  199. }
  200. bool GroupState::setKelvin(uint8_t kelvin) {
  201. if (isSetKelvin() && getKelvin() == kelvin) {
  202. return false;
  203. }
  204. setDirty();
  205. state.fields._isSetKelvin = 1;
  206. state.fields._kelvin = kelvin;
  207. return true;
  208. }
  209. bool GroupState::setMireds(uint16_t mireds) {
  210. return setKelvin(Units::miredsToWhiteVal(mireds, 100));
  211. }
  212. bool GroupState::isSetBulbMode() const { return state.fields._isSetBulbMode; }
  213. BulbMode GroupState::getBulbMode() const {
  214. BulbMode mode;
  215. // Night mode is a transient state. When power is toggled, the bulb returns
  216. // to the state it was last in. To handle this case, night mode state is
  217. // stored separately.
  218. if (isSetNightMode() && isNightMode()) {
  219. return BULB_MODE_NIGHT;
  220. } else {
  221. return static_cast<BulbMode>(state.fields._bulbMode);
  222. }
  223. }
  224. bool GroupState::setBulbMode(BulbMode bulbMode) {
  225. if (isSetBulbMode() && getBulbMode() == bulbMode) {
  226. return false;
  227. }
  228. setDirty();
  229. // As mentioned in isSetBulbMode, NIGHT_MODE is stored separately.
  230. if (bulbMode == BULB_MODE_NIGHT) {
  231. setNightMode(true);
  232. } else {
  233. state.fields._isSetBulbMode = 1;
  234. state.fields._bulbMode = bulbMode;
  235. }
  236. return true;
  237. }
  238. bool GroupState::isSetNightMode() const { return state.fields._isSetNightMode; }
  239. bool GroupState::isNightMode() const { return state.fields._isNightMode; }
  240. bool GroupState::setNightMode(bool nightMode) {
  241. if (isSetNightMode() && isNightMode() == nightMode) {
  242. return false;
  243. }
  244. setDirty();
  245. state.fields._isSetNightMode = 1;
  246. state.fields._isNightMode = nightMode;
  247. return true;
  248. }
  249. bool GroupState::isDirty() const { return state.fields._dirty; }
  250. inline bool GroupState::setDirty() {
  251. state.fields._dirty = 1;
  252. state.fields._mqttDirty = 1;
  253. }
  254. bool GroupState::clearDirty() { state.fields._dirty = 0; }
  255. bool GroupState::isMqttDirty() const { return state.fields._mqttDirty; }
  256. bool GroupState::clearMqttDirty() { state.fields._mqttDirty = 0; }
  257. void GroupState::load(Stream& stream) {
  258. for (size_t i = 0; i < DATA_BYTES; i++) {
  259. stream.readBytes(reinterpret_cast<uint8_t*>(&state.data[i]), 4);
  260. }
  261. clearDirty();
  262. }
  263. void GroupState::dump(Stream& stream) const {
  264. for (size_t i = 0; i < DATA_BYTES; i++) {
  265. stream.write(reinterpret_cast<const uint8_t*>(&state.data[i]), 4);
  266. }
  267. }
  268. bool GroupState::patch(const JsonObject& state) {
  269. bool changes = false;
  270. if (state.containsKey("state")) {
  271. changes |= setState(state["state"] == "ON" ? ON : OFF);
  272. }
  273. if (state.containsKey("brightness")) {
  274. changes |= setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  275. }
  276. if (state.containsKey("hue")) {
  277. changes |= setHue(state["hue"]);
  278. changes |= setBulbMode(BULB_MODE_COLOR);
  279. }
  280. if (state.containsKey("saturation")) {
  281. changes |= setSaturation(state["saturation"]);
  282. }
  283. if (state.containsKey("mode")) {
  284. changes |= setMode(state["mode"]);
  285. changes |= setBulbMode(BULB_MODE_SCENE);
  286. }
  287. if (state.containsKey("color_temp")) {
  288. changes |= setMireds(state["color_temp"]);
  289. changes |= setBulbMode(BULB_MODE_WHITE);
  290. }
  291. // Any changes other than setting mode to night should take device out of
  292. // night mode.
  293. if (changes && getBulbMode() == BULB_MODE_NIGHT) {
  294. setNightMode(false);
  295. }
  296. if (state.containsKey("command")) {
  297. const String& command = state["command"];
  298. if (command == "white_mode") {
  299. changes |= setBulbMode(BULB_MODE_WHITE);
  300. setNightMode(false);
  301. } else if (command == "night_mode") {
  302. changes |= setBulbMode(BULB_MODE_NIGHT);
  303. }
  304. }
  305. return changes;
  306. }
  307. void GroupState::applyColor(ArduinoJson::JsonObject& state) {
  308. uint8_t rgb[3];
  309. RGBConverter converter;
  310. converter.hsvToRgb(
  311. getHue()/360.0,
  312. // Default to fully saturated
  313. (isSetSaturation() ? getSaturation() : 100)/100.0,
  314. 1,
  315. rgb
  316. );
  317. applyColor(state, rgb[0], rgb[1], rgb[2]);
  318. }
  319. void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g, uint8_t b) {
  320. JsonObject& color = state.createNestedObject("color");
  321. color["r"] = r;
  322. color["g"] = g;
  323. color["b"] = b;
  324. }
  325. void GroupState::applyField(JsonObject& partialState, GroupStateField field) {
  326. if (isSetField(field)) {
  327. switch (field) {
  328. case GroupStateField::STATE:
  329. case GroupStateField::STATUS:
  330. partialState[GroupStateFieldHelpers::getFieldName(field)] = getState() == ON ? "ON" : "OFF";
  331. break;
  332. case GroupStateField::BRIGHTNESS:
  333. partialState["brightness"] = Units::rescale(getBrightness(), 255, 100);
  334. break;
  335. case GroupStateField::LEVEL:
  336. partialState["level"] = getBrightness();
  337. break;
  338. case GroupStateField::BULB_MODE:
  339. partialState["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  340. break;
  341. case GroupStateField::COLOR:
  342. if (getBulbMode() == BULB_MODE_COLOR) {
  343. applyColor(partialState);
  344. }
  345. break;
  346. case GroupStateField::COMPUTED_COLOR:
  347. if (getBulbMode() == BULB_MODE_COLOR) {
  348. applyColor(partialState);
  349. } else {
  350. applyColor(partialState, 255, 255, 255);
  351. }
  352. break;
  353. case GroupStateField::HUE:
  354. if (getBulbMode() == BULB_MODE_COLOR) {
  355. partialState["hue"] = getHue();
  356. }
  357. break;
  358. case GroupStateField::SATURATION:
  359. if (getBulbMode() == BULB_MODE_COLOR) {
  360. partialState["saturation"] = getSaturation();
  361. }
  362. break;
  363. case GroupStateField::MODE:
  364. if (getBulbMode() == BULB_MODE_SCENE) {
  365. partialState["mode"] = getMode();
  366. }
  367. break;
  368. case GroupStateField::COLOR_TEMP:
  369. if (getBulbMode() == BULB_MODE_WHITE) {
  370. partialState["color_temp"] = getMireds();
  371. }
  372. break;
  373. case GroupStateField::KELVIN:
  374. if (getBulbMode() == BULB_MODE_WHITE) {
  375. partialState["kelvin"] = getKelvin();
  376. }
  377. break;
  378. }
  379. }
  380. }
  381. void GroupState::applyState(JsonObject& partialState, GroupStateField* fields, size_t numFields) {
  382. for (size_t i = 0; i < numFields; i++) {
  383. applyField(partialState, fields[i]);
  384. }
  385. }