Clean up RPC response.

- Refactor mqtt_publish_stat() to take va_list
- Make publish_stat format its response with json_vprintf() so it is
  valid JSON.
- Call publish_stat in channel_set, the only place where the GPIO state
  changes
- Return (valid) JSON response in all RPCs
This commit is contained in:
Pim van Pelt
2018-01-24 14:08:29 +01:00
parent 118eb14447
commit 4a083555e8
5 changed files with 16 additions and 28 deletions

View File

@ -41,7 +41,6 @@ static bool rpc_args_to_idx_and_gpio(struct mg_rpc_request_info *ri, struct mg_s
static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
uint8_t gpio;
char msg[100];
int idx;
rpc_log(ri, args);
@ -50,8 +49,7 @@ static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_
return;
channel_handler(gpio, NULL);
channel_report(idx, msg, sizeof(msg));
mg_rpc_send_responsef(ri, msg);
mg_rpc_send_responsef(ri, "idx: %d, relay_state: %d", idx, channel_get(idx));
ri = NULL;
(void) ri;
@ -62,7 +60,6 @@ static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_
static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
uint8_t gpio;
char msg[100];
int idx;
rpc_log(ri, args);
@ -70,9 +67,7 @@ static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg
if (!rpc_args_to_idx_and_gpio(ri, args, &idx, &gpio))
return;
channel_report(idx, msg, sizeof(msg));
mqtt_publish_stat("channel", msg);
mg_rpc_send_responsef(ri, msg);
mg_rpc_send_responsef(ri, "idx: %d, relay_state: %d", idx, channel_get(idx));
ri = NULL;
(void) ri;
@ -83,7 +78,6 @@ 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;
char msg[100];
int idx;
int value;
@ -109,8 +103,7 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg
}
channel_set(idx, (bool) value);
channel_report(idx, msg, sizeof(msg));
mg_rpc_send_responsef(ri, msg);
mg_rpc_send_responsef(ri, "idx: %d, relay_state: %d", idx, channel_get(idx));
ri = NULL;
(void) ri;