Add Timespec.Replace RPC -- clear+set
This commit is contained in:
52
src/rpc.c
52
src/rpc.c
@ -236,6 +236,57 @@ exit:
|
|||||||
(void)fi;
|
(void)fi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void rpc_timespec_replace_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||||
|
struct mgos_timespec *ts;
|
||||||
|
int idx;
|
||||||
|
char *spec = NULL;
|
||||||
|
char fn[100];
|
||||||
|
|
||||||
|
rpc_log(ri, args);
|
||||||
|
|
||||||
|
json_scanf(args.p, args.len, ri->args_fmt, &idx, &spec);
|
||||||
|
if (idx < 0 || idx >= channel_get_total()) {
|
||||||
|
mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1);
|
||||||
|
ri = NULL;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
ts = channel_get_timespec(idx);
|
||||||
|
if (!ts) {
|
||||||
|
mg_rpc_send_errorf(ri, 400, "timespec on channel %d isn't set", idx);
|
||||||
|
ri = NULL;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
if (!timespec_clear_spec(ts)) {
|
||||||
|
mg_rpc_send_errorf(ri, 400, "Failed to clear timespec on channel %d", idx);
|
||||||
|
ri = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
channel_override_clear(idx);
|
||||||
|
|
||||||
|
if (!timespec_add_spec(ts, spec)) {
|
||||||
|
mg_rpc_send_errorf(ri, 400, "timespec '%Q' is malformed", spec);
|
||||||
|
ri = NULL;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
ri = NULL;
|
||||||
|
|
||||||
|
exit:
|
||||||
|
if (spec) {
|
||||||
|
free(spec);
|
||||||
|
}
|
||||||
|
(void)cb_arg;
|
||||||
|
(void)fi;
|
||||||
|
}
|
||||||
|
|
||||||
void rpc_init() {
|
void rpc_init() {
|
||||||
struct mg_rpc *c = mgos_rpc_get_global();
|
struct mg_rpc *c = mgos_rpc_get_global();
|
||||||
|
|
||||||
@ -245,4 +296,5 @@ void rpc_init() {
|
|||||||
mg_rpc_add_handler(c, "Timespec.Get", "{idx: %d}", rpc_timespec_get_handler, NULL);
|
mg_rpc_add_handler(c, "Timespec.Get", "{idx: %d}", rpc_timespec_get_handler, NULL);
|
||||||
mg_rpc_add_handler(c, "Timespec.Clear", "{idx: %d}", rpc_timespec_clear_handler, NULL);
|
mg_rpc_add_handler(c, "Timespec.Clear", "{idx: %d}", rpc_timespec_clear_handler, NULL);
|
||||||
mg_rpc_add_handler(c, "Timespec.Add", "{idx: %d, spec: %Q}", rpc_timespec_add_handler, NULL);
|
mg_rpc_add_handler(c, "Timespec.Add", "{idx: %d, spec: %Q}", rpc_timespec_add_handler, NULL);
|
||||||
|
mg_rpc_add_handler(c, "Timespec.Replace", "{idx: %d, spec: %Q}", rpc_timespec_replace_handler, NULL);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user