|
|
@ -11,6 +11,9 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
bool _dcz_enabled = false; |
|
|
|
unsigned long _dcz_skip_time = 0; |
|
|
|
unsigned long _dcz_last_idx = 0; |
|
|
|
unsigned long _dcz_last_time = 0; |
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Private methods
|
|
|
@ -25,6 +28,13 @@ int _domoticzRelay(unsigned int idx) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
bool _domoticzSkip(unsigned long idx) { |
|
|
|
if (idx == _dcz_last_idx && (millis() - _dcz_last_time < _dcz_skip_time)) return true; |
|
|
|
_dcz_last_idx = idx; |
|
|
|
_dcz_last_time = millis(); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { |
|
|
|
|
|
|
|
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); |
|
|
@ -52,9 +62,14 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
|
unsigned long idx = root["idx"]; |
|
|
|
int relayID = _domoticzRelay(idx); |
|
|
|
if (relayID >= 0) { |
|
|
|
|
|
|
|
// Skip message if recursive
|
|
|
|
if (_domoticzSkip(idx)) return; |
|
|
|
|
|
|
|
unsigned long value = root["nvalue"]; |
|
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx); |
|
|
|
relayStatus(relayID, value == 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
@ -71,9 +86,14 @@ template<typename T> void domoticzSend(const char * key, T nvalue, const char * |
|
|
|
if (!_dcz_enabled) return; |
|
|
|
unsigned int idx = getSetting(key).toInt(); |
|
|
|
if (idx > 0) { |
|
|
|
|
|
|
|
// Skip message if recursive
|
|
|
|
if (_domoticzSkip(idx)) return; |
|
|
|
|
|
|
|
char payload[128]; |
|
|
|
snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); |
|
|
|
mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -96,6 +116,7 @@ int domoticzIdx(unsigned int relayID) { |
|
|
|
|
|
|
|
void domoticzConfigure() { |
|
|
|
_dcz_enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; |
|
|
|
_dcz_skip_time = 1000 * getSetting("dczSkip", DOMOTICZ_SKIP_TIME).toInt(); |
|
|
|
} |
|
|
|
|
|
|
|
void domoticzSetup() { |
|
|
|