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.

259 lines
8.5 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 BROKER_SUPPORT
  112. void _domoticzBrokerCallback(const char * topic, unsigned char id, const char * payload) {
  113. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  114. unsigned char value = atoi(payload);
  115. domoticzSendRelay(id, value == 1);
  116. }
  117. }
  118. #endif // BROKER_SUPPORT
  119. #if WEB_SUPPORT
  120. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  121. return (strncmp(key, "dcz", 3) == 0);
  122. }
  123. void _domoticzWebSocketOnSend(JsonObject& root) {
  124. root["dczVisible"] = 1;
  125. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  126. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  127. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  128. JsonArray& relays = root.createNestedArray("dczRelays");
  129. for (unsigned char i=0; i<relayCount(); i++) {
  130. relays.add(domoticzIdx(i));
  131. }
  132. #if SENSOR_SUPPORT
  133. JsonArray& list = root.createNestedArray("dczMagnitudes");
  134. for (byte i=0; i<magnitudeCount(); i++) {
  135. JsonObject& element = list.createNestedObject();
  136. element["name"] = magnitudeName(i);
  137. element["type"] = magnitudeType(i);
  138. element["index"] = magnitudeIndex(i);
  139. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  140. }
  141. #endif
  142. }
  143. #endif // WEB_SUPPORT
  144. void _domoticzConfigure() {
  145. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  146. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  147. _dcz_enabled = enabled;
  148. }
  149. //------------------------------------------------------------------------------
  150. // Public API
  151. //------------------------------------------------------------------------------
  152. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  153. if (!_dcz_enabled) return;
  154. unsigned int idx = getSetting(key).toInt();
  155. if (idx > 0) {
  156. char payload[128];
  157. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  158. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  159. }
  160. }
  161. template<typename T> void domoticzSend(const char * key, T nvalue) {
  162. domoticzSend(key, nvalue, "");
  163. }
  164. void domoticzSendRelay(unsigned char relayID, bool status) {
  165. if (!_dcz_enabled) return;
  166. char buffer[15];
  167. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  168. domoticzSend(buffer, status ? "1" : "0");
  169. }
  170. void domoticzSendRelays() {
  171. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  172. domoticzSendRelay(relayID, relayStatus(relayID));
  173. }
  174. }
  175. unsigned int domoticzIdx(unsigned char relayID) {
  176. char buffer[15];
  177. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  178. return getSetting(buffer).toInt();
  179. }
  180. void domoticzSetup() {
  181. _domoticzConfigure();
  182. #if WEB_SUPPORT
  183. wsOnSendRegister(_domoticzWebSocketOnSend);
  184. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  185. #endif
  186. #if BROKER_SUPPORT
  187. brokerRegister(_domoticzBrokerCallback);
  188. #endif
  189. // Callbacks
  190. mqttRegister(_domoticzMqtt);
  191. espurnaRegisterReload(_domoticzConfigure);
  192. }
  193. bool domoticzEnabled() {
  194. return _dcz_enabled;
  195. }
  196. #endif