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.

270 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, 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(1024);
  84. JsonObject& root = jsonBuffer.parseObject(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 _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  120. return (strncmp(key, "dcz", 3) == 0);
  121. }
  122. void _domoticzWebSocketOnVisible(JsonObject& root) {
  123. root["dczVisible"] = static_cast<unsigned char>(haveRelaysOrSensors());
  124. }
  125. void _domoticzWebSocketOnConnected(JsonObject& root) {
  126. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  127. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  128. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  129. JsonArray& relays = root.createNestedArray("dczRelays");
  130. for (unsigned char i=0; i<relayCount(); i++) {
  131. relays.add(domoticzIdx(i));
  132. }
  133. #if SENSOR_SUPPORT
  134. _sensorWebSocketMagnitudes(root, "dcz");
  135. #endif
  136. }
  137. #endif // WEB_SUPPORT
  138. void _domoticzConfigure() {
  139. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  140. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  141. _dcz_relay_state.reserve(relayCount());
  142. for (size_t n = 0; n < relayCount(); ++n) {
  143. _dcz_relay_state[n] = relayStatus(n);
  144. }
  145. _dcz_enabled = enabled;
  146. }
  147. //------------------------------------------------------------------------------
  148. // Public API
  149. //------------------------------------------------------------------------------
  150. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  151. if (!_dcz_enabled) return;
  152. unsigned int idx = getSetting(key).toInt();
  153. if (idx > 0) {
  154. char payload[128];
  155. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  156. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  157. }
  158. }
  159. template<typename T> void domoticzSend(const char * key, T nvalue) {
  160. domoticzSend(key, nvalue, "");
  161. }
  162. void domoticzSendRelay(unsigned char relayID, bool status) {
  163. if (!_dcz_enabled) return;
  164. char buffer[15];
  165. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  166. domoticzSend(buffer, status ? "1" : "0");
  167. }
  168. void domoticzSendRelays() {
  169. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  170. domoticzSendRelay(relayID, relayStatus(relayID));
  171. }
  172. }
  173. unsigned int domoticzIdx(unsigned char relayID) {
  174. char buffer[15];
  175. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  176. return getSetting(buffer).toInt();
  177. }
  178. void domoticzSetup() {
  179. _domoticzConfigure();
  180. #if WEB_SUPPORT
  181. wsRegister()
  182. .onVisible(_domoticzWebSocketOnVisible)
  183. .onConnected(_domoticzWebSocketOnConnected)
  184. .onKeyCheck(_domoticzWebSocketOnKeyCheck);
  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