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.

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