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.

272 lines
7.3 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. #include "light.h"
  37. void _domoticzLight(unsigned int idx, const JsonObject& root) {
  38. if (!lightHasColor()) return;
  39. JsonObject& color = root["Color"];
  40. if (!color.success()) return;
  41. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  42. unsigned int cmode = color["m"];
  43. if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h
  44. lightChannel(0, color["r"]);
  45. lightChannel(1, color["g"]);
  46. lightChannel(2, color["b"]);
  47. // WARM WHITE (or MONOCHROME WHITE) and COLD WHITE are always sent.
  48. // Apply only when supported.
  49. if (lightChannels() > 3) {
  50. lightChannel(3, color["ww"]);
  51. }
  52. if (lightChannels() > 4) {
  53. lightChannel(4, color["cw"]);
  54. }
  55. // domoticz uses 100 as maximum value while we're using Light::BRIGHTNESS_MAX (unsigned char)
  56. unsigned char brightness = (root["Level"].as<uint8_t>() / 100.0) * Light::BRIGHTNESS_MAX;
  57. lightBrightness(brightness);
  58. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u brightness:%u for IDX %u\n"),
  59. color["r"].as<uint8_t>(),
  60. color["g"].as<uint8_t>(),
  61. color["b"].as<uint8_t>(),
  62. color["ww"].as<uint8_t>(),
  63. color["cw"].as<uint8_t>(),
  64. brightness,
  65. idx
  66. );
  67. lightUpdate(true, mqttForward());
  68. }
  69. }
  70. #endif
  71. void _domoticzMqtt(unsigned int type, const char * topic, char * payload) {
  72. if (!_dcz_enabled) return;
  73. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  74. if (type == MQTT_CONNECT_EVENT) {
  75. // Subscribe to domoticz action topics
  76. mqttSubscribeRaw(dczTopicOut.c_str());
  77. // Send relays state on connection
  78. domoticzSendRelays();
  79. }
  80. if (type == MQTT_MESSAGE_EVENT) {
  81. // Check topic
  82. if (dczTopicOut.equals(topic)) {
  83. // Parse response
  84. DynamicJsonBuffer jsonBuffer(1024);
  85. JsonObject& root = jsonBuffer.parseObject(payload);
  86. if (!root.success()) {
  87. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  88. return;
  89. }
  90. // IDX
  91. unsigned int idx = root["idx"];
  92. String stype = root["stype"];
  93. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  94. if (stype.startsWith("RGB") && (domoticzIdx(0) == idx)) {
  95. _domoticzLight(idx, root);
  96. }
  97. #endif
  98. int relayID = _domoticzRelay(idx);
  99. if (relayID >= 0) {
  100. unsigned char value = root["nvalue"];
  101. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  102. _domoticzStatus(relayID, value >= 1);
  103. }
  104. }
  105. }
  106. };
  107. #if BROKER_SUPPORT
  108. void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
  109. // Only process status messages
  110. if (BROKER_MSG_TYPE_STATUS != type) return;
  111. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  112. bool status = atoi(payload) == 1;
  113. if (_domoticzStatus(id) == status) return;
  114. _dcz_relay_state[id] = status;
  115. domoticzSendRelay(id, status);
  116. }
  117. }
  118. #endif // BROKER_SUPPORT
  119. #if WEB_SUPPORT
  120. bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  121. return (strncmp(key, "dcz", 3) == 0);
  122. }
  123. void _domoticzWebSocketOnVisible(JsonObject& root) {
  124. root["dczVisible"] = static_cast<unsigned char>(haveRelaysOrSensors());
  125. }
  126. void _domoticzWebSocketOnConnected(JsonObject& root) {
  127. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  128. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  129. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  130. JsonArray& relays = root.createNestedArray("dczRelays");
  131. for (unsigned char i=0; i<relayCount(); i++) {
  132. relays.add(domoticzIdx(i));
  133. }
  134. #if SENSOR_SUPPORT
  135. _sensorWebSocketMagnitudes(root, "dcz");
  136. #endif
  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. wsRegister()
  183. .onVisible(_domoticzWebSocketOnVisible)
  184. .onConnected(_domoticzWebSocketOnConnected)
  185. .onKeyCheck(_domoticzWebSocketOnKeyCheck);
  186. #endif
  187. #if BROKER_SUPPORT
  188. brokerRegister(_domoticzBrokerCallback);
  189. #endif
  190. // Callbacks
  191. mqttRegister(_domoticzMqtt);
  192. espurnaRegisterReload(_domoticzConfigure);
  193. }
  194. bool domoticzEnabled() {
  195. return _dcz_enabled;
  196. }
  197. #endif