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.

283 lines
7.7 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. // for ColorMode... see:
  42. // https://github.com/domoticz/domoticz/blob/development/hardware/ColorSwitch.h
  43. // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Set_a_light_to_a_certain_color_or_color_temperature
  44. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u t:%u brightness:%u for IDX %u\n"),
  45. color["r"].as<unsigned char>(),
  46. color["g"].as<unsigned char>(),
  47. color["b"].as<unsigned char>(),
  48. color["ww"].as<unsigned char>(),
  49. color["cw"].as<unsigned char>(),
  50. color["t"].as<unsigned char>(),
  51. color["Level"].as<unsigned char>(),
  52. idx
  53. );
  54. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  55. unsigned int cmode = color["m"];
  56. if (cmode == 2) { // ColorModeWhite - WW,CW,temperature (t unused for now)
  57. if (lightChannels() < 2) return;
  58. lightChannel(0, color["ww"]);
  59. lightChannel(1, color["cw"]);
  60. } else if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom
  61. if (lightChannels() < 3) return;
  62. lightChannel(0, color["r"]);
  63. lightChannel(1, color["g"]);
  64. lightChannel(2, color["b"]);
  65. // WARM WHITE (or MONOCHROME WHITE) and COLD WHITE are always sent.
  66. // Apply only when supported.
  67. if (lightChannels() > 3) {
  68. lightChannel(3, color["ww"]);
  69. }
  70. if (lightChannels() > 4) {
  71. lightChannel(4, color["cw"]);
  72. }
  73. }
  74. // domoticz uses 100 as maximum value while we're using Light::BRIGHTNESS_MAX (unsigned char)
  75. lightBrightness((root["Level"].as<unsigned char>() / 100.0) * Light::BRIGHTNESS_MAX);
  76. lightUpdate(true, mqttForward());
  77. }
  78. #endif
  79. void _domoticzMqtt(unsigned int type, const char * topic, char * payload) {
  80. if (!_dcz_enabled) return;
  81. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  82. if (type == MQTT_CONNECT_EVENT) {
  83. // Subscribe to domoticz action topics
  84. mqttSubscribeRaw(dczTopicOut.c_str());
  85. // Send relays state on connection
  86. domoticzSendRelays();
  87. }
  88. if (type == MQTT_MESSAGE_EVENT) {
  89. // Check topic
  90. if (dczTopicOut.equals(topic)) {
  91. // Parse response
  92. DynamicJsonBuffer jsonBuffer(1024);
  93. JsonObject& root = jsonBuffer.parseObject(payload);
  94. if (!root.success()) {
  95. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  96. return;
  97. }
  98. // IDX
  99. unsigned int idx = root["idx"];
  100. String stype = root["stype"];
  101. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  102. if (stype.startsWith("RGB") && (domoticzIdx(0) == idx)) {
  103. _domoticzLight(idx, root);
  104. }
  105. #endif
  106. int relayID = _domoticzRelay(idx);
  107. if (relayID >= 0) {
  108. unsigned char value = root["nvalue"];
  109. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  110. _domoticzStatus(relayID, value >= 1);
  111. }
  112. }
  113. }
  114. };
  115. #if BROKER_SUPPORT
  116. void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
  117. // Only process status messages
  118. if (BROKER_MSG_TYPE_STATUS != type) return;
  119. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  120. bool status = atoi(payload) == 1;
  121. if (_domoticzStatus(id) == status) return;
  122. _dcz_relay_state[id] = status;
  123. domoticzSendRelay(id, status);
  124. }
  125. }
  126. #endif // BROKER_SUPPORT
  127. #if WEB_SUPPORT
  128. bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  129. return (strncmp(key, "dcz", 3) == 0);
  130. }
  131. void _domoticzWebSocketOnVisible(JsonObject& root) {
  132. root["dczVisible"] = static_cast<unsigned char>(haveRelaysOrSensors());
  133. }
  134. void _domoticzWebSocketOnConnected(JsonObject& root) {
  135. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  136. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  137. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  138. JsonArray& relays = root.createNestedArray("dczRelays");
  139. for (unsigned char i=0; i<relayCount(); i++) {
  140. relays.add(domoticzIdx(i));
  141. }
  142. #if SENSOR_SUPPORT
  143. _sensorWebSocketMagnitudes(root, "dcz");
  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_relay_state.reserve(relayCount());
  151. for (size_t n = 0; n < relayCount(); ++n) {
  152. _dcz_relay_state[n] = relayStatus(n);
  153. }
  154. _dcz_enabled = enabled;
  155. }
  156. //------------------------------------------------------------------------------
  157. // Public API
  158. //------------------------------------------------------------------------------
  159. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  160. if (!_dcz_enabled) return;
  161. unsigned int idx = getSetting(key).toInt();
  162. if (idx > 0) {
  163. char payload[128];
  164. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  165. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  166. }
  167. }
  168. template<typename T> void domoticzSend(const char * key, T nvalue) {
  169. domoticzSend(key, nvalue, "");
  170. }
  171. void domoticzSendRelay(unsigned char relayID, bool status) {
  172. if (!_dcz_enabled) return;
  173. char buffer[15];
  174. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  175. domoticzSend(buffer, status ? "1" : "0");
  176. }
  177. void domoticzSendRelays() {
  178. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  179. domoticzSendRelay(relayID, relayStatus(relayID));
  180. }
  181. }
  182. unsigned int domoticzIdx(unsigned char relayID) {
  183. char buffer[15];
  184. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  185. return getSetting(buffer).toInt();
  186. }
  187. void domoticzSetup() {
  188. _domoticzConfigure();
  189. #if WEB_SUPPORT
  190. wsRegister()
  191. .onVisible(_domoticzWebSocketOnVisible)
  192. .onConnected(_domoticzWebSocketOnConnected)
  193. .onKeyCheck(_domoticzWebSocketOnKeyCheck);
  194. #endif
  195. #if BROKER_SUPPORT
  196. brokerRegister(_domoticzBrokerCallback);
  197. #endif
  198. // Callbacks
  199. mqttRegister(_domoticzMqtt);
  200. espurnaRegisterReload(_domoticzConfigure);
  201. }
  202. bool domoticzEnabled() {
  203. return _dcz_enabled;
  204. }
  205. #endif