mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-12 17:13:29 +07:00
Add reduced functionality version
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
#define MQTT_SUBSCRIBE_TOPIC "mg/#"
|
||||
|
||||
static time_t s_boot_timestamp = 0; // Updated by SNTP
|
||||
#ifndef DISABLE_ROUTING
|
||||
static struct mg_connection *s_sntp_conn = NULL; // SNTP connection
|
||||
#endif
|
||||
|
||||
// Define a system time alternative
|
||||
time_t ourtime(time_t *tp) {
|
||||
@@ -94,6 +96,8 @@ static void timer_metrics_fn(void *param) {
|
||||
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
||||
}
|
||||
|
||||
#ifndef DISABLE_ROUTING
|
||||
|
||||
// MQTT event handler function
|
||||
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)) {
|
||||
@@ -154,13 +158,17 @@ static void timer_sntp_fn(void *param) { // SNTP timer function. Sync up time
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// HTTP request handler function
|
||||
void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
void *fn_data) {
|
||||
if (ev == MG_EV_OPEN && c->is_listening) {
|
||||
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_sntp_fn, c->mgr);
|
||||
#endif
|
||||
s_config.url = strdup(MQTT_SERVER);
|
||||
s_config.pub = strdup(MQTT_PUBLISH_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
|
||||
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, "{%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",
|
||||
s_config.url, "pub", s_config.pub, "sub", s_config.sub,
|
||||
"connected", s_connected ? "true" : "false");
|
||||
#endif
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
// Admins only
|
||||
if (strcmp(u->name, "admin") == 0) {
|
||||
|
||||
+2124
-940
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,8 @@
|
||||
'use strict';
|
||||
import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js';
|
||||
|
||||
var devaddr = "address:port";
|
||||
|
||||
const MaxMetricsDataPoints = 50;
|
||||
|
||||
// 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:
|
||||
</p>
|
||||
|
||||
<div><code>curl -u admin:pass0 localhost:8000/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 localhost:8000/api/message/send -d 'message=hello'</code> </div>
|
||||
<div><code>curl -u admin:pass0 ${devaddr}/api/config/get</code> </div>
|
||||
<div><code>curl -u admin:pass0 ${devaddr}/api/config/set -d 'pub=mg/topic'</code> </div>
|
||||
<div><code>curl -u admin:pass0 ${devaddr}/api/message/send -d 'message=hello'</code> </div>
|
||||
|
||||
<p>
|
||||
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>
|
||||
<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>
|
||||
</div>
|
||||
</div>`;
|
||||
@@ -186,7 +188,8 @@ const Messages = function(props) {
|
||||
method: 'post',
|
||||
body: `message=${encodeURIComponent(txt)}`
|
||||
}).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`
|
||||
<div class="section">
|
||||
<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">
|
||||
<span class="addon nowrap">Publish message:</span>
|
||||
<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)} />
|
||||
</div>
|
||||
<div class="msg">
|
||||
|
||||
Reference in New Issue
Block a user