GroupState.cpp 10 KB

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