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.

173 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 _domoticzMqttSubscribe(bool value) {
  29. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  30. if (value) {
  31. mqttSubscribeRaw(dczTopicOut.c_str());
  32. } else {
  33. mqttUnsubscribeRaw(dczTopicOut.c_str());
  34. }
  35. }
  36. void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  37. if (!_dcz_enabled) return;
  38. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  39. if (type == MQTT_CONNECT_EVENT) {
  40. mqttSubscribeRaw(dczTopicOut.c_str());
  41. }
  42. if (type == MQTT_MESSAGE_EVENT) {
  43. // Check topic
  44. if (dczTopicOut.equals(topic)) {
  45. // Parse response
  46. DynamicJsonBuffer jsonBuffer;
  47. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  48. if (!root.success()) {
  49. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  50. return;
  51. }
  52. // IDX
  53. unsigned long idx = root["idx"];
  54. int relayID = _domoticzRelay(idx);
  55. if (relayID >= 0) {
  56. // Skip message if recursive
  57. if (_domoticzSkip(idx)) return;
  58. unsigned long value = root["nvalue"];
  59. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
  60. relayStatus(relayID, value == 1);
  61. }
  62. }
  63. }
  64. };
  65. void _domoticzWebSocketOnSend(JsonObject& root) {
  66. root["dczVisible"] = 1;
  67. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  68. root["dczSkip"] = getSetting("dczSkip", DOMOTICZ_SKIP_TIME);
  69. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  70. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  71. JsonArray& relays = root.createNestedArray("dczRelayIdx");
  72. for (byte i=0; i<relayCount(); i++) {
  73. relays.add(domoticzIdx(i));
  74. }
  75. #if SENSOR_SUPPORT
  76. JsonArray& list = root.createNestedArray("dczMagnitudes");
  77. for (byte i=0; i<magnitudeCount(); i++) {
  78. JsonObject& element = list.createNestedObject();
  79. element["name"] = magnitudeName(i);
  80. element["type"] = magnitudeType(i);
  81. element["index"] = magnitudeIndex(i);
  82. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  83. }
  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. _domoticzMqttSubscribe(_dcz_enabled);
  90. }
  91. //------------------------------------------------------------------------------
  92. // Public API
  93. //------------------------------------------------------------------------------
  94. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  95. if (!_dcz_enabled) return;
  96. unsigned int idx = getSetting(key).toInt();
  97. if (idx > 0) {
  98. // Skip message if recursive
  99. if (_domoticzSkip(idx)) return;
  100. char payload[128];
  101. snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  102. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  103. }
  104. }
  105. template<typename T> void domoticzSend(const char * key, T nvalue) {
  106. domoticzSend(key, nvalue, "");
  107. }
  108. void domoticzSendRelay(unsigned int relayID) {
  109. if (!_dcz_enabled) return;
  110. char buffer[15];
  111. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID);
  112. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  113. }
  114. int domoticzIdx(unsigned int relayID) {
  115. char buffer[15];
  116. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID);
  117. return getSetting(buffer).toInt();
  118. }
  119. void domoticzSetup() {
  120. _domoticzConfigure();
  121. #if WEB_SUPPORT
  122. wsOnSendRegister(_domoticzWebSocketOnSend);
  123. wsOnAfterParseRegister(_domoticzConfigure);
  124. #endif
  125. mqttRegister(_domoticzMqtt);
  126. }
  127. bool domoticzEnabled() {
  128. return _dcz_enabled;
  129. }
  130. #endif