Commit timespec to file on Timespec.Add; Unlink timespec file on Timespec.Clear; Initialize timespec from file in channel_init()

This commit is contained in:
Pim van Pelt
2018-11-04 19:19:17 +01:00
parent 8c4864b199
commit 61ddb0334e
3 changed files with 26 additions and 2 deletions

View File

@ -71,6 +71,7 @@ bool channel_init(const char *fn) {
int led = -1, relay = -1, button = -1; int led = -1, relay = -1, button = -1;
bool led_invert = false; bool led_invert = false;
bool relay_invert = false; bool relay_invert = false;
char fn[100];
LOG(LL_DEBUG, ("[%d]: [%.*s]", idx, val.len, val.ptr)); LOG(LL_DEBUG, ("[%d]: [%.*s]", idx, val.len, val.ptr));
if (val.len == 0 || !val.ptr) { if (val.len == 0 || !val.ptr) {
@ -105,6 +106,11 @@ bool channel_init(const char *fn) {
s_channels[idx].led_invert = led_invert; s_channels[idx].led_invert = led_invert;
s_channels[idx].channel_override = false; s_channels[idx].channel_override = false;
s_channels[idx].timespec = timespec_create(); s_channels[idx].timespec = timespec_create();
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
if (timespec_read_file(s_channels[idx].timespec, fn)) {
LOG(LL_INFO, ("Initialized timespec from file %s", fn));
}
} }
ret = true; ret = true;

View File

@ -157,7 +157,8 @@ static void rpc_timespec_get_handler(struct mg_rpc_request_info *ri, void *cb_ar
static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
struct mgos_timespec *ts; struct mgos_timespec *ts;
int idx; char fn[100];
int idx;
rpc_log(ri, args); rpc_log(ri, args);
@ -181,6 +182,9 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_
} }
channel_override_set(idx); channel_override_set(idx);
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
unlink(fn);
mg_rpc_send_responsef(ri, "{idx: %d}", idx); mg_rpc_send_responsef(ri, "{idx: %d}", idx);
ri = NULL; ri = NULL;
@ -192,6 +196,7 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar
struct mgos_timespec *ts; struct mgos_timespec *ts;
int idx; int idx;
char *spec = NULL; char *spec = NULL;
char fn[100];
rpc_log(ri, args); rpc_log(ri, args);
@ -214,6 +219,13 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar
} }
channel_override_set(idx); channel_override_set(idx);
snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx);
if (!timespec_write_file(ts, fn)) {
mg_rpc_send_errorf(ri, 400, "Could not commit timespec to file: %s", fn);
ri = NULL;
return;
}
mg_rpc_send_responsef(ri, "{idx: %d}", idx); mg_rpc_send_responsef(ri, "{idx: %d}", idx);
ri = NULL; ri = NULL;

View File

@ -274,6 +274,7 @@ bool timespec_write_file(struct mgos_timespec *ts, const char *fn) {
} }
close(fd); close(fd);
LOG(LL_INFO, ("Wrote timespec to %s", fn));
return true; return true;
} }
@ -294,13 +295,18 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
if (0 != stat(fn, &fp_stat)) { if (0 != stat(fn, &fp_stat)) {
LOG(LL_ERROR, ("Could not stat %s", fn)); LOG(LL_ERROR, ("Could not stat %s", fn));
return false;
} }
if (fp_stat.st_size > 1024) { if (fp_stat.st_size > 1024) {
LOG(LL_ERROR, ("File size of %s is larger than 1024 bytes (%u)", fn, (uint32_t)fp_stat.st_size)); LOG(LL_ERROR, ("File size of %s is larger than 1024 bytes (%u)", fn, (uint32_t)fp_stat.st_size));
return false;
} }
buf = malloc(fp_stat.st_size + 1); buf = malloc(fp_stat.st_size + 1);
if (!buf) { if (!buf) {
LOG(LL_ERROR, ("Could not malloc %u bytes for file %s", (uint32_t)fp_stat.st_size, fn)); LOG(LL_ERROR, ("Could not malloc %u bytes for file %s", (uint32_t)fp_stat.st_size, fn));
return false;
} }
if (!(fd = open(fn, O_RDONLY))) { if (!(fd = open(fn, O_RDONLY))) {
@ -316,7 +322,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
} }
buf[fp_stat.st_size] = '\0'; buf[fp_stat.st_size] = '\0';
close(fd); close(fd);
LOG(LL_INFO, ("buf='%s'", buf));
// Wipe the timespec and parse back // Wipe the timespec and parse back
timespec_clear_spec(ts); timespec_clear_spec(ts);
@ -328,5 +333,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
} }
} }
free(buf); free(buf);
LOG(LL_INFO, ("Read timespec from %s", fn));
return true; return true;
} }