From 8d9ed882c137b41b5e687835efa0ae936a8dd05d Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 4 Nov 2018 20:02:49 +0100 Subject: [PATCH] Stop setting channel_override_set() in RPC calls to change the Timespec; Detect if timespec is empty and skip driving the channel in this case --- src/channel.c | 18 +++++++++++------- src/rpc.c | 2 -- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/channel.c b/src/channel.c index 966ba92..99ee120 100644 --- a/src/channel.c +++ b/src/channel.c @@ -4,7 +4,7 @@ #include "frozen/frozen.h" static void channel_timespec_cb(void *arg); -static bool channel_timespec(int idx); +static void channel_timespec(int idx); static struct channel_t s_channels[CHANNEL_MAX]; static int s_num_channels = 0; @@ -245,6 +245,7 @@ bool channel_get(int idx) { // This method is called every second. It looks at the timespec for the channel // and compares it with the channel_override flag, to see if the channel should // be switched. Logic: +// - if timespec is missing or empty, return. // - if channel_override is false: follow timespec and set channel accordingly. // - if channel_override ie true: // - if timespec wants the channel set differently, leave it as-is @@ -252,18 +253,21 @@ bool channel_get(int idx) { // This behavior lets users set the channel (RPC or Button), and timespec will // pick it up and govern the channel after the current (RPC/Button) state // converges with the timespec. -// It returns the current state of the relay. -static bool channel_timespec(int idx) { +static void channel_timespec(int idx) { bool match; if (idx < 0 || idx >= channel_get_total()) { - return false; + return; } + if (timespec_empty(s_channels[idx].timespec)) { + return; + } + match = timespec_match_now(s_channels[idx].timespec); if (!s_channels[idx].channel_override) { // Follow timespec if (match == s_channels[idx].relay_state) { - return s_channels[idx].relay_state == 1; + return; } LOG(LL_INFO, ("Timespec drives channel %d to relay_state %d", idx, match)); channel_set(idx, match); @@ -272,11 +276,11 @@ static bool channel_timespec(int idx) { if (match == s_channels[idx].relay_state) { LOG(LL_INFO, ("User state agrees with timespec, clearing override")); s_channels[idx].channel_override = false; - return s_channels[idx].relay_state == 1; + return; } } - return s_channels[idx].relay_state == 1; + return; } static void channel_timespec_cb(void *arg) { diff --git a/src/rpc.c b/src/rpc.c index bd1fd13..a394716 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -180,7 +180,6 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_ ri = NULL; return; } - channel_override_set(idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); unlink(fn); @@ -217,7 +216,6 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar ri = NULL; goto exit; } - channel_override_set(idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); if (!timespec_write_file(ts, fn)) {