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.

246 lines
8.1 KiB

7 years ago
7 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. ) {
  52. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  53. if (lightHasColor()) {
  54. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  55. unsigned int cmode = root["Color"]["m"];
  56. unsigned int cval;
  57. if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h
  58. // RED
  59. cval = root["Color"]["r"];
  60. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received RED value %u for IDX %u\n"), cval, idx);
  61. lightChannel(0, cval);
  62. // GREEN
  63. cval = root["Color"]["g"];
  64. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received GREEN value %u for IDX %u\n"), cval, idx);
  65. lightChannel(1, cval);
  66. // BLUE
  67. cval = root["Color"]["b"];
  68. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BLUE value %u for IDX %u\n"), cval, idx);
  69. lightChannel(2, cval);
  70. // WARM WHITE or MONOCHROME WHITE if supported
  71. if (lightChannels() > 3) {
  72. cval = root["Color"]["ww"];
  73. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received WARM WHITE value %u for IDX %u\n"), cval, idx);
  74. lightChannel(3, cval);
  75. }
  76. // COLD WHITE if supported
  77. if (lightChannels() > 4) {
  78. cval = root["Color"]["cw"];
  79. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received COLD WHITE value %u for IDX %u\n"), cval, idx);
  80. lightChannel(4, cval);
  81. }
  82. unsigned int brightness = root["Level"];
  83. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BRIGHTNESS value %u for IDX %u\n"), brightness, idx);
  84. unsigned int br = (brightness / 100.0) * LIGHT_MAX_BRIGHTNESS;
  85. DEBUG_MSG_P(PSTR("[DOMOTICZ] Calculated BRIGHTNESS value %u for IDX %u\n"), br, idx);
  86. lightBrightness(br); // domoticz uses 100 as maximum value while we're using LIGHT_MAX_BRIGHTNESS
  87. // update lights
  88. lightUpdate(true, mqttForward());
  89. }
  90. unsigned char relayID = _domoticzRelay(idx);
  91. if (relayID >= 0) {
  92. unsigned char value = root["nvalue"];
  93. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  94. relayStatus(relayID, value > 0);
  95. }
  96. }
  97. #else
  98. DEBUG_MSG_P(PSTR("[DOMOTICZ] ESPurna compiled without LIGHT_PROVIDER"));
  99. #endif
  100. } else {
  101. unsigned char relayID = _domoticzRelay(idx);
  102. if (relayID >= 0) {
  103. unsigned char value = root["nvalue"];
  104. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  105. relayStatus(relayID, value == 1);
  106. }
  107. }
  108. }
  109. }
  110. };
  111. #if WEB_SUPPORT
  112. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  113. return (strncmp(key, "dcz", 3) == 0);
  114. }
  115. void _domoticzWebSocketOnSend(JsonObject& root) {
  116. root["dczVisible"] = 1;
  117. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  118. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  119. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  120. JsonArray& relays = root.createNestedArray("dczRelays");
  121. for (unsigned char i=0; i<relayCount(); i++) {
  122. relays.add(domoticzIdx(i));
  123. }
  124. #if SENSOR_SUPPORT
  125. JsonArray& list = root.createNestedArray("dczMagnitudes");
  126. for (byte i=0; i<magnitudeCount(); i++) {
  127. JsonObject& element = list.createNestedObject();
  128. element["name"] = magnitudeName(i);
  129. element["type"] = magnitudeType(i);
  130. element["index"] = magnitudeIndex(i);
  131. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  132. }
  133. #endif
  134. }
  135. #endif // WEB_SUPPORT
  136. void _domoticzConfigure() {
  137. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  138. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  139. _dcz_enabled = enabled;
  140. }
  141. //------------------------------------------------------------------------------
  142. // Public API
  143. //------------------------------------------------------------------------------
  144. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  145. if (!_dcz_enabled) return;
  146. unsigned int idx = getSetting(key).toInt();
  147. if (idx > 0) {
  148. char payload[128];
  149. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  150. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  151. }
  152. }
  153. template<typename T> void domoticzSend(const char * key, T nvalue) {
  154. domoticzSend(key, nvalue, "");
  155. }
  156. void domoticzSendRelay(unsigned char relayID) {
  157. if (!_dcz_enabled) return;
  158. char buffer[15];
  159. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  160. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  161. }
  162. void domoticzSendRelays() {
  163. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  164. domoticzSendRelay(relayID);
  165. }
  166. }
  167. unsigned int domoticzIdx(unsigned char relayID) {
  168. char buffer[15];
  169. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  170. return getSetting(buffer).toInt();
  171. }
  172. void domoticzSetup() {
  173. _domoticzConfigure();
  174. #if WEB_SUPPORT
  175. wsOnSendRegister(_domoticzWebSocketOnSend);
  176. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  177. #endif
  178. // Callbacks
  179. mqttRegister(_domoticzMqtt);
  180. espurnaRegisterReload(_domoticzConfigure);
  181. }
  182. bool domoticzEnabled() {
  183. return _dcz_enabled;
  184. }
  185. #endif