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.

172 lines
4.8 KiB

  1. /*
  2. DOMOTICZ MODULE
  3. Copyright (C) 2016-2017 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. unsigned long _dcz_skip_time = 0;
  9. unsigned long _dcz_last_idx = 0;
  10. unsigned long _dcz_last_time = 0;
  11. //------------------------------------------------------------------------------
  12. // Private methods
  13. //------------------------------------------------------------------------------
  14. int _domoticzRelay(unsigned int idx) {
  15. for (int relayID=0; relayID<relayCount(); relayID++) {
  16. if (domoticzIdx(relayID) == idx) {
  17. return relayID;
  18. }
  19. }
  20. return -1;
  21. }
  22. bool _domoticzSkip(unsigned long idx) {
  23. if (idx == _dcz_last_idx && (millis() - _dcz_last_time < _dcz_skip_time)) return true;
  24. _dcz_last_idx = idx;
  25. _dcz_last_time = millis();
  26. return false;
  27. }
  28. void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  29. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  30. if (type == MQTT_CONNECT_EVENT) {
  31. mqttSubscribeRaw(dczTopicOut.c_str());
  32. }
  33. if (type == MQTT_MESSAGE_EVENT) {
  34. if (!_dcz_enabled) return;
  35. // Check topic
  36. if (dczTopicOut.equals(topic)) {
  37. // Parse response
  38. DynamicJsonBuffer jsonBuffer;
  39. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  40. if (!root.success()) {
  41. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  42. return;
  43. }
  44. // IDX
  45. unsigned long idx = root["idx"];
  46. int relayID = _domoticzRelay(idx);
  47. if (relayID >= 0) {
  48. // Skip message if recursive
  49. if (_domoticzSkip(idx)) return;
  50. unsigned long value = root["nvalue"];
  51. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
  52. relayStatus(relayID, value == 1);
  53. }
  54. }
  55. }
  56. };
  57. void _domoticzWebSocketOnSend(JsonObject& root) {
  58. root["dczVisible"] = 1;
  59. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  60. root["dczSkip"] = getSetting("dczSkip", DOMOTICZ_SKIP_TIME);
  61. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  62. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  63. JsonArray& dczRelayIdx = root.createNestedArray("dczRelayIdx");
  64. for (byte i=0; i<relayCount(); i++) {
  65. dczRelayIdx.add(domoticzIdx(i));
  66. }
  67. #if DHT_SUPPORT
  68. root["dczTmpIdx"] = getSetting("dczTmpIdx").toInt();
  69. root["dczHumIdx"] = getSetting("dczHumIdx").toInt();
  70. #endif
  71. #if DS18B20_SUPPORT
  72. root["dczTmpIdx"] = getSetting("dczTmpIdx").toInt();
  73. #endif
  74. #if ANALOG_SUPPORT
  75. root["dczAnaIdx"] = getSetting("dczAnaIdx").toInt();
  76. #endif
  77. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  78. root["dczPowIdx"] = getSetting("dczPowIdx").toInt();
  79. root["dczEnergyIdx"] = getSetting("dczEnergyIdx").toInt();
  80. root["dczCurrentIdx"] = getSetting("dczCurrentIdx").toInt();
  81. #if POWER_HAS_ACTIVE
  82. root["dczVoltIdx"] = getSetting("dczVoltIdx").toInt();
  83. #endif
  84. #endif
  85. }
  86. void _domoticzConfigure() {
  87. _dcz_enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  88. _dcz_skip_time = 1000 * getSetting("dczSkip", DOMOTICZ_SKIP_TIME).toInt();
  89. }
  90. //------------------------------------------------------------------------------
  91. // Public API
  92. //------------------------------------------------------------------------------
  93. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  94. if (!_dcz_enabled) return;
  95. unsigned int idx = getSetting(key).toInt();
  96. if (idx > 0) {
  97. // Skip message if recursive
  98. if (_domoticzSkip(idx)) return;
  99. char payload[128];
  100. snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  101. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  102. }
  103. }
  104. template<typename T> void domoticzSend(const char * key, T nvalue) {
  105. domoticzSend(key, nvalue, "");
  106. }
  107. void domoticzSendRelay(unsigned int relayID) {
  108. if (!_dcz_enabled) return;
  109. char buffer[15];
  110. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID);
  111. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  112. }
  113. int domoticzIdx(unsigned int relayID) {
  114. char buffer[15];
  115. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID);
  116. return getSetting(buffer).toInt();
  117. }
  118. void domoticzSetup() {
  119. _domoticzConfigure();
  120. #if WEB_SUPPORT
  121. wsOnSendRegister(_domoticzWebSocketOnSend);
  122. wsOnAfterParseRegister(_domoticzConfigure);
  123. #endif
  124. mqttRegister(_domoticzMqtt);
  125. }
  126. bool domoticzEnabled() {
  127. return _dcz_enabled;
  128. }
  129. #endif