From cb8d9c344dcdafb5cc9727ffb23c603f324395bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 5 Jan 2019 03:52:50 +0100 Subject: [PATCH] Manage relay changes in third party modules via broker --- code/espurna/config/dependencies.h | 17 +++++++++++++++ code/espurna/domoticz.ino | 19 ++++++++++++++--- code/espurna/influxdb.ino | 12 +++++++++++ code/espurna/led.ino | 13 ++++++++++++ code/espurna/relay.ino | 34 +----------------------------- code/espurna/thinkspeak.ino | 18 +++++++++++++--- 6 files changed, 74 insertions(+), 39 deletions(-) diff --git a/code/espurna/config/dependencies.h b/code/espurna/config/dependencies.h index 6bc3f5ab..1c31dce4 100644 --- a/code/espurna/config/dependencies.h +++ b/code/espurna/config/dependencies.h @@ -33,9 +33,26 @@ #define DEBUG_SERIAL_SUPPORT 0 #endif +#if ALEXA_SUPPORT +#undef BROKER_SUPPORT +#define BROKER_SUPPORT 1 // If Alexa enabled enable BROKER +#endif + +#if INFLUXDB_SUPPORT +#undef BROKER_SUPPORT +#define BROKER_SUPPORT 1 // If InfluxDB enabled enable BROKER +#endif + +#if THINKSPEAK_SUPPORT +#undef BROKER_SUPPORT +#define BROKER_SUPPORT 1 // If Thingspeak enabled enable BROKER +#endif + #if DOMOTICZ_SUPPORT #undef MQTT_SUPPORT #define MQTT_SUPPORT 1 // If Domoticz enabled enable MQTT +#undef BROKER_SUPPORT +#define BROKER_SUPPORT 1 // If Domoticz enabled enable BROKER #endif #if HOMEASSISTANT_SUPPORT diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 23386529..7a76c27b 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -80,6 +80,15 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) }; +#if BROKER_SUPPORT +void _domoticzBrokerCallback(const char * topic, unsigned char id, const char * payload) { + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { + unsigned char value = atoi(payload); + domoticzSendRelay(id, value == 1); + } +} +#endif // BROKER_SUPPORT + #if WEB_SUPPORT bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) { @@ -137,16 +146,16 @@ template void domoticzSend(const char * key, T nvalue) { domoticzSend(key, nvalue, ""); } -void domoticzSendRelay(unsigned char relayID) { +void domoticzSendRelay(unsigned char relayID, bool status) { if (!_dcz_enabled) return; char buffer[15]; snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); - domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); + domoticzSend(buffer, status ? "1" : "0"); } void domoticzSendRelays() { for (uint8_t relayID=0; relayID < relayCount(); relayID++) { - domoticzSendRelay(relayID); + domoticzSendRelay(relayID, relayStatus(relayID)); } } @@ -165,6 +174,10 @@ void domoticzSetup() { wsOnReceiveRegister(_domoticzWebSocketOnReceive); #endif + #if BROKER_SUPPORT + brokerRegister(_domoticzBrokerCallback); + #endif + // Callbacks mqttRegister(_domoticzMqtt); espurnaRegisterReload(_domoticzConfigure); diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 67bc4d96..d2b3b330 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -38,6 +38,14 @@ void _idbConfigure() { } } +#if BROKER_SUPPORT +void _idbBrokerCallback(const char * topic, unsigned char id, const char * payload) { + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { + idbSend(topic, id, (char *) payload); + } +} +#endif // BROKER_SUPPORT + // ----------------------------------------------------------------------------- bool idbSend(const char * topic, const char * payload) { @@ -108,6 +116,10 @@ void idbSetup() { wsOnReceiveRegister(_idbWebSocketOnReceive); #endif + #if BROKER_SUPPORT + brokerRegister(_idbBrokerCallback); + #endif + // Main callbacks espurnaRegisterReload(_idbConfigure); diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 7fe6716a..eda1f339 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -73,6 +73,14 @@ void _ledWebSocketOnSend(JsonObject& root) { #endif +#if BROKER_SUPPORT +void _ledBrokerCallback(const char * topic, unsigned char id, const char * payload) { + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { + ledUpdate(true); + } +} +#endif // BROKER_SUPPORT + #if MQTT_SUPPORT void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) { @@ -173,6 +181,11 @@ void ledSetup() { wsOnReceiveRegister(_ledWebSocketOnReceive); #endif + #if BROKER_SUPPORT + brokerRegister(_ledBrokerCallback); + #endif + + DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); // Main callbacks diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 71016bd1..897b19d6 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -164,7 +164,7 @@ void _relayProcess(bool mode) { // Only process the relays we have to change if (target == _relays[id].current_status) continue; - // Only process the relays we have change to the requested mode + // Only process the relays we have to change to the requested mode if (target != mode) continue; // Only process if the change_time has arrived @@ -201,24 +201,6 @@ void _relayProcess(bool mode) { } - #if DOMOTICZ_SUPPORT - domoticzSendRelay(id); - #endif - - #if INFLUXDB_SUPPORT - relayInfluxDB(id); - #endif - - #if THINGSPEAK_SUPPORT - tspkEnqueueRelay(id, target); - tspkFlush(); - #endif - - // Flag relay-based LEDs to update status - #if LED_SUPPORT - ledUpdate(true); - #endif - _relays[id].report = false; _relays[id].group_report = false; @@ -993,20 +975,6 @@ void relaySetupMQTT() { #endif -//------------------------------------------------------------------------------ -// InfluxDB -//------------------------------------------------------------------------------ - -#if INFLUXDB_SUPPORT - -void relayInfluxDB(unsigned char id) { - if (id >= _relays.size()) return; - idbSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0"); -} - -#endif - - //------------------------------------------------------------------------------ // Settings //------------------------------------------------------------------------------ diff --git a/code/espurna/thinkspeak.ino b/code/espurna/thinkspeak.ino index 5d626c88..8db7973e 100644 --- a/code/espurna/thinkspeak.ino +++ b/code/espurna/thinkspeak.ino @@ -35,6 +35,16 @@ unsigned char _tspk_tries = 0; // ----------------------------------------------------------------------------- +#if BROKER_SUPPORT +void _tspkBrokerCallback(const char * topic, unsigned char id, const char * payload) { + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { + tspkEnqueueRelay(id, (char *) payload); + tspkFlush(); + } +} +#endif // BROKER_SUPPORT + + #if WEB_SUPPORT bool _tspkWebSocketOnReceive(const char * key, JsonVariant& value) { @@ -259,12 +269,10 @@ void _tspkFlush() { // ----------------------------------------------------------------------------- -bool tspkEnqueueRelay(unsigned char index, unsigned char status) { +bool tspkEnqueueRelay(unsigned char index, char * payload) { if (!_tspk_enabled) return true; unsigned char id = getSetting("tspkRelay", index, 0).toInt(); if (id > 0) { - char payload[3] = {0}; - itoa(status ? 1 : 0, payload, 10); _tspkEnqueue(id, payload); return true; } @@ -298,6 +306,10 @@ void tspkSetup() { wsOnReceiveRegister(_tspkWebSocketOnReceive); #endif + #if BROKER_SUPPORT + brokerRegister(_tspkBrokerCallback); + #endif + DEBUG_MSG_P(PSTR("[THINGSPEAK] Async %s, SSL %s\n"), THINGSPEAK_USE_ASYNC ? "ENABLED" : "DISABLED", THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED"