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.

265 lines
8.6 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. && 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. root["dczVisible"] = 1;
  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. #if SENSOR_SUPPORT
  136. JsonArray& list = root.createNestedArray("dczMagnitudes");
  137. for (byte i=0; i<magnitudeCount(); i++) {
  138. JsonObject& element = list.createNestedObject();
  139. element["name"] = magnitudeName(i);
  140. element["type"] = magnitudeType(i);
  141. element["index"] = magnitudeIndex(i);
  142. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  143. }
  144. #endif
  145. }
  146. #endif // WEB_SUPPORT
  147. void _domoticzConfigure() {
  148. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  149. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  150. _dcz_enabled = enabled;
  151. }
  152. //------------------------------------------------------------------------------
  153. // Public API
  154. //------------------------------------------------------------------------------
  155. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  156. if (!_dcz_enabled) return;
  157. unsigned int idx = getSetting(key).toInt();
  158. if (idx > 0) {
  159. char payload[128];
  160. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  161. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  162. }
  163. }
  164. template<typename T> void domoticzSend(const char * key, T nvalue) {
  165. domoticzSend(key, nvalue, "");
  166. }
  167. void domoticzSendRelay(unsigned char relayID, bool status) {
  168. if (!_dcz_enabled) return;
  169. char buffer[15];
  170. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  171. domoticzSend(buffer, status ? "1" : "0");
  172. }
  173. void domoticzSendRelays() {
  174. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  175. domoticzSendRelay(relayID, relayStatus(relayID));
  176. }
  177. }
  178. unsigned int domoticzIdx(unsigned char relayID) {
  179. char buffer[15];
  180. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  181. return getSetting(buffer).toInt();
  182. }
  183. void domoticzSetup() {
  184. _domoticzConfigure();
  185. #if WEB_SUPPORT
  186. wsOnSendRegister(_domoticzWebSocketOnSend);
  187. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  188. #endif
  189. #if BROKER_SUPPORT
  190. brokerRegister(_domoticzBrokerCallback);
  191. #endif
  192. // Callbacks
  193. mqttRegister(_domoticzMqtt);
  194. espurnaRegisterReload(_domoticzConfigure);
  195. }
  196. bool domoticzEnabled() {
  197. return _dcz_enabled;
  198. }
  199. #endif