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.

161 lines
4.5 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. 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 int idx = root["idx"];
  45. unsigned char relayID = _domoticzRelay(idx);
  46. if (relayID >= 0) {
  47. unsigned char value = root["nvalue"];
  48. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx);
  49. relayStatus(relayID, value == 1);
  50. }
  51. }
  52. }
  53. };
  54. #if WEB_SUPPORT
  55. bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
  56. return (strncmp(key, "dcz", 3) == 0);
  57. }
  58. void _domoticzWebSocketOnSend(JsonObject& root) {
  59. root["dczVisible"] = 1;
  60. root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  61. root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
  62. root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  63. JsonArray& relays = root.createNestedArray("dczRelays");
  64. for (unsigned char i=0; i<relayCount(); i++) {
  65. relays.add(domoticzIdx(i));
  66. }
  67. #if SENSOR_SUPPORT
  68. JsonArray& list = root.createNestedArray("dczMagnitudes");
  69. for (byte i=0; i<magnitudeCount(); i++) {
  70. JsonObject& element = list.createNestedObject();
  71. element["name"] = magnitudeName(i);
  72. element["type"] = magnitudeType(i);
  73. element["index"] = magnitudeIndex(i);
  74. element["idx"] = getSetting("dczMagnitude", i, 0).toInt();
  75. }
  76. #endif
  77. }
  78. #endif // WEB_SUPPORT
  79. void _domoticzConfigure() {
  80. bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  81. if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);
  82. _dcz_enabled = enabled;
  83. }
  84. //------------------------------------------------------------------------------
  85. // Public API
  86. //------------------------------------------------------------------------------
  87. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  88. if (!_dcz_enabled) return;
  89. unsigned int idx = getSetting(key).toInt();
  90. if (idx > 0) {
  91. char payload[128];
  92. snprintf(payload, sizeof(payload), "{\"idx\": %u, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  93. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  94. }
  95. }
  96. template<typename T> void domoticzSend(const char * key, T nvalue) {
  97. domoticzSend(key, nvalue, "");
  98. }
  99. void domoticzSendRelay(unsigned char relayID) {
  100. if (!_dcz_enabled) return;
  101. char buffer[15];
  102. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  103. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  104. }
  105. unsigned int domoticzIdx(unsigned char relayID) {
  106. char buffer[15];
  107. snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
  108. return getSetting(buffer).toInt();
  109. }
  110. void domoticzSetup() {
  111. _domoticzConfigure();
  112. #if WEB_SUPPORT
  113. wsOnSendRegister(_domoticzWebSocketOnSend);
  114. wsOnAfterParseRegister(_domoticzConfigure);
  115. wsOnReceiveRegister(_domoticzWebSocketOnReceive);
  116. #endif
  117. mqttRegister(_domoticzMqtt);
  118. }
  119. bool domoticzEnabled() {
  120. return _dcz_enabled;
  121. }
  122. #endif