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.

175 lines
4.8 KiB

6 years ago
6 years ago
  1. /*
  2. DOMOTICZ MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: dcz
  5. */
  6. #if DOMOTICZ_SUPPORT
  7. #include <ArduinoJson.h>
  8. bool _dcz_enabled = false;
  9. //------------------------------------------------------------------------------
  10. // Private methods
  11. //------------------------------------------------------------------------------
  12. unsigned char _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. void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  29. if (!_dcz_enabled) return;
  30. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  31. if (type == MQTT_CONNECT_EVENT) {
  32. // Subscribe to domoticz action topics
  33. mqttSubscribeRaw(dczTopicOut.c_str());
  34. // Send relays state on connection
  35. domoticzSendRelays();
  36. }
  37. if (type == MQTT_MESSAGE_EVENT) {
  38. // Check topic
  39. if (dczTopicOut.equals(topic)) {
  40. // Parse response
  41. DynamicJsonBuffer jsonBuffer;
  42. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  43. if (!root.success()) {
  44. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  45. return;
  46. }
  47. // IDX
  48. unsigned int idx = root["idx"];
  49. unsigned char relayID = _domoticzRelay(idx);
  50. if (relayID >= 0) {
  51. unsigned char value = root["nvalue"];
  52. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  53. relayStatus(relayID, value == 1);
  54. }
  55. }
  56. }
  57. };
  58. #if WEB_SUPPORT
  59. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  60. return (strncmp(key, "dcz", 3) == 0);
  61. }
  62. void _domoticzWebSocketOnSend(JsonObject& root) {
  63. root["dczVisible"] = 1;
  64. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  65. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  66. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  67. JsonArray& relays = root.createNestedArray("dczRelays");
  68. for (unsigned char i=0; i<relayCount(); i++) {
  69. relays.add(domoticzIdx(i));
  70. }
  71. #if SENSOR_SUPPORT
  72. JsonArray& list = root.createNestedArray("dczMagnitudes");
  73. for (byte i=0; i<magnitudeCount(); i++) {
  74. JsonObject& element = list.createNestedObject();
  75. element["name"] = magnitudeName(i);
  76. element["type"] = magnitudeType(i);
  77. element["index"] = magnitudeIndex(i);
  78. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  79. }
  80. #endif
  81. }
  82. #endif // WEB_SUPPORT
  83. void _domoticzConfigure() {
  84. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  85. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  86. _dcz_enabled = enabled;
  87. }
  88. //------------------------------------------------------------------------------
  89. // Public API
  90. //------------------------------------------------------------------------------
  91. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  92. if (!_dcz_enabled) return;
  93. unsigned int idx = getSetting(key).toInt();
  94. if (idx > 0) {
  95. char payload[128];
  96. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  97. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  98. }
  99. }
  100. template<typename T> void domoticzSend(const char * key, T nvalue) {
  101. domoticzSend(key, nvalue, "");
  102. }
  103. void domoticzSendRelay(unsigned char relayID) {
  104. if (!_dcz_enabled) return;
  105. char buffer[15];
  106. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  107. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  108. }
  109. void domoticzSendRelays() {
  110. for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
  111. domoticzSendRelay(relayID);
  112. }
  113. }
  114. unsigned int domoticzIdx(unsigned char relayID) {
  115. char buffer[15];
  116. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  117. return getSetting(buffer).toInt();
  118. }
  119. void domoticzSetup() {
  120. _domoticzConfigure();
  121. #if WEB_SUPPORT
  122. wsOnSendRegister(_domoticzWebSocketOnSend);
  123. wsOnAfterParseRegister(_domoticzConfigure);
  124. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  125. #endif
  126. mqttRegister(_domoticzMqtt);
  127. }
  128. bool domoticzEnabled() {
  129. return _dcz_enabled;
  130. }
  131. #endif