|
@ -10,40 +10,21 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) { |
|
|
|
|
|
unsigned int idx = getSetting(key).toInt(); |
|
|
|
|
|
if (idx > 0) { |
|
|
|
|
|
char payload[128]; |
|
|
|
|
|
snprintf(payload, 128, "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); |
|
|
|
|
|
mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template<typename T> void domoticzSend(const char * key, T nvalue) { |
|
|
|
|
|
domoticzSend(key, nvalue, ""); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
bool _dczEnabled = false; |
|
|
|
|
|
|
|
|
void relayDomoticzSend(unsigned int relayID) { |
|
|
|
|
|
char buffer[15]; |
|
|
|
|
|
sprintf(buffer, "dczRelayIdx%d", relayID); |
|
|
|
|
|
domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Private methods
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
int relayFromIdx(unsigned int idx) { |
|
|
|
|
|
|
|
|
int _domoticzRelay(unsigned int idx) { |
|
|
for (int relayID=0; relayID<relayCount(); relayID++) { |
|
|
for (int relayID=0; relayID<relayCount(); relayID++) { |
|
|
if (relayToIdx(relayID) == idx) { |
|
|
|
|
|
|
|
|
if (domoticzIdx(relayID) == idx) { |
|
|
return relayID; |
|
|
return relayID; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return -1; |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int relayToIdx(unsigned int relayID) { |
|
|
|
|
|
char buffer[15]; |
|
|
|
|
|
sprintf(buffer, "dczRelayIdx%d", relayID); |
|
|
|
|
|
return getSetting(buffer).toInt(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { |
|
|
void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { |
|
|
|
|
|
|
|
|
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); |
|
|
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); |
|
@ -54,6 +35,8 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
|
|
|
|
|
|
if (type == MQTT_MESSAGE_EVENT) { |
|
|
if (type == MQTT_MESSAGE_EVENT) { |
|
|
|
|
|
|
|
|
|
|
|
if (!_dczEnabled) return; |
|
|
|
|
|
|
|
|
// Check topic
|
|
|
// Check topic
|
|
|
if (dczTopicOut.equals(topic)) { |
|
|
if (dczTopicOut.equals(topic)) { |
|
|
|
|
|
|
|
@ -67,7 +50,7 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
|
|
|
|
|
|
// IDX
|
|
|
// IDX
|
|
|
unsigned long idx = root["idx"]; |
|
|
unsigned long idx = root["idx"]; |
|
|
int relayID = relayFromIdx(idx); |
|
|
|
|
|
|
|
|
int relayID = _domoticzRelay(idx); |
|
|
if (relayID >= 0) { |
|
|
if (relayID >= 0) { |
|
|
unsigned long value = root["nvalue"]; |
|
|
unsigned long value = root["nvalue"]; |
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx); |
|
|
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx); |
|
@ -80,7 +63,43 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Public API
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) { |
|
|
|
|
|
if (!_dczEnabled) return; |
|
|
|
|
|
unsigned int idx = getSetting(key).toInt(); |
|
|
|
|
|
if (idx > 0) { |
|
|
|
|
|
char payload[128]; |
|
|
|
|
|
snprintf(payload, 128, "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); |
|
|
|
|
|
mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template<typename T> void domoticzSend(const char * key, T nvalue) { |
|
|
|
|
|
domoticzSend(key, nvalue, ""); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void domoticzSendRelay(unsigned int relayID) { |
|
|
|
|
|
if (!_dczEnabled) return; |
|
|
|
|
|
char buffer[15]; |
|
|
|
|
|
sprintf(buffer, "dczRelayIdx%d", relayID); |
|
|
|
|
|
domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int domoticzIdx(unsigned int relayID) { |
|
|
|
|
|
char buffer[15]; |
|
|
|
|
|
sprintf(buffer, "dczRelayIdx%d", relayID); |
|
|
|
|
|
return getSetting(buffer).toInt(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void domoticzConfigure() { |
|
|
|
|
|
_dczEnabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void domoticzSetup() { |
|
|
void domoticzSetup() { |
|
|
|
|
|
domoticzConfigure(); |
|
|
mqttRegister(_domoticzMqtt); |
|
|
mqttRegister(_domoticzMqtt); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|