Merge pull request #1944 from cesanta/devdash

Add reduced functionality version
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2023-01-10 12:31:03 +00:00
commit d70ab3cfa8
3 files changed
+2146 -946

No files matched your search

+13
View File
@@ -10,7 +10,9 @@
#define MQTT_SUBSCRIBE_TOPIC "mg/#" #define MQTT_SUBSCRIBE_TOPIC "mg/#"
static time_t s_boot_timestamp = 0; // Updated by SNTP static time_t s_boot_timestamp = 0; // Updated by SNTP
#ifndef DISABLE_ROUTING
static struct mg_connection *s_sntp_conn = NULL; // SNTP connection static struct mg_connection *s_sntp_conn = NULL; // SNTP connection
#endif
// Define a system time alternative // Define a system time alternative
time_t ourtime(time_t *tp) { time_t ourtime(time_t *tp) {
@@ -94,6 +96,8 @@ static void timer_metrics_fn(void *param) {
10 + (int) ((double) rand() * 10 / RAND_MAX)); 10 + (int) ((double) rand() * 10 / RAND_MAX));
} }
#ifndef DISABLE_ROUTING
// MQTT event handler function // MQTT event handler function
static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) { static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
if (ev == MG_EV_CONNECT && mg_url_is_ssl(s_config.url)) { if (ev == MG_EV_CONNECT && mg_url_is_ssl(s_config.url)) {
@@ -154,13 +158,17 @@ static void timer_sntp_fn(void *param) { // SNTP timer function. Sync up time
} }
} }
#endif
// HTTP request handler function // HTTP request handler function
void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data, void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
void *fn_data) { void *fn_data) {
if (ev == MG_EV_OPEN && c->is_listening) { if (ev == MG_EV_OPEN && c->is_listening) {
mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_metrics_fn, c->mgr); mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_metrics_fn, c->mgr);
#ifndef DISABLE_ROUTING
mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_mqtt_fn, c->mgr); mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_mqtt_fn, c->mgr);
mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_sntp_fn, c->mgr); mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_sntp_fn, c->mgr);
#endif
s_config.url = strdup(MQTT_SERVER); s_config.url = strdup(MQTT_SERVER);
s_config.pub = strdup(MQTT_PUBLISH_TOPIC); s_config.pub = strdup(MQTT_PUBLISH_TOPIC);
s_config.sub = strdup(MQTT_SUBSCRIBE_TOPIC); s_config.sub = strdup(MQTT_SUBSCRIBE_TOPIC);
@@ -179,9 +187,14 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
// All URIs starting with /api/ must be authenticated // All URIs starting with /api/ must be authenticated
mg_http_reply(c, 403, "", "Denied\n"); mg_http_reply(c, 403, "", "Denied\n");
} else if (mg_http_match_uri(hm, "/api/config/get")) { } else if (mg_http_match_uri(hm, "/api/config/get")) {
#ifdef DISABLE_ROUTING
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q,%Q:%Q}\n", "url", s_config.url,
"pub", s_config.pub, "sub", s_config.sub);
#else
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q,%Q:%Q,%Q:%s}\n", "url", mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q,%Q:%Q,%Q:%s}\n", "url",
s_config.url, "pub", s_config.pub, "sub", s_config.sub, s_config.url, "pub", s_config.pub, "sub", s_config.sub,
"connected", s_connected ? "true" : "false"); "connected", s_connected ? "true" : "false");
#endif
} else if (mg_http_match_uri(hm, "/api/config/set")) { } else if (mg_http_match_uri(hm, "/api/config/set")) {
// Admins only // Admins only
if (strcmp(u->name, "admin") == 0) { if (strcmp(u->name, "admin") == 0) {
File diff suppressed because it is too large. Load diff
+9 -6
View File
@@ -1,6 +1,8 @@
'use strict'; 'use strict';
import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js'; import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js';
var devaddr = "address:port";
const MaxMetricsDataPoints = 50; const MaxMetricsDataPoints = 50;
// This simple publish/subscribe is used to pass notifications that were // This simple publish/subscribe is used to pass notifications that were
@@ -53,14 +55,14 @@ const Hero = props => html`
using <code>curl</code> command-line utility: using <code>curl</code> command-line utility:
</p> </p>
<div><code>curl -u admin:pass0 localhost:8000/api/config/get</code> </div> <div><code>curl -u admin:pass0 ${devaddr}/api/config/get</code> </div>
<div><code>curl -u admin:pass0 localhost:8000/api/config/set -d 'pub=mg/topic'</code> </div> <div><code>curl -u admin:pass0 ${devaddr}/api/config/set -d 'pub=mg/topic'</code> </div>
<div><code>curl -u admin:pass0 localhost:8000/api/message/send -d 'message=hello'</code> </div> <div><code>curl -u admin:pass0 ${devaddr}/api/message/send -d 'message=hello'</code> </div>
<p> <p>
The device can send notifications to this dashboard at anytime. Notifications The device can send notifications to this dashboard at anytime. Notifications
are sent over WebSocket at URI <code>/api/watch</code> as JSON strings: <code>{"name": "..", "data": ...}</code> are sent over WebSocket at URI <code>/api/watch</code> as JSON strings: <code>{"name": "..", "data": ...}</code>
<div>Try <code>wscat --auth user1:pass1 --connect ws://localhost:8000/api/watch</code></div> <div>Try <code>wscat --auth user1:pass1 --connect ws://${devaddr}/api/watch</code></div>
</p> </p>
</div> </div>
</div>`; </div>`;
@@ -186,7 +188,8 @@ const Messages = function(props) {
method: 'post', method: 'post',
body: `message=${encodeURIComponent(txt)}` body: `message=${encodeURIComponent(txt)}`
}).then(r => setTxt('')); }).then(r => setTxt(''));
const connstatus = props.config.connected ? 'connected' : 'disconnected'; const routing = "connected" in props.config;
const connstatus = !routing ? 'This device has no MQTT functionality' : props.config.connected ? 'connected' : 'disconnected';
return html` return html`
<div class="section"> <div class="section">
<h3 style="background: #30c040; color: #fff; padding: 0.4em;">MQTT messages</h3> <h3 style="background: #30c040; color: #fff; padding: 0.4em;">MQTT messages</h3>
@@ -199,7 +202,7 @@ const Messages = function(props) {
<div style="margin: 0.5em 0; display: flex"> <div style="margin: 0.5em 0; display: flex">
<span class="addon nowrap">Publish message:</span> <span class="addon nowrap">Publish message:</span>
<input placeholder="type and press enter..." style="flex: 1 100%;" <input placeholder="type and press enter..." style="flex: 1 100%;"
value=${txt} onchange=${sendmessage} value=${txt} onchange=${sendmessage} disabled=${!routing}
oninput=${ev => setTxt(ev.target.value)} /> oninput=${ev => setTxt(ev.target.value)} />
</div> </div>
<div class="msg"> <div class="msg">