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.

346 lines
10 KiB

providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
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. #include "domoticz.h"
  6. #if DOMOTICZ_SUPPORT
  7. #include "broker.h"
  8. #include "light.h"
  9. #include "mqtt.h"
  10. #include "relay.h"
  11. #include "rpc.h"
  12. #include "sensor.h"
  13. #include "ws.h"
  14. bool _dcz_enabled = false;
  15. std::bitset<RelaysMax> _dcz_relay_state;
  16. //------------------------------------------------------------------------------
  17. // Private methods
  18. //------------------------------------------------------------------------------
  19. unsigned int _domoticzIdx(unsigned char relayID, unsigned int defaultValue = 0) {
  20. return getSetting({"dczRelayIdx", relayID}, defaultValue);
  21. }
  22. int _domoticzRelay(unsigned int idx) {
  23. for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
  24. if (_domoticzIdx(relayID) == idx) {
  25. return relayID;
  26. }
  27. }
  28. return -1;
  29. }
  30. void _domoticzMqttSubscribe(bool value) {
  31. const String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  32. if (value) {
  33. mqttSubscribeRaw(dczTopicOut.c_str());
  34. } else {
  35. mqttUnsubscribeRaw(dczTopicOut.c_str());
  36. }
  37. }
  38. bool _domoticzStatus(unsigned char id) {
  39. return _dcz_relay_state[id];
  40. }
  41. void _domoticzStatus(unsigned char id, bool status) {
  42. _dcz_relay_state[id] = status;
  43. relayStatus(id, status);
  44. }
  45. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  46. #include "light.h"
  47. void _domoticzLight(unsigned int idx, const JsonObject& root) {
  48. JsonObject& color = root["Color"];
  49. if (color.success()) {
  50. // for ColorMode... see:
  51. // https://github.com/domoticz/domoticz/blob/development/hardware/ColorSwitch.h
  52. // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Set_a_light_to_a_certain_color_or_color_temperature
  53. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u t:%u brightness:%u for IDX %u\n"),
  54. color["r"].as<unsigned char>(),
  55. color["g"].as<unsigned char>(),
  56. color["b"].as<unsigned char>(),
  57. color["ww"].as<unsigned char>(),
  58. color["cw"].as<unsigned char>(),
  59. color["t"].as<unsigned char>(),
  60. color["Level"].as<unsigned char>(),
  61. idx
  62. );
  63. // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h):
  64. unsigned int cmode = color["m"];
  65. if (cmode == 2) { // ColorModeWhite - WW,CW,temperature (t unused for now)
  66. if (lightChannels() < 2) return;
  67. lightChannel(0, color["ww"]);
  68. lightChannel(1, color["cw"]);
  69. } else if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom
  70. if (lightChannels() < 3) return;
  71. lightChannel(0, color["r"]);
  72. lightChannel(1, color["g"]);
  73. lightChannel(2, color["b"]);
  74. // WARM WHITE (or MONOCHROME WHITE) and COLD WHITE are always sent.
  75. // Apply only when supported.
  76. if (lightChannels() > 3) {
  77. lightChannel(3, color["ww"]);
  78. }
  79. if (lightChannels() > 4) {
  80. lightChannel(4, color["cw"]);
  81. }
  82. }
  83. }
  84. // domoticz uses 100 as maximum value while we're using Light::BRIGHTNESS_MAX (default 255)
  85. lightBrightness((root["Level"].as<long>() / 100l) * Light::BRIGHTNESS_MAX);
  86. lightUpdate();
  87. }
  88. #endif
  89. void _domoticzMqtt(unsigned int type, const char * topic, char * payload) {
  90. if (!_dcz_enabled) return;
  91. const String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  92. if (type == MQTT_CONNECT_EVENT) {
  93. // Subscribe to domoticz action topics
  94. mqttSubscribeRaw(dczTopicOut.c_str());
  95. // Send relays state on connection
  96. #if RELAY_SUPPORT
  97. domoticzSendRelays();
  98. #endif
  99. }
  100. if (type == MQTT_MESSAGE_EVENT) {
  101. // Check topic
  102. if (dczTopicOut.equals(topic)) {
  103. // Parse response
  104. DynamicJsonBuffer jsonBuffer(1024);
  105. JsonObject& root = jsonBuffer.parseObject(payload);
  106. if (!root.success()) {
  107. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  108. return;
  109. }
  110. // IDX
  111. unsigned int idx = root["idx"];
  112. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  113. String stype = root["stype"];
  114. String switchType = root["switchType"];
  115. if ((_domoticzIdx(0) == idx) && (stype.startsWith("RGB") || (switchType.equals("Dimmer")))) {
  116. _domoticzLight(idx, root);
  117. }
  118. #endif
  119. int relayID = _domoticzRelay(idx);
  120. if (relayID >= 0) {
  121. unsigned char value = root["nvalue"];
  122. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  123. _domoticzStatus(relayID, value >= 1);
  124. }
  125. }
  126. }
  127. };
  128. void _domoticzRelayConfigure(size_t size) {
  129. for (size_t n = 0; n < size; ++n) {
  130. _dcz_relay_state[n] = relayStatus(n);
  131. }
  132. }
  133. void _domoticzConfigure() {
  134. const bool enabled = getSetting("dczEnabled", 1 == DOMOTICZ_ENABLED);
  135. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  136. #if RELAY_SUPPORT
  137. _domoticzRelayConfigure(relayCount());
  138. #endif
  139. _dcz_enabled = enabled;
  140. }
  141. void _domoticzConfigCallback(const String& key, const String& value) {
  142. if (key.equals("relayDummy")) {
  143. _domoticzRelayConfigure(value.toInt());
  144. return;
  145. }
  146. }
  147. void _domoticzBrokerCallback(const String& topic, unsigned char id, unsigned int value) {
  148. // Only process status messages for switches
  149. if (!topic.equals(MQTT_TOPIC_RELAY)) {
  150. return;
  151. }
  152. if (_domoticzStatus(id) == value) return;
  153. _dcz_relay_state[id] = value;
  154. domoticzSendRelay(id, value);
  155. }
  156. #if SENSOR_SUPPORT
  157. void domoticzSendMagnitude(unsigned char type, unsigned char index, double value, const char* buffer) {
  158. if (!_dcz_enabled) return;
  159. char key[15];
  160. snprintf_P(key, sizeof(key), PSTR("dczMagnitude%d"), index);
  161. // Domoticz expects some additional data, dashboard might break otherwise.
  162. // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Barometer
  163. // TODO: Must send 'forecast' data. Default is last 3 hours:
  164. // https://github.com/domoticz/domoticz/blob/6027b1d9e3b6588a901de42d82f3a6baf1374cd1/hardware/I2C.cpp#L1092-L1193
  165. // 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.)
  166. if (MAGNITUDE_PRESSURE == type) {
  167. String svalue = buffer;
  168. svalue += ";-1";
  169. domoticzSend(key, 0, svalue.c_str());
  170. // Special case to allow us to use it with switches directly
  171. } else if (MAGNITUDE_DIGITAL == type) {
  172. int nvalue = (buffer[0] >= 48) ? (buffer[0] - 48) : 0;
  173. domoticzSend(key, nvalue, buffer);
  174. // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Humidity
  175. // nvalue contains HUM (relative humidity)
  176. // svalue contains HUM_STAT, one of consts below
  177. } else if (MAGNITUDE_HUMIDITY == type) {
  178. const char status = 48 + (
  179. (value > 70) ? HUMIDITY_WET :
  180. (value > 45) ? HUMIDITY_COMFORTABLE :
  181. (value > 30) ? HUMIDITY_NORMAL :
  182. HUMIDITY_DRY
  183. );
  184. char svalue[2] = {status, '\0'};
  185. domoticzSend(key, static_cast<int>(value), svalue);
  186. // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Air_quality
  187. // nvalue contains the ppm
  188. // svalue is not used (?)
  189. } else if (MAGNITUDE_CO2 == type) {
  190. domoticzSend(key, static_cast<int>(value), "");
  191. // Otherwise, send char string (nvalue is only for integers)
  192. } else {
  193. domoticzSend(key, 0, buffer);
  194. }
  195. }
  196. #endif // SENSOR_SUPPORT
  197. #if WEB_SUPPORT
  198. bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  199. return (strncmp(key, "dcz", 3) == 0);
  200. }
  201. void _domoticzWebSocketOnVisible(JsonObject& root) {
  202. root["dczVisible"] = static_cast<unsigned char>(haveRelaysOrSensors());
  203. }
  204. void _domoticzWebSocketOnConnected(JsonObject& root) {
  205. root["dczEnabled"] = getSetting("dczEnabled", 1 == DOMOTICZ_ENABLED);
  206. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  207. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  208. JsonArray& relays = root.createNestedArray("dczRelays");
  209. for (unsigned char i=0; i<relayCount(); i++) {
  210. relays.add(_domoticzIdx(i));
  211. }
  212. #if SENSOR_SUPPORT
  213. sensorWebSocketMagnitudes(root, "dcz");
  214. #endif
  215. }
  216. #endif // WEB_SUPPORT
  217. //------------------------------------------------------------------------------
  218. // Public API
  219. //------------------------------------------------------------------------------
  220. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  221. if (!_dcz_enabled) return;
  222. const auto idx = getSetting(key, 0);
  223. if (idx > 0) {
  224. char payload[128];
  225. snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  226. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  227. }
  228. }
  229. template<typename T> void domoticzSend(const char * key, T nvalue) {
  230. domoticzSend(key, nvalue, "");
  231. }
  232. void domoticzSendRelay(unsigned char relayID, bool status) {
  233. if (!_dcz_enabled) return;
  234. char buffer[15];
  235. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  236. domoticzSend(buffer, status ? "1" : "0");
  237. }
  238. void domoticzSendRelays() {
  239. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  240. domoticzSendRelay(relayID, relayStatus(relayID));
  241. }
  242. }
  243. void domoticzSetup() {
  244. _domoticzConfigure();
  245. #if WEB_SUPPORT
  246. wsRegister()
  247. .onVisible(_domoticzWebSocketOnVisible)
  248. .onConnected(_domoticzWebSocketOnConnected)
  249. .onKeyCheck(_domoticzWebSocketOnKeyCheck);
  250. #endif
  251. StatusBroker::Register(_domoticzBrokerCallback);
  252. ConfigBroker::Register(_domoticzConfigCallback);
  253. // Callbacks
  254. mqttRegister(_domoticzMqtt);
  255. espurnaRegisterReload(_domoticzConfigure);
  256. }
  257. bool domoticzEnabled() {
  258. return _dcz_enabled;
  259. }
  260. #endif