From b7467e3be18b5bd941e7385769ca2ccd985e264a Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 4 Nov 2018 20:50:23 +0100 Subject: [PATCH] add mqtt_publish_stat() for override and timespec changes; add channel_override_clear(); call c_o_c() when Timespec.Clear removes the timespec, as there is no concept of overrides when there is no timespec set --- include/main.h | 1 + src/channel.c | 21 +++++++++++++++++++++ src/rpc.c | 1 + 3 files changed, 23 insertions(+) diff --git a/include/main.h b/include/main.h index 316cc02..22bf1e6 100644 --- a/include/main.h +++ b/include/main.h @@ -35,5 +35,6 @@ struct mgos_timespec *channel_get_timespec(int idx); int channel_get_total(); void channel_handler(int gpio, void *arg); void channel_override_set(int idx); +void channel_override_clear(int idx); #endif // __MAIN_H diff --git a/src/channel.c b/src/channel.c index 99ee120..52f202a 100644 --- a/src/channel.c +++ b/src/channel.c @@ -259,6 +259,7 @@ static void channel_timespec(int idx) { if (idx < 0 || idx >= channel_get_total()) { return; } + if (timespec_empty(s_channels[idx].timespec)) { return; } @@ -270,12 +271,14 @@ static void channel_timespec(int idx) { return; } LOG(LL_INFO, ("Timespec drives channel %d to relay_state %d", idx, match)); + mqtt_publish_stat("timespec", "{idx: %d, relay_state: %d, override: %B}", idx, match, s_channels[idx].channel_override); channel_set(idx, match); } else { // Follow override until it matches timespec if (match == s_channels[idx].relay_state) { LOG(LL_INFO, ("User state agrees with timespec, clearing override")); s_channels[idx].channel_override = false; + mqtt_publish_stat("override", "{idx: %d, relay_state: %d, override: %B}", idx, match, s_channels[idx].channel_override); return; } } @@ -316,19 +319,37 @@ void channel_override_set(int idx) { if (idx < 0 || idx >= channel_get_total()) { return; } + + if (timespec_empty(s_channels[idx].timespec)) { + return; + } + match = timespec_match_now(s_channels[idx].timespec); if (match != s_channels[idx].relay_state) { LOG(LL_INFO, ("User state disagrees with timespec, setting override")); s_channels[idx].channel_override = true; + mqtt_publish_stat("override", "{idx: %d, relay_state: %d, override: %B}", idx, s_channels[idx].relay_state, s_channels[idx].channel_override); return; } if (s_channels[idx].channel_override && (match == s_channels[idx].relay_state)) { LOG(LL_INFO, ("User state agrees with timespec, clearing override")); + mqtt_publish_stat("override", "{idx: %d, relay_state: %d, override: %B}", idx, s_channels[idx].relay_state, s_channels[idx].channel_override); s_channels[idx].channel_override = false; return; } } +// Clear the override flag on a channel. +void channel_override_clear(int idx) { + if (idx < 0 || idx >= channel_get_total()) { + return; + } + + s_channels[idx].channel_override = false; + mqtt_publish_stat("override", "{idx: %d, relay_state: %d, override: %B}", idx, s_channels[idx].relay_state, s_channels[idx].channel_override); + return; +} + // Channel handler arg is non-NULL if it was a pushbutton, and NULL if it was an RPC. void channel_handler(int gpio, void *arg) { uint8_t idx; diff --git a/src/rpc.c b/src/rpc.c index a394716..edfd9bf 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -180,6 +180,7 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_ ri = NULL; return; } + channel_override_clear(idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); unlink(fn);