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.

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