GroupState.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. #include <MiLightRemoteConfig.h>
  4. #include <RGBConverter.h>
  5. const BulbId DEFAULT_BULB_ID;
  6. static const GroupStateField ALL_PHYSICAL_FIELDS[] = {
  7. GroupStateField::BRIGHTNESS,
  8. GroupStateField::BULB_MODE,
  9. GroupStateField::HUE,
  10. GroupStateField::KELVIN,
  11. GroupStateField::MODE,
  12. GroupStateField::SATURATION,
  13. GroupStateField::STATE
  14. };
  15. // Number of units each increment command counts for
  16. static const uint8_t INCREMENT_COMMAND_VALUE = 10;
  17. const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
  18. static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
  19. GroupState& state = instances[remoteType];
  20. switch (remoteType) {
  21. case REMOTE_TYPE_RGB:
  22. state.setBulbMode(BULB_MODE_COLOR);
  23. break;
  24. case REMOTE_TYPE_CCT:
  25. state.setBulbMode(BULB_MODE_WHITE);
  26. break;
  27. }
  28. return state;
  29. }
  30. BulbId::BulbId()
  31. : deviceId(0),
  32. groupId(0),
  33. deviceType(REMOTE_TYPE_UNKNOWN)
  34. { }
  35. BulbId::BulbId(const BulbId &other)
  36. : deviceId(other.deviceId),
  37. groupId(other.groupId),
  38. deviceType(other.deviceType)
  39. { }
  40. BulbId::BulbId(
  41. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  42. )
  43. : deviceId(deviceId),
  44. groupId(groupId),
  45. deviceType(deviceType)
  46. { }
  47. void BulbId::operator=(const BulbId &other) {
  48. deviceId = other.deviceId;
  49. groupId = other.groupId;
  50. deviceType = other.deviceType;
  51. }
  52. // determine if now BulbId's are the same. This compared deviceID (the controller/remote ID) and
  53. // groupId (the group number on the controller, 1-4 or 1-8 depending), but ignores the deviceType
  54. // (type of controller/remote) as this doesn't directly affect the identity of the bulb
  55. bool BulbId::operator==(const BulbId &other) {
  56. return deviceId == other.deviceId
  57. && groupId == other.groupId
  58. && deviceType == other.deviceType;
  59. }
  60. GroupState::GroupState() {
  61. state.fields._state = 0;
  62. state.fields._brightness = 0;
  63. state.fields._brightnessColor = 0;
  64. state.fields._brightnessMode = 0;
  65. state.fields._hue = 0;
  66. state.fields._saturation = 0;
  67. state.fields._mode = 0;
  68. state.fields._bulbMode = 0;
  69. state.fields._kelvin = 0;
  70. state.fields._isSetState = 0;
  71. state.fields._isSetHue = 0;
  72. state.fields._isSetBrightness = 0;
  73. state.fields._isSetBrightnessColor = 0;
  74. state.fields._isSetBrightnessMode = 0;
  75. state.fields._isSetSaturation = 0;
  76. state.fields._isSetMode = 0;
  77. state.fields._isSetKelvin = 0;
  78. state.fields._isSetBulbMode = 0;
  79. state.fields._dirty = 1;
  80. state.fields._mqttDirty = 0;
  81. state.fields._isSetNightMode = 0;
  82. state.fields._isNightMode = 0;
  83. scratchpad.fields._isSetBrightnessScratch = 0;
  84. scratchpad.fields._brightnessScratch = 0;
  85. scratchpad.fields._isSetKelvinScratch = 0;
  86. scratchpad.fields._kelvinScratch = 0;
  87. }
  88. GroupState& GroupState::operator=(const GroupState& other) {
  89. memcpy(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t));
  90. scratchpad.rawData = other.scratchpad.rawData;
  91. }
  92. GroupState::GroupState(const GroupState& other) {
  93. memcpy(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t));
  94. scratchpad.rawData = other.scratchpad.rawData;
  95. }
  96. bool GroupState::operator==(const GroupState& other) const {
  97. return memcmp(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t)) == 0;
  98. }
  99. bool GroupState::isEqualIgnoreDirty(const GroupState& other) const {
  100. GroupState meCopy = *this;
  101. GroupState otherCopy = other;
  102. meCopy.clearDirty();
  103. meCopy.clearMqttDirty();
  104. otherCopy.clearDirty();
  105. otherCopy.clearMqttDirty();
  106. return meCopy == otherCopy;
  107. }
  108. void GroupState::print(Stream& stream) const {
  109. stream.printf("State: %08X %08X\n", state.rawData[0], state.rawData[1]);
  110. }
  111. bool GroupState::isSetField(GroupStateField field) const {
  112. switch (field) {
  113. case GroupStateField::COMPUTED_COLOR:
  114. // Always set -- either send RGB color or white
  115. return true;
  116. case GroupStateField::DEVICE_ID:
  117. case GroupStateField::GROUP_ID:
  118. case GroupStateField::DEVICE_TYPE:
  119. // These are always defined
  120. return true;
  121. case GroupStateField::STATE:
  122. case GroupStateField::STATUS:
  123. return isSetState();
  124. case GroupStateField::BRIGHTNESS:
  125. case GroupStateField::LEVEL:
  126. return isSetBrightness();
  127. case GroupStateField::COLOR:
  128. case GroupStateField::HUE:
  129. return isSetHue();
  130. case GroupStateField::SATURATION:
  131. return isSetSaturation();
  132. case GroupStateField::MODE:
  133. return isSetMode();
  134. case GroupStateField::EFFECT:
  135. return isSetEffect();
  136. case GroupStateField::KELVIN:
  137. case GroupStateField::COLOR_TEMP:
  138. return isSetKelvin();
  139. case GroupStateField::BULB_MODE:
  140. return isSetBulbMode();
  141. }
  142. Serial.print(F("WARNING: tried to check if unknown field was set: "));
  143. Serial.println(static_cast<unsigned int>(field));
  144. return false;
  145. }
  146. bool GroupState::isSetScratchField(GroupStateField field) const {
  147. switch (field) {
  148. case GroupStateField::BRIGHTNESS:
  149. return scratchpad.fields._isSetBrightnessScratch;
  150. case GroupStateField::KELVIN:
  151. return scratchpad.fields._isSetKelvinScratch;
  152. }
  153. Serial.print(F("WARNING: tried to check if unknown scratch field was set: "));
  154. Serial.println(static_cast<unsigned int>(field));
  155. return false;
  156. }
  157. uint16_t GroupState::getFieldValue(GroupStateField field) const {
  158. switch (field) {
  159. case GroupStateField::STATE:
  160. case GroupStateField::STATUS:
  161. return getState();
  162. case GroupStateField::BRIGHTNESS:
  163. return getBrightness();
  164. case GroupStateField::HUE:
  165. return getHue();
  166. case GroupStateField::SATURATION:
  167. return getSaturation();
  168. case GroupStateField::MODE:
  169. return getMode();
  170. case GroupStateField::KELVIN:
  171. return getKelvin();
  172. case GroupStateField::BULB_MODE:
  173. return getBulbMode();
  174. }
  175. Serial.print(F("WARNING: tried to fetch value for unknown field: "));
  176. Serial.println(static_cast<unsigned int>(field));
  177. return 0;
  178. }
  179. uint16_t GroupState::getScratchFieldValue(GroupStateField field) const {
  180. switch (field) {
  181. case GroupStateField::BRIGHTNESS:
  182. return scratchpad.fields._brightnessScratch;
  183. case GroupStateField::KELVIN:
  184. return scratchpad.fields._kelvinScratch;
  185. }
  186. Serial.print(F("WARNING: tried to fetch value for unknown scratch field: "));
  187. Serial.println(static_cast<unsigned int>(field));
  188. return 0;
  189. }
  190. void GroupState::setFieldValue(GroupStateField field, uint16_t value) {
  191. switch (field) {
  192. case GroupStateField::STATE:
  193. case GroupStateField::STATUS:
  194. setState(static_cast<MiLightStatus>(value));
  195. break;
  196. case GroupStateField::BRIGHTNESS:
  197. setBrightness(value);
  198. break;
  199. case GroupStateField::HUE:
  200. setHue(value);
  201. break;
  202. case GroupStateField::SATURATION:
  203. setSaturation(value);
  204. break;
  205. case GroupStateField::MODE:
  206. setMode(value);
  207. break;
  208. case GroupStateField::KELVIN:
  209. setKelvin(value);
  210. break;
  211. case GroupStateField::BULB_MODE:
  212. setBulbMode(static_cast<BulbMode>(value));
  213. break;
  214. default:
  215. Serial.print(F("WARNING: tried to set value for unknown field: "));
  216. Serial.println(static_cast<unsigned int>(field));
  217. break;
  218. }
  219. }
  220. void GroupState::setScratchFieldValue(GroupStateField field, uint16_t value) {
  221. switch (field) {
  222. case GroupStateField::BRIGHTNESS:
  223. scratchpad.fields._isSetBrightnessScratch = 1;
  224. scratchpad.fields._brightnessScratch = value;
  225. break;
  226. case GroupStateField::KELVIN:
  227. scratchpad.fields._isSetKelvinScratch = 1;
  228. scratchpad.fields._kelvinScratch = value;
  229. break;
  230. default:
  231. Serial.print(F("WARNING: tried to set value for unknown scratch field: "));
  232. Serial.println(static_cast<unsigned int>(field));
  233. break;
  234. }
  235. }
  236. bool GroupState::isSetState() const { return state.fields._isSetState; }
  237. MiLightStatus GroupState::getState() const { return state.fields._state ? ON : OFF; }
  238. bool GroupState::isOn() const {
  239. return !isNightMode() && (!isSetState() || getState() == MiLightStatus::ON);
  240. }
  241. bool GroupState::setState(const MiLightStatus status) {
  242. if (!isNightMode() && isSetState() && getState() == status) {
  243. return false;
  244. }
  245. setDirty();
  246. state.fields._isSetState = 1;
  247. state.fields._state = status == ON ? 1 : 0;
  248. // Changing status will clear night mode
  249. setNightMode(false);
  250. return true;
  251. }
  252. bool GroupState::isSetBrightness() const {
  253. if (! state.fields._isSetBulbMode) {
  254. return state.fields._isSetBrightness;
  255. }
  256. switch (state.fields._bulbMode) {
  257. case BULB_MODE_WHITE:
  258. return state.fields._isSetBrightness;
  259. case BULB_MODE_COLOR:
  260. return state.fields._isSetBrightnessColor;
  261. case BULB_MODE_SCENE:
  262. return state.fields._isSetBrightnessMode;
  263. }
  264. return false;
  265. }
  266. uint8_t GroupState::getBrightness() const {
  267. switch (state.fields._bulbMode) {
  268. case BULB_MODE_WHITE:
  269. return state.fields._brightness;
  270. case BULB_MODE_COLOR:
  271. return state.fields._brightnessColor;
  272. case BULB_MODE_SCENE:
  273. return state.fields._brightnessMode;
  274. }
  275. return 0;
  276. }
  277. bool GroupState::setBrightness(uint8_t brightness) {
  278. if (isSetBrightness() && getBrightness() == brightness) {
  279. return false;
  280. }
  281. setDirty();
  282. uint8_t bulbMode = state.fields._bulbMode;
  283. if (! state.fields._isSetBulbMode) {
  284. bulbMode = BULB_MODE_WHITE;
  285. }
  286. switch (bulbMode) {
  287. case BULB_MODE_WHITE:
  288. state.fields._isSetBrightness = 1;
  289. state.fields._brightness = brightness;
  290. break;
  291. case BULB_MODE_COLOR:
  292. state.fields._isSetBrightnessColor = 1;
  293. state.fields._brightnessColor = brightness;
  294. break;
  295. case BULB_MODE_SCENE:
  296. state.fields._isSetBrightnessMode = 1;
  297. state.fields._brightnessMode = brightness;
  298. default:
  299. return false;
  300. }
  301. return true;
  302. }
  303. bool GroupState::isSetHue() const { return state.fields._isSetHue; }
  304. uint16_t GroupState::getHue() const {
  305. return Units::rescale<uint16_t, uint16_t>(state.fields._hue, 360, 255);
  306. }
  307. bool GroupState::setHue(uint16_t hue) {
  308. if (isSetHue() && getHue() == hue) {
  309. return false;
  310. }
  311. setDirty();
  312. state.fields._isSetHue = 1;
  313. state.fields._hue = Units::rescale<uint16_t, uint16_t>(hue, 255, 360);
  314. return true;
  315. }
  316. bool GroupState::isSetSaturation() const { return state.fields._isSetSaturation; }
  317. uint8_t GroupState::getSaturation() const { return state.fields._saturation; }
  318. bool GroupState::setSaturation(uint8_t saturation) {
  319. if (isSetSaturation() && getSaturation() == saturation) {
  320. return false;
  321. }
  322. setDirty();
  323. state.fields._isSetSaturation = 1;
  324. state.fields._saturation = saturation;
  325. return true;
  326. }
  327. bool GroupState::isSetMode() const { return state.fields._isSetMode; }
  328. bool GroupState::isSetEffect() const {
  329. // only BULB_MODE_COLOR does not have an effect.
  330. return isSetBulbMode() && getBulbMode() != BULB_MODE_COLOR;
  331. }
  332. uint8_t GroupState::getMode() const { return state.fields._mode; }
  333. bool GroupState::setMode(uint8_t mode) {
  334. if (isSetMode() && getMode() == mode) {
  335. return false;
  336. }
  337. setDirty();
  338. state.fields._isSetMode = 1;
  339. state.fields._mode = mode;
  340. return true;
  341. }
  342. bool GroupState::isSetKelvin() const { return state.fields._isSetKelvin; }
  343. uint8_t GroupState::getKelvin() const { return state.fields._kelvin; }
  344. uint16_t GroupState::getMireds() const {
  345. return Units::whiteValToMireds(getKelvin(), 100);
  346. }
  347. bool GroupState::setKelvin(uint8_t kelvin) {
  348. if (isSetKelvin() && getKelvin() == kelvin) {
  349. return false;
  350. }
  351. setDirty();
  352. state.fields._isSetKelvin = 1;
  353. state.fields._kelvin = kelvin;
  354. return true;
  355. }
  356. bool GroupState::setMireds(uint16_t mireds) {
  357. return setKelvin(Units::miredsToWhiteVal(mireds, 100));
  358. }
  359. bool GroupState::isSetBulbMode() const { return state.fields._isSetBulbMode; }
  360. BulbMode GroupState::getBulbMode() const {
  361. BulbMode mode;
  362. // Night mode is a transient state. When power is toggled, the bulb returns
  363. // to the state it was last in. To handle this case, night mode state is
  364. // stored separately.
  365. if (isSetNightMode() && isNightMode()) {
  366. return BULB_MODE_NIGHT;
  367. } else {
  368. return static_cast<BulbMode>(state.fields._bulbMode);
  369. }
  370. }
  371. bool GroupState::setBulbMode(BulbMode bulbMode) {
  372. if (isSetBulbMode() && getBulbMode() == bulbMode) {
  373. return false;
  374. }
  375. setDirty();
  376. // As mentioned in isSetBulbMode, NIGHT_MODE is stored separately.
  377. if (bulbMode == BULB_MODE_NIGHT) {
  378. setNightMode(true);
  379. } else {
  380. state.fields._isSetBulbMode = 1;
  381. state.fields._bulbMode = bulbMode;
  382. }
  383. return true;
  384. }
  385. bool GroupState::isSetNightMode() const { return state.fields._isSetNightMode; }
  386. bool GroupState::isNightMode() const { return state.fields._isNightMode; }
  387. bool GroupState::setNightMode(bool nightMode) {
  388. if (isSetNightMode() && isNightMode() == nightMode) {
  389. return false;
  390. }
  391. setDirty();
  392. state.fields._isSetNightMode = 1;
  393. state.fields._isNightMode = nightMode;
  394. return true;
  395. }
  396. bool GroupState::isDirty() const { return state.fields._dirty; }
  397. inline bool GroupState::setDirty() {
  398. state.fields._dirty = 1;
  399. state.fields._mqttDirty = 1;
  400. }
  401. bool GroupState::clearDirty() { state.fields._dirty = 0; }
  402. bool GroupState::isMqttDirty() const { return state.fields._mqttDirty; }
  403. bool GroupState::clearMqttDirty() { state.fields._mqttDirty = 0; }
  404. void GroupState::load(Stream& stream) {
  405. for (size_t i = 0; i < DATA_LONGS; i++) {
  406. stream.readBytes(reinterpret_cast<uint8_t*>(&state.rawData[i]), 4);
  407. }
  408. clearDirty();
  409. }
  410. void GroupState::dump(Stream& stream) const {
  411. for (size_t i = 0; i < DATA_LONGS; i++) {
  412. stream.write(reinterpret_cast<const uint8_t*>(&state.rawData[i]), 4);
  413. }
  414. }
  415. bool GroupState::applyIncrementCommand(GroupStateField field, IncrementDirection dir) {
  416. if (field != GroupStateField::KELVIN && field != GroupStateField::BRIGHTNESS) {
  417. Serial.print(F("WARNING: tried to apply increment for unsupported field: "));
  418. Serial.println(static_cast<uint8_t>(field));
  419. return false;
  420. }
  421. int8_t dirValue = static_cast<int8_t>(dir);
  422. // If there's already a known value, update it
  423. if (isSetField(field)) {
  424. int8_t currentValue = static_cast<int8_t>(getFieldValue(field));
  425. int8_t newValue = currentValue + (dirValue * INCREMENT_COMMAND_VALUE);
  426. #ifdef STATE_DEBUG
  427. debugState("Updating field from increment command");
  428. #endif
  429. // For now, assume range for both brightness and kelvin is [0, 100]
  430. setFieldValue(field, constrain(newValue, 0, 100));
  431. return true;
  432. // Otherwise start or update scratch state
  433. } else {
  434. if (isSetScratchField(field)) {
  435. int8_t newValue = static_cast<int8_t>(getScratchFieldValue(field)) + dirValue;
  436. if (newValue == 0 || newValue == 10) {
  437. setFieldValue(field, newValue * INCREMENT_COMMAND_VALUE);
  438. return true;
  439. } else {
  440. setScratchFieldValue(field, newValue);
  441. }
  442. } else if (dir == IncrementDirection::DECREASE) {
  443. setScratchFieldValue(field, 9);
  444. } else {
  445. setScratchFieldValue(field, 1);
  446. }
  447. #ifdef STATE_DEBUG
  448. Serial.print(F("Updated scratch field: "));
  449. Serial.print(static_cast<int8_t>(field));
  450. Serial.print(F(" to: "));
  451. Serial.println(getScratchFieldValue(field));
  452. #endif
  453. }
  454. return false;
  455. }
  456. bool GroupState::patch(const GroupState& other) {
  457. for (size_t i = 0; i < size(ALL_PHYSICAL_FIELDS); ++i) {
  458. GroupStateField field = ALL_PHYSICAL_FIELDS[i];
  459. if (other.isSetField(field)) {
  460. setFieldValue(field, other.getFieldValue(field));
  461. }
  462. }
  463. }
  464. /*
  465. Update group state to reflect a packet state
  466. Called both when a packet is sent locally, and when an intercepted packet is read
  467. (see main.cpp onPacketSentHandler)
  468. Returns true if the packet changes affects a state change
  469. */
  470. bool GroupState::patch(const JsonObject& state) {
  471. bool changes = false;
  472. #ifdef STATE_DEBUG
  473. Serial.print(F("Patching existing state with: "));
  474. state.printTo(Serial);
  475. Serial.println();
  476. #endif
  477. if (state.containsKey("state")) {
  478. bool stateChange = setState(state["state"] == "ON" ? ON : OFF);
  479. changes |= stateChange;
  480. }
  481. // Devices do not support changing their state while off, so don't apply state
  482. // changes to devices we know are off.
  483. if (isOn() && state.containsKey("brightness")) {
  484. bool stateChange = setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  485. changes |= stateChange;
  486. }
  487. if (isOn() && state.containsKey("hue")) {
  488. changes |= setHue(state["hue"]);
  489. changes |= setBulbMode(BULB_MODE_COLOR);
  490. }
  491. if (isOn() && state.containsKey("saturation")) {
  492. changes |= setSaturation(state["saturation"]);
  493. }
  494. if (isOn() && state.containsKey("mode")) {
  495. changes |= setMode(state["mode"]);
  496. changes |= setBulbMode(BULB_MODE_SCENE);
  497. }
  498. if (isOn() && state.containsKey("color_temp")) {
  499. changes |= setMireds(state["color_temp"]);
  500. changes |= setBulbMode(BULB_MODE_WHITE);
  501. }
  502. if (state.containsKey("command")) {
  503. const String& command = state["command"];
  504. if (isOn() && command == "set_white") {
  505. changes |= setBulbMode(BULB_MODE_WHITE);
  506. } else if (command == "night_mode") {
  507. changes |= setBulbMode(BULB_MODE_NIGHT);
  508. } else if (isOn() && command == "brightness_up") {
  509. changes |= applyIncrementCommand(GroupStateField::BRIGHTNESS, IncrementDirection::INCREASE);
  510. } else if (isOn() && command == "brightness_down") {
  511. changes |= applyIncrementCommand(GroupStateField::BRIGHTNESS, IncrementDirection::DECREASE);
  512. } else if (isOn() && command == "temperature_up") {
  513. changes |= applyIncrementCommand(GroupStateField::KELVIN, IncrementDirection::INCREASE);
  514. } else if (isOn() && command == "temperature_down") {
  515. changes |= applyIncrementCommand(GroupStateField::KELVIN, IncrementDirection::DECREASE);
  516. }
  517. }
  518. if (changes) {
  519. debugState("GroupState::patch: State changed");
  520. }
  521. else {
  522. debugState("GroupState::patch: State not changed");
  523. }
  524. return changes;
  525. }
  526. void GroupState::applyColor(ArduinoJson::JsonObject& state) {
  527. uint8_t rgb[3];
  528. RGBConverter converter;
  529. converter.hsvToRgb(
  530. getHue()/360.0,
  531. // Default to fully saturated
  532. (isSetSaturation() ? getSaturation() : 100)/100.0,
  533. 1,
  534. rgb
  535. );
  536. applyColor(state, rgb[0], rgb[1], rgb[2]);
  537. }
  538. void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g, uint8_t b) {
  539. JsonObject& color = state.createNestedObject("color");
  540. color["r"] = r;
  541. color["g"] = g;
  542. color["b"] = b;
  543. }
  544. // gather partial state for a single field; see GroupState::applyState to gather many fields
  545. void GroupState::applyField(JsonObject& partialState, const BulbId& bulbId, GroupStateField field) {
  546. if (isSetField(field)) {
  547. switch (field) {
  548. case GroupStateField::STATE:
  549. case GroupStateField::STATUS:
  550. partialState[GroupStateFieldHelpers::getFieldName(field)] = getState() == ON ? "ON" : "OFF";
  551. break;
  552. case GroupStateField::BRIGHTNESS:
  553. partialState["brightness"] = Units::rescale(getBrightness(), 255, 100);
  554. break;
  555. case GroupStateField::LEVEL:
  556. partialState["level"] = getBrightness();
  557. break;
  558. case GroupStateField::BULB_MODE:
  559. partialState["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  560. break;
  561. case GroupStateField::COLOR:
  562. if (getBulbMode() == BULB_MODE_COLOR) {
  563. applyColor(partialState);
  564. }
  565. break;
  566. case GroupStateField::COMPUTED_COLOR:
  567. if (getBulbMode() == BULB_MODE_COLOR) {
  568. applyColor(partialState);
  569. } else {
  570. applyColor(partialState, 255, 255, 255);
  571. }
  572. break;
  573. case GroupStateField::HUE:
  574. if (getBulbMode() == BULB_MODE_COLOR) {
  575. partialState["hue"] = getHue();
  576. }
  577. break;
  578. case GroupStateField::SATURATION:
  579. if (getBulbMode() == BULB_MODE_COLOR) {
  580. partialState["saturation"] = getSaturation();
  581. }
  582. break;
  583. case GroupStateField::MODE:
  584. if (getBulbMode() == BULB_MODE_SCENE) {
  585. partialState["mode"] = getMode();
  586. }
  587. break;
  588. case GroupStateField::EFFECT:
  589. if (getBulbMode() == BULB_MODE_SCENE) {
  590. partialState["effect"] = String(getMode());
  591. } else if (getBulbMode() == BULB_MODE_WHITE) {
  592. partialState["effect"] = "white_mode";
  593. } else if (getBulbMode() == BULB_MODE_NIGHT) {
  594. partialState["effect"] = "night_mode";
  595. }
  596. break;
  597. case GroupStateField::COLOR_TEMP:
  598. if (getBulbMode() == BULB_MODE_WHITE) {
  599. partialState["color_temp"] = getMireds();
  600. }
  601. break;
  602. case GroupStateField::KELVIN:
  603. if (getBulbMode() == BULB_MODE_WHITE) {
  604. partialState["kelvin"] = getKelvin();
  605. }
  606. break;
  607. case GroupStateField::DEVICE_ID:
  608. partialState["device_id"] = bulbId.deviceId;
  609. break;
  610. case GroupStateField::GROUP_ID:
  611. partialState["group_id"] = bulbId.groupId;
  612. break;
  613. case GroupStateField::DEVICE_TYPE:
  614. const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromType(bulbId.deviceType);
  615. if (remoteConfig) {
  616. partialState["device_type"] = remoteConfig->name;
  617. }
  618. break;
  619. }
  620. }
  621. }
  622. // helper function to debug the current state (in JSON) to the serial port
  623. void GroupState::debugState(char const *debugMessage) {
  624. #ifdef STATE_DEBUG
  625. // using static to keep large buffers off the call stack
  626. static StaticJsonBuffer<500> jsonBuffer;
  627. // define fields to show (if count changes, make sure to update count to applyState below)
  628. GroupStateField fields[] {
  629. GroupStateField::BRIGHTNESS,
  630. GroupStateField::BULB_MODE,
  631. GroupStateField::COLOR,
  632. GroupStateField::COLOR_TEMP,
  633. GroupStateField::COMPUTED_COLOR,
  634. GroupStateField::EFFECT,
  635. GroupStateField::HUE,
  636. GroupStateField::KELVIN,
  637. GroupStateField::LEVEL,
  638. GroupStateField::MODE,
  639. GroupStateField::SATURATION,
  640. GroupStateField::STATE,
  641. GroupStateField::STATUS };
  642. // since our buffer is reused, make sure to clear it every time
  643. jsonBuffer.clear();
  644. JsonObject& jsonState = jsonBuffer.createObject();
  645. // Fake id
  646. BulbId id;
  647. // use applyState to build JSON of all fields (from above)
  648. applyState(jsonState, id, fields, 13);
  649. // convert to string and print
  650. Serial.printf("%s: ", debugMessage);
  651. jsonState.printTo(Serial);
  652. Serial.println("");
  653. #endif
  654. }
  655. // build up a partial state representation based on the specified GrouipStateField array. Used
  656. // to gather a subset of states (configurable in the UI) for sending to MQTT and web responses.
  657. void GroupState::applyState(JsonObject& partialState, const BulbId& bulbId, GroupStateField* fields, size_t numFields) {
  658. for (size_t i = 0; i < numFields; i++) {
  659. applyField(partialState, bulbId, fields[i]);
  660. }
  661. }