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.

106 lines
2.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 _dczEnabled = 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 _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
  20. String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
  21. if (type == MQTT_CONNECT_EVENT) {
  22. mqttSubscribeRaw(dczTopicOut.c_str());
  23. }
  24. if (type == MQTT_MESSAGE_EVENT) {
  25. if (!_dczEnabled) return;
  26. // Check topic
  27. if (dczTopicOut.equals(topic)) {
  28. // Parse response
  29. DynamicJsonBuffer jsonBuffer;
  30. JsonObject& root = jsonBuffer.parseObject((char *) payload);
  31. if (!root.success()) {
  32. DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
  33. return;
  34. }
  35. // IDX
  36. unsigned long idx = root["idx"];
  37. int relayID = _domoticzRelay(idx);
  38. if (relayID >= 0) {
  39. unsigned long value = root["nvalue"];
  40. DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
  41. relayStatus(relayID, value == 1);
  42. }
  43. }
  44. }
  45. };
  46. //------------------------------------------------------------------------------
  47. // Public API
  48. //------------------------------------------------------------------------------
  49. template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
  50. if (!_dczEnabled) return;
  51. unsigned int idx = getSetting(key).toInt();
  52. if (idx > 0) {
  53. char payload[128];
  54. snprintf(payload, strlen(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue);
  55. mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
  56. }
  57. }
  58. template<typename T> void domoticzSend(const char * key, T nvalue) {
  59. domoticzSend(key, nvalue, "");
  60. }
  61. void domoticzSendRelay(unsigned int relayID) {
  62. if (!_dczEnabled) return;
  63. char buffer[15];
  64. sprintf_P(buffer, PSTR("dczRelayIdx%d"), relayID);
  65. domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
  66. }
  67. int domoticzIdx(unsigned int relayID) {
  68. char buffer[15];
  69. sprintf_P(buffer, PSTR("dczRelayIdx%d"), relayID);
  70. return getSetting(buffer).toInt();
  71. }
  72. void domoticzConfigure() {
  73. _dczEnabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
  74. }
  75. void domoticzSetup() {
  76. domoticzConfigure();
  77. mqttRegister(_domoticzMqtt);
  78. }
  79. #endif