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

This commit is contained in:
Pim van Pelt
2018-11-04 20:50:23 +01:00
parent aa3c0b0cda
commit b7467e3be1
3 changed files with 23 additions and 0 deletions

View File

@ -35,5 +35,6 @@ struct mgos_timespec *channel_get_timespec(int idx);
int channel_get_total(); int channel_get_total();
void channel_handler(int gpio, void *arg); void channel_handler(int gpio, void *arg);
void channel_override_set(int idx); void channel_override_set(int idx);
void channel_override_clear(int idx);
#endif // __MAIN_H #endif // __MAIN_H

View File

@ -259,6 +259,7 @@ static void channel_timespec(int idx) {
if (idx < 0 || idx >= channel_get_total()) { if (idx < 0 || idx >= channel_get_total()) {
return; return;
} }
if (timespec_empty(s_channels[idx].timespec)) { if (timespec_empty(s_channels[idx].timespec)) {
return; return;
} }
@ -270,12 +271,14 @@ static void channel_timespec(int idx) {
return; 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));
mqtt_publish_stat("timespec", "{idx: %d, relay_state: %d, override: %B}", idx, match, s_channels[idx].channel_override);
channel_set(idx, match); channel_set(idx, match);
} else { } else {
// Follow override until it matches timespec // Follow override until it matches timespec
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;
mqtt_publish_stat("override", "{idx: %d, relay_state: %d, override: %B}", idx, match, s_channels[idx].channel_override);
return; return;
} }
} }
@ -316,19 +319,37 @@ void channel_override_set(int idx) {
if (idx < 0 || idx >= channel_get_total()) { if (idx < 0 || idx >= channel_get_total()) {
return; 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 (match != s_channels[idx].relay_state) { if (match != s_channels[idx].relay_state) {
LOG(LL_INFO, ("User state disagrees with timespec, setting override")); LOG(LL_INFO, ("User state disagrees with timespec, setting override"));
s_channels[idx].channel_override = true; 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; return;
} }
if (s_channels[idx].channel_override && (match == s_channels[idx].relay_state)) { if (s_channels[idx].channel_override && (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"));
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; s_channels[idx].channel_override = false;
return; 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. // 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) { void channel_handler(int gpio, void *arg) {
uint8_t idx; uint8_t idx;

View File

@ -180,6 +180,7 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_
ri = NULL; ri = NULL;
return; return;
} }
channel_override_clear(idx);
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
unlink(fn); unlink(fn);