GroupState.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. }
  68. bool GroupState::isSetField(GroupStateField field) const {
  69. switch (field) {
  70. case GroupStateField::COMPUTED_COLOR:
  71. // Always set -- either send RGB color or white
  72. return true;
  73. case GroupStateField::STATE:
  74. case GroupStateField::STATUS:
  75. return isSetState();
  76. case GroupStateField::BRIGHTNESS:
  77. case GroupStateField::LEVEL:
  78. return isSetBrightness();
  79. case GroupStateField::COLOR:
  80. case GroupStateField::HUE:
  81. return isSetHue();
  82. case GroupStateField::SATURATION:
  83. return isSetSaturation();
  84. case GroupStateField::MODE:
  85. return isSetMode();
  86. case GroupStateField::KELVIN:
  87. case GroupStateField::COLOR_TEMP:
  88. return isSetKelvin();
  89. case GroupStateField::BULB_MODE:
  90. return isSetBulbMode();
  91. }
  92. Serial.print(F("WARNING: tried to check if unknown field was set: "));
  93. Serial.println(static_cast<unsigned int>(field));
  94. return false;
  95. }
  96. bool GroupState::isSetState() const { return state.fields._isSetState; }
  97. MiLightStatus GroupState::getState() const { return state.fields._state ? ON : OFF; }
  98. bool GroupState::setState(const MiLightStatus status) {
  99. if (isSetState() && getState() == status) {
  100. return false;
  101. }
  102. setDirty();
  103. state.fields._isSetState = 1;
  104. state.fields._state = status == ON ? 1 : 0;
  105. return true;
  106. }
  107. bool GroupState::isSetBrightness() const {
  108. if (! state.fields._isSetBulbMode) {
  109. return state.fields._isSetBrightness;
  110. }
  111. switch (state.fields._bulbMode) {
  112. case BULB_MODE_WHITE:
  113. return state.fields._isSetBrightness;
  114. case BULB_MODE_COLOR:
  115. return state.fields._isSetBrightnessColor;
  116. case BULB_MODE_SCENE:
  117. return state.fields._isSetBrightnessMode;
  118. }
  119. return false;
  120. }
  121. uint8_t GroupState::getBrightness() const {
  122. switch (state.fields._bulbMode) {
  123. case BULB_MODE_WHITE:
  124. return state.fields._brightness;
  125. case BULB_MODE_COLOR:
  126. return state.fields._brightnessColor;
  127. case BULB_MODE_SCENE:
  128. return state.fields._brightnessMode;
  129. }
  130. return 0;
  131. }
  132. bool GroupState::setBrightness(uint8_t brightness) {
  133. if (isSetBrightness() && getBrightness() == brightness) {
  134. return false;
  135. }
  136. setDirty();
  137. uint8_t bulbMode = state.fields._bulbMode;
  138. if (! state.fields._isSetBulbMode) {
  139. bulbMode = BULB_MODE_WHITE;
  140. }
  141. switch (bulbMode) {
  142. case BULB_MODE_WHITE:
  143. state.fields._isSetBrightness = 1;
  144. state.fields._brightness = brightness;
  145. break;
  146. case BULB_MODE_COLOR:
  147. state.fields._isSetBrightnessColor = 1;
  148. state.fields._brightnessColor = brightness;
  149. break;
  150. case BULB_MODE_SCENE:
  151. state.fields._isSetBrightnessMode = 1;
  152. state.fields._brightnessMode = brightness;
  153. default:
  154. return false;
  155. }
  156. return true;
  157. }
  158. bool GroupState::isSetHue() const { return state.fields._isSetHue; }
  159. uint16_t GroupState::getHue() const {
  160. return Units::rescale<uint16_t, uint16_t>(state.fields._hue, 360, 255);
  161. }
  162. bool GroupState::setHue(uint16_t hue) {
  163. if (isSetHue() && getHue() == hue) {
  164. return false;
  165. }
  166. setDirty();
  167. state.fields._isSetHue = 1;
  168. state.fields._hue = Units::rescale<uint16_t, uint16_t>(hue, 255, 360);
  169. return true;
  170. }
  171. bool GroupState::isSetSaturation() const { return state.fields._isSetSaturation; }
  172. uint8_t GroupState::getSaturation() const { return state.fields._saturation; }
  173. bool GroupState::setSaturation(uint8_t saturation) {
  174. if (isSetSaturation() && getSaturation() == saturation) {
  175. return false;
  176. }
  177. setDirty();
  178. state.fields._isSetSaturation = 1;
  179. state.fields._saturation = saturation;
  180. return true;
  181. }
  182. bool GroupState::isSetMode() const { return state.fields._isSetMode; }
  183. uint8_t GroupState::getMode() const { return state.fields._mode; }
  184. bool GroupState::setMode(uint8_t mode) {
  185. if (isSetMode() && getMode() == mode) {
  186. return false;
  187. }
  188. setDirty();
  189. state.fields._isSetMode = 1;
  190. state.fields._mode = mode;
  191. return true;
  192. }
  193. bool GroupState::isSetKelvin() const { return state.fields._isSetKelvin; }
  194. uint8_t GroupState::getKelvin() const { return state.fields._kelvin; }
  195. uint16_t GroupState::getMireds() const {
  196. return Units::whiteValToMireds(getKelvin(), 100);
  197. }
  198. bool GroupState::setKelvin(uint8_t kelvin) {
  199. if (isSetKelvin() && getKelvin() == kelvin) {
  200. return false;
  201. }
  202. setDirty();
  203. state.fields._isSetKelvin = 1;
  204. state.fields._kelvin = kelvin;
  205. return true;
  206. }
  207. bool GroupState::setMireds(uint16_t mireds) {
  208. return setKelvin(Units::miredsToWhiteVal(mireds, 100));
  209. }
  210. bool GroupState::isSetBulbMode() const { return state.fields._isSetBulbMode; }
  211. BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(state.fields._bulbMode); }
  212. bool GroupState::setBulbMode(BulbMode bulbMode) {
  213. if (isSetBulbMode() && getBulbMode() == bulbMode) {
  214. return false;
  215. }
  216. setDirty();
  217. state.fields._isSetBulbMode = 1;
  218. state.fields._bulbMode = bulbMode;
  219. return true;
  220. }
  221. bool GroupState::isDirty() const { return state.fields._dirty; }
  222. inline bool GroupState::setDirty() {
  223. state.fields._dirty = 1;
  224. state.fields._mqttDirty = 1;
  225. }
  226. bool GroupState::clearDirty() { state.fields._dirty = 0; }
  227. bool GroupState::isMqttDirty() const { return state.fields._mqttDirty; }
  228. bool GroupState::clearMqttDirty() { state.fields._mqttDirty = 0; }
  229. void GroupState::load(Stream& stream) {
  230. for (size_t i = 0; i < DATA_BYTES; i++) {
  231. stream.readBytes(reinterpret_cast<uint8_t*>(&state.data[i]), 4);
  232. }
  233. clearDirty();
  234. }
  235. void GroupState::dump(Stream& stream) const {
  236. for (size_t i = 0; i < DATA_BYTES; i++) {
  237. stream.write(reinterpret_cast<const uint8_t*>(&state.data[i]), 4);
  238. }
  239. }
  240. bool GroupState::patch(const JsonObject& state) {
  241. bool changes = false;
  242. if (state.containsKey("state")) {
  243. changes |= setState(state["state"] == "ON" ? ON : OFF);
  244. }
  245. if (state.containsKey("brightness")) {
  246. changes |= setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  247. }
  248. if (state.containsKey("hue")) {
  249. changes |= setHue(state["hue"]);
  250. changes |= setBulbMode(BULB_MODE_COLOR);
  251. }
  252. if (state.containsKey("saturation")) {
  253. changes |= setSaturation(state["saturation"]);
  254. }
  255. if (state.containsKey("mode")) {
  256. changes |= setMode(state["mode"]);
  257. changes |= setBulbMode(BULB_MODE_SCENE);
  258. }
  259. if (state.containsKey("color_temp")) {
  260. changes |= setMireds(state["color_temp"]);
  261. changes |= setBulbMode(BULB_MODE_WHITE);
  262. }
  263. if (state.containsKey("command")) {
  264. const String& command = state["command"];
  265. if (command == "white_mode") {
  266. changes |= setBulbMode(BULB_MODE_WHITE);
  267. } else if (command == "night_mode") {
  268. changes |= setBulbMode(BULB_MODE_NIGHT);
  269. }
  270. }
  271. return changes;
  272. }
  273. void GroupState::applyColor(ArduinoJson::JsonObject& state) {
  274. uint8_t rgb[3];
  275. RGBConverter converter;
  276. converter.hsvToRgb(
  277. getHue()/360.0,
  278. // Default to fully saturated
  279. (isSetSaturation() ? getSaturation() : 100)/100.0,
  280. 1,
  281. rgb
  282. );
  283. applyColor(state, rgb[0], rgb[1], rgb[2]);
  284. }
  285. void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g, uint8_t b) {
  286. JsonObject& color = state.createNestedObject("color");
  287. color["r"] = r;
  288. color["g"] = g;
  289. color["b"] = b;
  290. }
  291. void GroupState::applyField(JsonObject& partialState, GroupStateField field) {
  292. if (isSetField(field)) {
  293. switch (field) {
  294. case GroupStateField::STATE:
  295. case GroupStateField::STATUS:
  296. partialState[GroupStateFieldHelpers::getFieldName(field)] = getState() == ON ? "ON" : "OFF";
  297. break;
  298. case GroupStateField::BRIGHTNESS:
  299. partialState["brightness"] = Units::rescale(getBrightness(), 255, 100);
  300. break;
  301. case GroupStateField::LEVEL:
  302. partialState["level"] = getBrightness();
  303. break;
  304. case GroupStateField::BULB_MODE:
  305. partialState["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  306. break;
  307. case GroupStateField::COLOR:
  308. if (getBulbMode() == BULB_MODE_COLOR) {
  309. applyColor(partialState);
  310. }
  311. break;
  312. case GroupStateField::COMPUTED_COLOR:
  313. if (getBulbMode() == BULB_MODE_COLOR) {
  314. applyColor(partialState);
  315. } else {
  316. applyColor(partialState, 255, 255, 255);
  317. }
  318. break;
  319. case GroupStateField::HUE:
  320. if (getBulbMode() == BULB_MODE_COLOR) {
  321. partialState["hue"] = getHue();
  322. }
  323. break;
  324. case GroupStateField::SATURATION:
  325. if (getBulbMode() == BULB_MODE_COLOR) {
  326. partialState["saturation"] = getSaturation();
  327. }
  328. break;
  329. case GroupStateField::MODE:
  330. if (getBulbMode() == BULB_MODE_SCENE) {
  331. partialState["mode"] = getMode();
  332. }
  333. break;
  334. case GroupStateField::COLOR_TEMP:
  335. if (getBulbMode() == BULB_MODE_WHITE) {
  336. partialState["color_temp"] = getMireds();
  337. }
  338. break;
  339. case GroupStateField::KELVIN:
  340. if (getBulbMode() == BULB_MODE_WHITE) {
  341. partialState["kelvin"] = getKelvin();
  342. }
  343. break;
  344. }
  345. }
  346. }
  347. void GroupState::applyState(JsonObject& partialState, GroupStateField* fields, size_t numFields) {
  348. for (size_t i = 0; i < numFields; i++) {
  349. applyField(partialState, fields[i]);
  350. }
  351. }