diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index eb923ace..223b1ab6 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -11,6 +11,7 @@ Copyright (C) 2016-2018 by Xose PĂ©rez #include bool _dcz_enabled = false; +std::vector _dcz_relay_state; //------------------------------------------------------------------------------ // Private methods @@ -36,6 +37,15 @@ void _domoticzMqttSubscribe(bool value) { } +bool _domoticzStatus(unsigned char id) { + return _dcz_relay_state[id]; +} + +void _domoticzStatus(unsigned char id, bool status) { + _dcz_relay_state[id] = status; + relayStatus(id, status); +} + void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { if (!_dcz_enabled) return; @@ -128,7 +138,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) if (relayID >= 0) { unsigned char value = root["nvalue"]; DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); - relayStatus(relayID, value > 0); + _domoticzStatus(relayID, value > 0); } } #else @@ -139,7 +149,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) if (relayID >= 0) { unsigned char value = root["nvalue"]; DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); - relayStatus(relayID, value == 1); + _domoticzStatus(relayID, value == 1); } } @@ -199,6 +209,12 @@ void _domoticzWebSocketOnSend(JsonObject& root) { void _domoticzConfigure() { bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled); + + _dcz_relay_state.reserve(relayCount()); + for (size_t n = 0; n < relayCount(); ++n) { + _dcz_relay_state[n] = relayStatus(n); + } + _dcz_enabled = enabled; } @@ -222,6 +238,8 @@ template void domoticzSend(const char * key, T nvalue) { void domoticzSendRelay(unsigned char relayID, bool status) { if (!_dcz_enabled) return; + if (_domoticzStatus(relayID) == status) return; + _dcz_relay_state[relayID] = status; char buffer[15]; snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); domoticzSend(buffer, status ? "1" : "0");