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.

269 lines
7.2 KiB

6 years ago
6 years ago
  1. /*
  2. DOMOTICZ MODULE
  3. Copyright (C) 2016-2019 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. std::vector<bool> _dcz_relay_state;
  9. //------------------------------------------------------------------------------
  10. // Private methods
  11. //------------------------------------------------------------------------------
  12. int _domoticzRelay(unsigned int idx) {
  13. for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
  14. if (domoticzIdx(relayID) == idx) {
  15. return relayID;
  16. }
  17. }
  18. return -1;
  19. }
  20. void _domoticzMqttSubscribe(bool value) {
  21. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  22. if (value) {
  23. mqttSubscribeRaw(dczTopicOut.c_str());
  24. } else {
  25. mqttUnsubscribeRaw(dczTopicOut.c_str());
  26. }
  27. }
  28. bool _domoticzStatus(unsigned char id) {
  29. return _dcz_relay_state[id];
  30. }
  31. void _domoticzStatus(unsigned char id, bool status) {
  32. _dcz_relay_state[id] = status;
  33. relayStatus(id, status);
  34. }
  35. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  36. void _domoticzLight(unsigned int idx, const JsonObject& root) {
  37. if (!lightHasColor()) return;
  38. JsonObject& color = root["Color"];
  39. if (!color.success()) return;
  40. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  41. unsigned int cmode = color["m"];
  42. if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h
  43. lightChannel(0, color["r"]);
  44. lightChannel(1, color["g"]);
  45. lightChannel(2, color["b"]);
  46. // WARM WHITE (or MONOCHROME WHITE) and COLD WHITE are always sent.
  47. // Apply only when supported.
  48. if (lightChannels() > 3) {
  49. lightChannel(3, color["ww"]);
  50. }
  51. if (lightChannels() > 4) {
  52. lightChannel(4, color["cw"]);
  53. }
  54. // domoticz uses 100 as maximum value while we're using LIGHT_MAX_BRIGHTNESS
  55. unsigned int brightness = (root["Level"].as<uint8_t>() / 100.0) * LIGHT_MAX_BRIGHTNESS;
  56. lightBrightness(brightness);
  57. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u brightness:%u for IDX %u\n"),
  58. color["r"].as<uint8_t>(),
  59. color["g"].as<uint8_t>(),
  60. color["b"].as<uint8_t>(),
  61. color["ww"].as<uint8_t>(),
  62. color["cw"].as<uint8_t>(),
  63. brightness,
  64. idx
  65. );
  66. lightUpdate(true, mqttForward());
  67. }
  68. }
  69. #endif
  70. void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  71. if (!_dcz_enabled) return;
  72. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  73. if (type == MQTT_CONNECT_EVENT) {
  74. // Subscribe to domoticz action topics
  75. mqttSubscribeRaw(dczTopicOut.c_str());
  76. // Send relays state on connection
  77. domoticzSendRelays();
  78. }
  79. if (type == MQTT_MESSAGE_EVENT) {
  80. // Check topic
  81. if (dczTopicOut.equals(topic)) {
  82. // Parse response
  83. DynamicJsonBuffer jsonBuffer;
  84. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  85. if (!root.success()) {
  86. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  87. return;
  88. }
  89. // IDX
  90. unsigned int idx = root["idx"];
  91. String stype = root["stype"];
  92. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  93. if (stype.startsWith("RGB") && (domoticzIdx(0) == idx)) {
  94. _domoticzLight(idx, root);
  95. }
  96. #endif
  97. int relayID = _domoticzRelay(idx);
  98. if (relayID >= 0) {
  99. unsigned char value = root["nvalue"];
  100. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  101. _domoticzStatus(relayID, value >= 1);
  102. }
  103. }
  104. }
  105. };
  106. #if BROKER_SUPPORT
  107. void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
  108. // Only process status messages
  109. if (BROKER_MSG_TYPE_STATUS != type) return;
  110. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  111. bool status = atoi(payload) == 1;
  112. if (_domoticzStatus(id) == status) return;
  113. _dcz_relay_state[id] = status;
  114. domoticzSendRelay(id, status);
  115. }
  116. }
  117. #endif // BROKER_SUPPORT
  118. #if WEB_SUPPORT
  119. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  120. return (strncmp(key, "dcz", 3) == 0);
  121. }
  122. void _domoticzWebSocketOnSend(JsonObject& root) {
  123. unsigned char visible = 0;
  124. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  125. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  126. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  127. JsonArray& relays = root.createNestedArray("dczRelays");
  128. for (unsigned char i=0; i<relayCount(); i++) {
  129. relays.add(domoticzIdx(i));
  130. }
  131. visible = (relayCount() > 0);
  132. #if SENSOR_SUPPORT
  133. _sensorWebSocketMagnitudes(root, "dcz");
  134. visible = visible || (magnitudeCount() > 0);
  135. #endif
  136. root["dczVisible"] = visible;
  137. }
  138. #endif // WEB_SUPPORT
  139. void _domoticzConfigure() {
  140. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  141. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  142. _dcz_relay_state.reserve(relayCount());
  143. for (size_t n = 0; n < relayCount(); ++n) {
  144. _dcz_relay_state[n] = relayStatus(n);
  145. }
  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