GroupState.cpp 8.8 KB

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