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

This commit is contained in:
Pim van Pelt
2018-11-04 20:02:49 +01:00
parent 8149a6cd10
commit 8d9ed882c1
2 changed files with 11 additions and 9 deletions

View File

@ -4,7 +4,7 @@
#include "frozen/frozen.h" #include "frozen/frozen.h"
static void channel_timespec_cb(void *arg); 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 struct channel_t s_channels[CHANNEL_MAX];
static int s_num_channels = 0; 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 // 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 // and compares it with the channel_override flag, to see if the channel should
// be switched. Logic: // be switched. Logic:
// - if timespec is missing or empty, return.
// - if channel_override is false: follow timespec and set channel accordingly. // - if channel_override is false: follow timespec and set channel accordingly.
// - if channel_override ie true: // - if channel_override ie true:
// - if timespec wants the channel set differently, leave it as-is // - 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 // 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 // pick it up and govern the channel after the current (RPC/Button) state
// converges with the timespec. // converges with the timespec.
// It returns the current state of the relay. static void channel_timespec(int idx) {
static bool channel_timespec(int idx) {
bool match; bool match;
if (idx < 0 || idx >= channel_get_total()) { 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); match = timespec_match_now(s_channels[idx].timespec);
if (!s_channels[idx].channel_override) { if (!s_channels[idx].channel_override) {
// Follow timespec // Follow timespec
if (match == s_channels[idx].relay_state) { 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)); LOG(LL_INFO, ("Timespec drives channel %d to relay_state %d", idx, match));
channel_set(idx, match); channel_set(idx, match);
@ -272,11 +276,11 @@ static bool channel_timespec(int idx) {
if (match == s_channels[idx].relay_state) { if (match == s_channels[idx].relay_state) {
LOG(LL_INFO, ("User state agrees with timespec, clearing override")); LOG(LL_INFO, ("User state agrees with timespec, clearing override"));
s_channels[idx].channel_override = false; 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) { static void channel_timespec_cb(void *arg) {

View File

@ -180,7 +180,6 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_
ri = NULL; ri = NULL;
return; return;
} }
channel_override_set(idx);
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
unlink(fn); unlink(fn);
@ -217,7 +216,6 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar
ri = NULL; ri = NULL;
goto exit; goto exit;
} }
channel_override_set(idx);
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
if (!timespec_write_file(ts, fn)) { if (!timespec_write_file(ts, fn)) {