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.

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