GroupState.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. const GroupState& GroupState::defaultState() {
  4. static GroupState instance;
  5. return instance;
  6. }
  7. GroupId::GroupId()
  8. : deviceId(0),
  9. groupId(0),
  10. deviceType(REMOTE_TYPE_UNKNOWN)
  11. { }
  12. GroupId::GroupId(const GroupId &other)
  13. : deviceId(other.deviceId),
  14. groupId(other.groupId),
  15. deviceType(other.deviceType)
  16. { }
  17. GroupId::GroupId(
  18. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  19. )
  20. : deviceId(deviceId),
  21. groupId(groupId),
  22. deviceType(deviceType)
  23. { }
  24. void GroupId::operator=(const GroupId &other) {
  25. deviceId = other.deviceId;
  26. groupId = other.groupId;
  27. deviceType = other.deviceType;
  28. }
  29. bool GroupId::operator==(const GroupId &other) {
  30. return deviceId == other.deviceId
  31. && groupId == other.groupId
  32. && deviceType == other.deviceType;
  33. }
  34. GroupState::GroupState() {
  35. _state = 0;
  36. _brightness = 0;
  37. _hue = 0;
  38. _saturation = 0;
  39. _mode = 0;
  40. _bulbMode = 0;
  41. _kelvin = 0;
  42. _isSetState = 0;
  43. _isSetHue = 0;
  44. _isSetBrightness = 0;
  45. _isSetSaturation = 0;
  46. _isSetMode = 0;
  47. _isSetKelvin = 0;
  48. _isSetBulbMode = 0;
  49. }
  50. bool GroupState::isSetState() const { return _isSetState; }
  51. MiLightStatus GroupState::getState() const { return _state ? ON : OFF; }
  52. void GroupState::setState(const MiLightStatus& state) {
  53. _isSetState = 1;
  54. _state = state == ON ? 1 : 0;
  55. }
  56. bool GroupState::isSetBrightness() const { return _isSetBrightness; }
  57. uint8_t GroupState::getBrightness() const { return _brightness; }
  58. void GroupState::setBrightness(uint8_t brightness) {
  59. _isSetBrightness = 1;
  60. _brightness = brightness;
  61. }
  62. bool GroupState::isSetHue() const { return _isSetHue; }
  63. uint8_t GroupState::getHue() const { return _hue; }
  64. void GroupState::setHue(uint8_t hue) {
  65. _isSetHue = 1;
  66. _hue = hue;
  67. }
  68. bool GroupState::isSetSaturation() const { return _isSetSaturation; }
  69. uint8_t GroupState::getSaturation() const { return _saturation; }
  70. void GroupState::setSaturation(uint8_t saturation) {
  71. _isSetSaturation = 1;
  72. _saturation = saturation;
  73. }
  74. bool GroupState::isSetMode() const { return _isSetMode; }
  75. uint8_t GroupState::getMode() const { return _mode; }
  76. void GroupState::setMode(uint8_t mode) {
  77. _isSetMode = 1;
  78. _mode = mode;
  79. }
  80. bool GroupState::isSetKelvin() const { return _isSetKelvin; }
  81. uint8_t GroupState::getKelvin() const { return _kelvin; }
  82. void GroupState::setKelvin(uint8_t kelvin) {
  83. _isSetKelvin = 1;
  84. _kelvin = kelvin;
  85. }
  86. bool GroupState::isSetBulbMode() const { return _isSetBulbMode; }
  87. BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(_bulbMode); }
  88. void GroupState::setBulbMode(BulbMode bulbMode) {
  89. _isSetBulbMode = 1;
  90. _bulbMode = bulbMode;
  91. }
  92. void GroupState::patch(const JsonObject& state) {
  93. if (state.containsKey("state")) {
  94. setState(state["state"] == "ON" ? ON : OFF);
  95. }
  96. if (state.containsKey("brightness")) {
  97. setBrightness(Units::rescale(state["brightness"], 100, 255));
  98. }
  99. if (state.containsKey("hue")) {
  100. setHue(Units::rescale<uint8_t, uint16_t>(state["hue"], 255, 360));
  101. setBulbMode(BULB_MODE_COLOR);
  102. }
  103. if (state.containsKey("saturation")) {
  104. setSaturation(state["saturation"]);
  105. }
  106. if (state.containsKey("mode")) {
  107. setMode(state["mode"]);
  108. setBulbMode(BULB_MODE_SCENE);
  109. }
  110. if (state.containsKey("color_temp")) {
  111. setKelvin(Units::miredsToWhiteVal(state["color_temp"], 100));
  112. setBulbMode(BULB_MODE_WHITE);
  113. }
  114. if (state.containsKey("command")) {
  115. const String& command = state["command"];
  116. if (command == "white_mode") {
  117. setBulbMode(BULB_MODE_WHITE);
  118. } else if (command == "night_mode") {
  119. setBulbMode(BULB_MODE_NIGHT);
  120. }
  121. }
  122. }
  123. void GroupState::applyState(JsonObject& state) {
  124. if (_isSetState) {
  125. state["state"] = getState() == ON ? "ON" : "OFF";
  126. }
  127. if (_isSetBrightness) {
  128. state["brightness"] = Units::rescale(getBrightness(), 255, 100);
  129. }
  130. if (_isSetBulbMode) {
  131. state["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  132. if (getBulbMode() == BULB_MODE_COLOR) {
  133. if (_isSetHue) {
  134. state["hue"] = Units::rescale<uint8_t, uint16_t>(getHue(), 360, 255);
  135. }
  136. if (_isSetSaturation) {
  137. state["saturation"] = getSaturation();
  138. }
  139. } else if (getBulbMode() == BULB_MODE_SCENE) {
  140. if (_isSetMode) {
  141. state["mode"] = getMode();
  142. }
  143. } else if (getBulbMode() == BULB_MODE_WHITE) {
  144. if (_isSetKelvin) {
  145. state["color_temp"] = Units::whiteValToMireds(getKelvin(), 100);
  146. }
  147. }
  148. }
  149. }