Browse Source

Avoid recursion in domoticz messages

fastled
Xose Pérez 7 years ago
parent
commit
2068dc41b9
6 changed files with 3464 additions and 3431 deletions
  1. +1
    -0
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +21
    -0
      code/espurna/domoticz.ino
  4. +3435
    -3431
      code/espurna/static/index.html.gz.h
  5. +1
    -0
      code/espurna/web.ino
  6. +6
    -0
      code/html/index.html

+ 1
- 0
code/espurna/config/general.h View File

@ -672,6 +672,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define DOMOTICZ_ENABLED 0 // Disable domoticz by default #define DOMOTICZ_ENABLED 0 // Disable domoticz by default
#define DOMOTICZ_IN_TOPIC "domoticz/in" // Default subscription topic #define DOMOTICZ_IN_TOPIC "domoticz/in" // Default subscription topic
#define DOMOTICZ_OUT_TOPIC "domoticz/out" // Default publication topic #define DOMOTICZ_OUT_TOPIC "domoticz/out" // Default publication topic
#define DOMOTICZ_SKIP_TIME 2 // Avoid recursion skipping messages to same IDX within 2 seconds
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// HOME ASSISTANT // HOME ASSISTANT


BIN
code/espurna/data/index.html.gz View File


+ 21
- 0
code/espurna/domoticz.ino View File

@ -11,6 +11,9 @@ Copyright (C) 2016-2017 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;
unsigned long _dcz_skip_time = 0;
unsigned long _dcz_last_idx = 0;
unsigned long _dcz_last_time = 0;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Private methods // Private methods
@ -25,6 +28,13 @@ int _domoticzRelay(unsigned int idx) {
return -1; 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) { 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);
@ -52,9 +62,14 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload)
unsigned long idx = root["idx"]; unsigned long idx = root["idx"];
int relayID = _domoticzRelay(idx); int relayID = _domoticzRelay(idx);
if (relayID >= 0) { if (relayID >= 0) {
// Skip message if recursive
if (_domoticzSkip(idx)) return;
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);
relayStatus(relayID, value == 1); relayStatus(relayID, value == 1);
} }
} }
@ -71,9 +86,14 @@ template<typename T> void domoticzSend(const char * key, T nvalue, const char *
if (!_dcz_enabled) return; if (!_dcz_enabled) return;
unsigned int idx = getSetting(key).toInt(); unsigned int idx = getSetting(key).toInt();
if (idx > 0) { if (idx > 0) {
// Skip message if recursive
if (_domoticzSkip(idx)) return;
char payload[128]; char payload[128];
snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); 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); mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload);
} }
} }
@ -96,6 +116,7 @@ int domoticzIdx(unsigned int relayID) {
void domoticzConfigure() { void domoticzConfigure() {
_dcz_enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; _dcz_enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
_dcz_skip_time = 1000 * getSetting("dczSkip", DOMOTICZ_SKIP_TIME).toInt();
} }
void domoticzSetup() { void domoticzSetup() {


+ 3435
- 3431
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 1
- 0
code/espurna/web.ino View File

@ -559,6 +559,7 @@ void _wsStart(uint32_t client_id) {
root["dczVisible"] = 1; root["dczVisible"] = 1;
root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
root["dczSkip"] = getSetting("dczSkip", DOMOTICZ_SKIP_TIME);
root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC); root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC);
root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);


+ 6
- 0
code/html/index.html View File

@ -711,6 +711,12 @@
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="dczEnabled" tabindex="30" /></div> <div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="dczEnabled" tabindex="30" /></div>
</div> </div>
<div class="pure-g">
<label class="pure-u-1 pure-u-sm-1-4" for="dczSkip">Anti-recursion time</label>
<div class="pure-u-1 pure-u-sm-1-8"><input class="pure-u-sm-23-24" name="dczSkip" type="number" min="0" max="10" tabindex="31" /></div>
<div class="pure-u-1 pure-u-sm-5-8 hint center">Skips in/out messages from the same IDX within this time in seconds</div>
</div>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="dczTopicIn">Domoticz IN Topic</label> <label class="pure-u-1 pure-u-md-1-4" for="dczTopicIn">Domoticz IN Topic</label>
<input class="pure-u-1 pure-u-md-3-4" name="dczTopicIn" type="text" tabindex="31" /> <input class="pure-u-1 pure-u-md-3-4" name="dczTopicIn" type="text" tabindex="31" />


Loading…
Cancel
Save