Add 'duration' argument to Channel.Set RPC call
This commit is contained in:
27
src/rpc.c
27
src/rpc.c
@ -80,19 +80,28 @@ static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg
|
||||
|
||||
static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
uint8_t gpio;
|
||||
int idx;
|
||||
int value;
|
||||
int idx = -1;
|
||||
int value = -1;
|
||||
int duration = -1;
|
||||
|
||||
rpc_log(ri, args);
|
||||
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &idx, &value) != 2) {
|
||||
mg_rpc_send_errorf(ri, 400, "idx and value are required");
|
||||
json_scanf(args.p, args.len, ri->args_fmt, &idx, &value, &duration);
|
||||
|
||||
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;
|
||||
return;
|
||||
}
|
||||
|
||||
if (idx < 0 || idx >= channel_get_total()) {
|
||||
mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1);
|
||||
if (value < 0 || value > 1) {
|
||||
mg_rpc_send_errorf(ri, 400, "value must be 0 or 1");
|
||||
ri = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
if (duration > 60) {
|
||||
mg_rpc_send_errorf(ri, 400, "duration, if set, must be between 1 and 60 seconds");
|
||||
ri = NULL;
|
||||
return;
|
||||
}
|
||||
@ -104,7 +113,11 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg
|
||||
return;
|
||||
}
|
||||
|
||||
if (duration > 0) {
|
||||
channel_set_duration(idx, (bool)value, duration);
|
||||
} else {
|
||||
channel_set(idx, (bool)value);
|
||||
}
|
||||
mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx));
|
||||
ri = NULL;
|
||||
|
||||
@ -119,5 +132,5 @@ void rpc_init() {
|
||||
|
||||
mg_rpc_add_handler(c, "Channel.Toggle", "{idx: %d}", rpc_channel_toggle_handler, NULL);
|
||||
mg_rpc_add_handler(c, "Channel.Get", "{idx: %d}", rpc_channel_get_handler, NULL);
|
||||
mg_rpc_add_handler(c, "Channel.Set", "{idx: %d, value: %d}", rpc_channel_set_handler, NULL);
|
||||
mg_rpc_add_handler(c, "Channel.Set", "{idx: %d, value: %d, duration: %d}", rpc_channel_set_handler, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user