Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.8 KiB

6 years ago
6 years ago
  1. /*
  2. DOMOTICZ MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DOMOTICZ_SUPPORT
  6. #include <ArduinoJson.h>
  7. bool _dcz_enabled = false;
  8. //------------------------------------------------------------------------------
  9. // Private methods
  10. //------------------------------------------------------------------------------
  11. unsigned char _domoticzRelay(unsigned int idx) {
  12. for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
  13. if (domoticzIdx(relayID) == idx) {
  14. return relayID;
  15. }
  16. }
  17. return -1;
  18. }
  19. void _domoticzMqttSubscribe(bool value) {
  20. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  21. if (value) {
  22. mqttSubscribeRaw(dczTopicOut.c_str());
  23. } else {
  24. mqttUnsubscribeRaw(dczTopicOut.c_str());
  25. }
  26. }
  27. void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  28. if (!_dcz_enabled) return;
  29. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  30. if (type == MQTT_CONNECT_EVENT) {
  31. // Subscribe to domoticz action topics
  32. mqttSubscribeRaw(dczTopicOut.c_str());
  33. // Send relays state on connection
  34. domoticzSendRelays();
  35. }
  36. if (type == MQTT_MESSAGE_EVENT) {
  37. // Check topic
  38. if (dczTopicOut.equals(topic)) {
  39. // Parse response
  40. DynamicJsonBuffer jsonBuffer;
  41. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  42. if (!root.success()) {
  43. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  44. return;
  45. }
  46. // IDX
  47. unsigned int idx = root["idx"];
  48. String stype = root["stype"];
  49. if (
  50. (stype.equals("RGB") || stype.equals("RGBW") || stype.equals("RGBWW"))
  51. && lightHasColor()
  52. ) {
  53. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  54. unsigned int cmode = root["Color"]["m"];
  55. unsigned int cval;
  56. if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h
  57. // RED
  58. cval = root["Color"]["r"];
  59. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received RED value %u for IDX %u\n"), cval, idx);
  60. lightChannel(0, cval);
  61. // GREEN
  62. cval = root["Color"]["g"];
  63. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received GREEN value %u for IDX %u\n"), cval, idx);
  64. lightChannel(1, cval);
  65. // BLUE
  66. cval = root["Color"]["b"];
  67. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BLUE value %u for IDX %u\n"), cval, idx);
  68. lightChannel(2, cval);
  69. // WARM WHITE or MONOCHROME WHITE if supported
  70. if (lightChannels() > 3) {
  71. cval = root["Color"]["ww"];
  72. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received WARM WHITE value %u for IDX %u\n"), cval, idx);
  73. lightChannel(3, cval);
  74. }
  75. // COLD WHITE if supported
  76. if (lightChannels() > 4) {
  77. cval = root["Color"]["cw"];
  78. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received COLD WHITE value %u for IDX %u\n"), cval, idx);
  79. lightChannel(4, cval);
  80. }
  81. unsigned int brightness = root["Level"];
  82. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BRIGHTNESS value %u for IDX %u\n"), brightness, idx);
  83. unsigned int br = (brightness / 100.0) * LIGHT_MAX_BRIGHTNESS;
  84. DEBUG_MSG_P(PSTR("[DOMOTICZ] Calculated BRIGHTNESS value %u for IDX %u\n"), br, idx);
  85. lightBrightness(br); // domoticz uses 100 as maximum value while we're using LIGHT_MAX_BRIGHTNESS
  86. // update lights
  87. lightUpdate(true, mqttForward());
  88. }
  89. unsigned char relayID = _domoticzRelay(idx);
  90. if (relayID >= 0) {
  91. unsigned char value = root["nvalue"];
  92. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  93. relayStatus(relayID, value > 0);
  94. }
  95. } else {
  96. unsigned char relayID = _domoticzRelay(idx);
  97. if (relayID >= 0) {
  98. unsigned char value = root["nvalue"];
  99. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  100. relayStatus(relayID, value == 1);
  101. }
  102. }
  103. }
  104. }
  105. };
  106. #if WEB_SUPPORT
  107. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  108. return (strncmp(key, "dcz", 3) == 0);
  109. }
  110. void _domoticzWebSocketOnSend(JsonObject& root) {
  111. root["dczVisible"] = 1;
  112. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  113. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  114. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  115. JsonArray& relays = root.createNestedArray("dczRelays");
  116. for (unsigned char i=0; i<relayCount(); i++) {
  117. relays.add(domoticzIdx(i));
  118. }
  119. #if SENSOR_SUPPORT
  120. JsonArray& list = root.createNestedArray("dczMagnitudes");
  121. for (byte i=0; i<magnitudeCount(); i++) {
  122. JsonObject& element = list.createNestedObject();
  123. element["name"] = magnitudeName(i);
  124. element["type"] = magnitudeType(i);
  125. element["index"] = magnitudeIndex(i);
  126. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  127. }
  128. #endif
  129. }
  130. #endif // WEB_SUPPORT
  131. void _domoticzConfigure() {
  132. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  133. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  134. _dcz_enabled = enabled;
  135. }
  136. //------------------------------------------------------------------------------
  137. // Public API
  138. //------------------------------------------------------------------------------
  139. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  140. if (!_dcz_enabled) return;
  141. unsigned int idx = getSetting(key).toInt();
  142. if (idx > 0) {
  143. char payload[128];
  144. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  145. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  146. }
  147. }
  148. template<typename T> void domoticzSend(const char * key, T nvalue) {
  149. domoticzSend(key, nvalue, "");
  150. }
  151. void domoticzSendRelay(unsigned char relayID) {
  152. if (!_dcz_enabled) return;
  153. char buffer[15];
  154. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  155. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  156. }
  157. void domoticzSendRelays() {
  158. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  159. domoticzSendRelay(relayID);
  160. }
  161. }
  162. unsigned int domoticzIdx(unsigned char relayID) {
  163. char buffer[15];
  164. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  165. return getSetting(buffer).toInt();
  166. }
  167. void domoticzSetup() {
  168. _domoticzConfigure();
  169. #if WEB_SUPPORT
  170. wsOnSendRegister(_domoticzWebSocketOnSend);
  171. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  172. #endif
  173. // Callbacks
  174. mqttRegister(_domoticzMqtt);
  175. espurnaRegisterReload(_domoticzConfigure);
  176. }
  177. bool domoticzEnabled() {
  178. return _dcz_enabled;
  179. }
  180. #endif