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.

151 lines
4.2 KiB

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