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.

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