|
@ -11,6 +11,7 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
#include <ArduinoJson.h>
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
bool _dcz_enabled = false; |
|
|
bool _dcz_enabled = false; |
|
|
|
|
|
std::vector<bool> _dcz_relay_state; |
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
//------------------------------------------------------------------------------
|
|
|
// Private methods
|
|
|
// 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) { |
|
|
void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { |
|
|
|
|
|
|
|
|
if (!_dcz_enabled) return; |
|
|
if (!_dcz_enabled) return; |
|
@ -128,7 +138,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
if (relayID >= 0) { |
|
|
if (relayID >= 0) { |
|
|
unsigned char value = root["nvalue"]; |
|
|
unsigned char value = root["nvalue"]; |
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); |
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); |
|
|
relayStatus(relayID, value > 0); |
|
|
|
|
|
|
|
|
_domoticzStatus(relayID, value > 0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
#else
|
|
|
#else
|
|
@ -139,7 +149,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
if (relayID >= 0) { |
|
|
if (relayID >= 0) { |
|
|
unsigned char value = root["nvalue"]; |
|
|
unsigned char value = root["nvalue"]; |
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); |
|
|
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() { |
|
|
void _domoticzConfigure() { |
|
|
bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; |
|
|
bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; |
|
|
if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled); |
|
|
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; |
|
|
_dcz_enabled = enabled; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -222,6 +238,8 @@ template<typename T> void domoticzSend(const char * key, T nvalue) { |
|
|
|
|
|
|
|
|
void domoticzSendRelay(unsigned char relayID, bool status) { |
|
|
void domoticzSendRelay(unsigned char relayID, bool status) { |
|
|
if (!_dcz_enabled) return; |
|
|
if (!_dcz_enabled) return; |
|
|
|
|
|
if (_domoticzStatus(relayID) == status) return; |
|
|
|
|
|
_dcz_relay_state[relayID] = status; |
|
|
char buffer[15]; |
|
|
char buffer[15]; |
|
|
snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); |
|
|
snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); |
|
|
domoticzSend(buffer, status ? "1" : "0"); |
|
|
domoticzSend(buffer, status ? "1" : "0"); |
|
|