mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-20 11:19:56 +00:00
Update to use MG_ESC()
This commit is contained in:
1 parent
f4cf40d38f
commit
1908d3200e
8 files changed
+46
-56
No files matched your search
@@ -115,8 +115,8 @@ static void send_notification(struct mg_mgr *mgr, const char *fmt, ...) {
|
||||
|
||||
// Send simulated metrics data to the dashboard, for chart rendering
|
||||
static void timer_metrics_fn(void *param) {
|
||||
send_notification(param, "{%m:%m,%m:[%lu, %d]}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "metrics", mg_print_esc, 0, "data",
|
||||
send_notification(param, "{%m:%m,%m:[%lu, %d]}", MG_ESC("name"),
|
||||
MG_ESC("metrics"), MG_ESC("data"),
|
||||
(unsigned long) ourtime(NULL),
|
||||
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
||||
}
|
||||
@@ -136,19 +136,18 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
memset(&sub_opts, 0, sizeof(sub_opts));
|
||||
sub_opts.topic = mg_str(s_config.sub);
|
||||
sub_opts.qos = 2;
|
||||
|
||||
|
||||
mg_mqtt_sub(s_mqtt, &sub_opts);
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
MG_INFO(("MQTT connected, server %s", MQTT_SERVER));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
struct mg_mqtt_message *mm = ev_data;
|
||||
send_notification(c->mgr, "{%m:%m,%m:{%m: %m, %m: %m, %m: %d}}",
|
||||
mg_print_esc, 0, "name", mg_print_esc, 0, "message",
|
||||
mg_print_esc, 0, "data", mg_print_esc, 0, "topic",
|
||||
mg_print_esc, (int) mm->topic.len, mm->topic.ptr,
|
||||
mg_print_esc, 0, "data", mg_print_esc, (int) mm->data.len,
|
||||
mm->data.ptr, mg_print_esc, 0, "qos", (int) mm->qos);
|
||||
send_notification(
|
||||
c->mgr, "{%m:%m,%m:{%m: %m, %m: %m, %m: %d}}", MG_ESC("name"),
|
||||
MG_ESC("message"), MG_ESC("data"), MG_ESC("topic"), mg_print_esc,
|
||||
(int) mm->topic.len, mm->topic.ptr, MG_ESC("data"), mg_print_esc,
|
||||
(int) mm->data.len, mm->data.ptr, MG_ESC("qos"), (int) mm->qos);
|
||||
} else if (ev == MG_EV_MQTT_CMD) {
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_DEBUG(("%lu cmd %d qos %d", c->id, mm->cmd, mm->qos));
|
||||
@@ -156,8 +155,8 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
s_mqtt = NULL;
|
||||
if (s_connected) {
|
||||
s_connected = false;
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
}
|
||||
}
|
||||
(void) fnd;
|
||||
@@ -226,16 +225,14 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
mg_http_reply(c, 403, "", "Denied\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||
#ifdef DISABLE_ROUTING
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m}\n", mg_print_esc, 0,
|
||||
"url", mg_print_esc, 0, s_config.url, mg_print_esc, 0,
|
||||
"pub", mg_print_esc, 0, s_config.pub, mg_print_esc, 0,
|
||||
"sub", mg_print_esc, 0, s_config.sub);
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m}\n", MG_ESC("url"),
|
||||
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
||||
MG_ESC("sub"), MG_ESC(s_config.sub));
|
||||
#else
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m,%m:%s}\n", mg_print_esc,
|
||||
0, "url", mg_print_esc, 0, s_config.url, mg_print_esc, 0,
|
||||
"pub", mg_print_esc, 0, s_config.pub, mg_print_esc, 0,
|
||||
"sub", mg_print_esc, 0, s_config.sub, mg_print_esc, 0,
|
||||
"connected", s_connected ? "true" : "false");
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m,%m:%s}\n", MG_ESC("url"),
|
||||
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
||||
MG_ESC("sub"), MG_ESC(s_config.sub), MG_ESC("connected"),
|
||||
s_connected ? "true" : "false");
|
||||
#endif
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
// Admins only
|
||||
@@ -244,8 +241,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
update_config(&hm->body, "pub", &s_config.pub);
|
||||
update_config(&hm->body, "sub", &s_config.sub);
|
||||
if (s_mqtt) s_mqtt->is_closing = 1; // Ask to disconnect from MQTT
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
mg_http_reply(c, 403, "", "Denied\n");
|
||||
@@ -267,9 +264,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
c->data[0] = 'W'; // Mark ourselves as a event listener
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m}\n", mg_print_esc, 0, "user",
|
||||
mg_print_esc, 0, u->name, mg_print_esc, 0, "token",
|
||||
mg_print_esc, 0, u->token);
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m}\n", MG_ESC("user"),
|
||||
MG_ESC(u->name), MG_ESC("token"), MG_ESC(u->token));
|
||||
} else {
|
||||
struct mg_http_serve_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
|
||||
Reference in new issue
Block a user