mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-24 20:21:49 +00:00
Rework dashboard
This commit is contained in:
1 parent
cac7f653c9
commit
31c7d66245
7 files changed
+1104
-920
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
PROG ?= example
|
PROG ?= example
|
||||||
SOURCES ?= ../../mongoose.c main.c net.c packed_fs.c
|
SOURCES ?= ../../mongoose.c main.c net.c packed_fs.c
|
||||||
CFLAGS ?= -I../.. -DMG_ENABLE_PACKED_FS=1 $(EXTRA)
|
CFLAGS ?= -I../.. -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_LINES=1 $(EXTRA)
|
||||||
FILES_TO_EMBED ?= $(wildcard web_root/*)
|
FILES_TO_EMBED ?= $(wildcard web_root/*)
|
||||||
ROOT ?= $(realpath $(CURDIR)/../../..)
|
ROOT ?= $(realpath $(CURDIR)/../../..)
|
||||||
DOCKER ?= docker run --rm -e Tmp=. -e WINEDEBUG=-all -v $(ROOT):$(ROOT) -w $(CURDIR)
|
DOCKER ?= docker run --rm -e Tmp=. -e WINEDEBUG=-all -v $(ROOT):$(ROOT) -w $(CURDIR)
|
||||||
|
|||||||
@@ -2,15 +2,17 @@
|
|||||||
// All rights reserved
|
// All rights reserved
|
||||||
|
|
||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
const char *s_listening_url = "http://0.0.0.0:8000";
|
||||||
|
|
||||||
void device_dashboard_fn(struct mg_connection *, int, void *, void *);
|
void device_dashboard_fn(struct mg_connection *, int, void *, void *);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct mg_mgr mgr;
|
struct mg_mgr mgr;
|
||||||
mg_log_set("3");
|
mg_log_set("2"); // Set to 3 for debug, to 4 for very verbose level
|
||||||
mg_mgr_init(&mgr);
|
mg_mgr_init(&mgr);
|
||||||
mg_http_listen(&mgr, "http://0.0.0.0:8000", device_dashboard_fn, &mgr);
|
mg_http_listen(&mgr, s_listening_url, device_dashboard_fn, &mgr);
|
||||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
MG_INFO(("Listening on %s", s_listening_url));
|
||||||
|
while (mgr.conns != NULL) mg_mgr_poll(&mgr, 500);
|
||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@ static struct config {
|
|||||||
} s_config;
|
} s_config;
|
||||||
|
|
||||||
static struct mg_connection *s_mqtt = NULL; // MQTT connection
|
static struct mg_connection *s_mqtt = NULL; // MQTT connection
|
||||||
|
static bool s_connected = false; // MQTT connection established
|
||||||
|
|
||||||
// Try to update a single configuration value
|
// Try to update a single configuration value
|
||||||
static void update_config(struct mg_str *body, const char *name, char **value) {
|
static void update_config(struct mg_str *body, const char *name, char **value) {
|
||||||
@@ -66,8 +67,10 @@ static void send_notification(struct mg_mgr *mgr, const char *name,
|
|||||||
const char *data) {
|
const char *data) {
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
if (c->label[0] == 'W')
|
if (c->label[0] != 'W') continue;
|
||||||
mg_http_printf_chunk(c, "{\"name\": \"%s\", \"data\": %s}", name, data);
|
// c->is_hexdumping = 1;
|
||||||
|
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{\"name\": \"%s\", \"data\": %s}", name,
|
||||||
|
data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,33 +79,40 @@ static void timer_metrics_fn(void *param) {
|
|||||||
char buf[50];
|
char buf[50];
|
||||||
mg_snprintf(buf, sizeof(buf), "[ %lu, %d ]", (unsigned long) time(NULL),
|
mg_snprintf(buf, sizeof(buf), "[ %lu, %d ]", (unsigned long) time(NULL),
|
||||||
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
||||||
// MG_INFO(("%s", buf));
|
|
||||||
send_notification(param, "metrics", buf);
|
send_notification(param, "metrics", buf);
|
||||||
|
// MG_INFO(("%s", buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
// MQTT event handler function
|
// MQTT event handler function
|
||||||
static void mqtt_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
static void mqtt_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||||
if (ev == MG_EV_MQTT_OPEN) {
|
if (ev == MG_EV_MQTT_OPEN) {
|
||||||
struct mg_str topic = mg_str(s_config.pub);
|
s_connected = true;
|
||||||
mg_mqtt_sub(s_mqtt, &topic, 1);
|
// c->is_hexdumping = 1;
|
||||||
|
mg_mqtt_sub(s_mqtt, mg_str(s_config.sub), 1);
|
||||||
|
send_notification(c->mgr, "config", "null");
|
||||||
} else if (ev == MG_EV_MQTT_MSG) {
|
} else if (ev == MG_EV_MQTT_MSG) {
|
||||||
struct mg_mqtt_message *mm = evd;
|
struct mg_mqtt_message *mm = evd;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
snprintf(buf, sizeof(buf), "{\"topic\":\"%.*s\",\"data\":\"%.*s\"}",
|
snprintf(buf, sizeof(buf), "{\"topic\":\"%.*s\",\"data\":\"%.*s\"}",
|
||||||
(int) mm->topic.len, mm->topic.ptr, (int) mm->data.len,
|
(int) mm->topic.len, mm->topic.ptr, (int) mm->data.len,
|
||||||
mm->data.ptr);
|
mm->data.ptr);
|
||||||
send_notification(param, "message", buf);
|
send_notification(c->mgr, "message", buf);
|
||||||
} else if (ev == MG_EV_CLOSE) {
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
s_mqtt = NULL;
|
s_mqtt = NULL;
|
||||||
|
if (s_connected) {
|
||||||
|
s_connected = false;
|
||||||
|
send_notification(c->mgr, "config", "null");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
(void) fnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep MQTT connection open - reconnect if closed
|
// Keep MQTT connection open - reconnect if closed
|
||||||
static void timer_metrics_fn(void *param) {
|
static void timer_mqtt_fn(void *param) {
|
||||||
struct mg_mgr *mgr = (struct mg_mgr *) param;
|
struct mg_mgr *mgr = (struct mg_mgr *) param;
|
||||||
if (s_mqtt == NULL) {
|
if (s_mqtt == NULL) {
|
||||||
struct mg_mqtt_opts opts = {};
|
struct mg_mqtt_opts opts = {};
|
||||||
s_mqtt = mg_mqtt_connect(mgr, s_config.rl, &opts, mqtt_fn, NULL);
|
s_mqtt = mg_mqtt_connect(mgr, s_config.url, &opts, mqtt_fn, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,8 +137,10 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
mg_printf(c, "%s", "HTTP/1.1 403 Denied\r\nContent-Length: 0\r\n\r\n");
|
mg_printf(c, "%s", "HTTP/1.1 403 Denied\r\nContent-Length: 0\r\n\r\n");
|
||||||
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
mg_http_printf_chunk(c, "{\"url\":\"%s\",\"pub\":\"%s\",\"sub\":\"%s\"}",
|
mg_http_printf_chunk(
|
||||||
s_config.url, s_config.pub, s_config.sub);
|
c, "{\"url\":\"%s\",\"pub\":\"%s\",\"sub\":\"%s\",\"connected\":%s}",
|
||||||
|
s_config.url, s_config.pub, s_config.sub,
|
||||||
|
s_connected ? "true" : "false");
|
||||||
mg_http_printf_chunk(c, "");
|
mg_http_printf_chunk(c, "");
|
||||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||||
// Admins only
|
// Admins only
|
||||||
@@ -136,6 +148,7 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
update_config(&hm->body, "url", &s_config.url);
|
update_config(&hm->body, "url", &s_config.url);
|
||||||
update_config(&hm->body, "pub", &s_config.pub);
|
update_config(&hm->body, "pub", &s_config.pub);
|
||||||
update_config(&hm->body, "sub", &s_config.sub);
|
update_config(&hm->body, "sub", &s_config.sub);
|
||||||
|
if (s_mqtt) s_mqtt->is_closing = 1; // Ask to disconnect from MQTT
|
||||||
send_notification(fn_data, "config", "null");
|
send_notification(fn_data, "config", "null");
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||||
} else {
|
} else {
|
||||||
@@ -143,14 +156,15 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
}
|
}
|
||||||
} else if (mg_http_match_uri(hm, "/api/message/send")) {
|
} else if (mg_http_match_uri(hm, "/api/message/send")) {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
if (mg_http_get_var(&hm->body, "message", buf + 1, sizeof(buf) - 2) > 0) {
|
if (s_connected &&
|
||||||
buf[0] = buf[strlen(buf)] = '"';
|
mg_http_get_var(&hm->body, "message", buf, sizeof(buf)) > 0) {
|
||||||
send_notification(fn_data, "message", buf);
|
mg_mqtt_pub(s_mqtt, mg_str(s_config.pub), mg_str(buf), 1, false);
|
||||||
}
|
}
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||||
} else if (mg_http_match_uri(hm, "/api/watch")) {
|
} else if (mg_http_match_uri(hm, "/api/watch")) {
|
||||||
c->label[0] = 'W'; // Mark ourselves as a event listener
|
c->label[0] = 'W'; // Mark ourselves as a event listener
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
mg_ws_upgrade(c, hm, NULL);
|
||||||
|
// mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
mg_http_printf_chunk(c, "{\"user\":\"%s\",\"token\":\"%s\"}\n", u->name,
|
mg_http_printf_chunk(c, "{\"user\":\"%s\",\"token\":\"%s\"}\n", u->name,
|
||||||
@@ -158,7 +172,7 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
|||||||
mg_http_printf_chunk(c, "");
|
mg_http_printf_chunk(c, "");
|
||||||
} else {
|
} else {
|
||||||
struct mg_http_serve_opts opts = {0};
|
struct mg_http_serve_opts opts = {0};
|
||||||
#if 0
|
#if 1
|
||||||
opts.root_dir = "/web_root";
|
opts.root_dir = "/web_root";
|
||||||
opts.fs = &mg_fs_packed;
|
opts.fs = &mg_fs_packed;
|
||||||
#else
|
#else
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
Binary file not shown.
|
Before Width: | Height: | Size: 196 KiB After Width: | Height: | Size: 283 KiB |
@@ -3,6 +3,23 @@ import {Component, h, html, render, useEffect, useState, useRef} from './preact.
|
|||||||
|
|
||||||
const MaxMetricsDataPoints = 50;
|
const MaxMetricsDataPoints = 50;
|
||||||
|
|
||||||
|
// This simple publish/subscribe is used to pass notifications that were
|
||||||
|
// received from the server, to all child components of the app.
|
||||||
|
var PubSub = (function() {
|
||||||
|
var handlers = {}, id = 0;
|
||||||
|
return {
|
||||||
|
subscribe: function(fn) {
|
||||||
|
handlers[id++] = fn;
|
||||||
|
},
|
||||||
|
unsubscribe: function(id) {
|
||||||
|
delete handlers[id];
|
||||||
|
},
|
||||||
|
publish: function(data) {
|
||||||
|
for (var k in handlers) handlers[k](data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
const Nav = props => html`
|
const Nav = props => html`
|
||||||
<div style="background: #333; padding: 0.5em; color: #fff;">
|
<div style="background: #333; padding: 0.5em; color: #fff;">
|
||||||
<div class="container d-flex">
|
<div class="container d-flex">
|
||||||
@@ -41,23 +58,69 @@ const Hero = props => html`
|
|||||||
using <code>curl</code> command-line utility:
|
using <code>curl</code> command-line utility:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div><code>curl localhost:8000/api/config/get</code> - get current device configuration</div>
|
<div><code>curl localhost:8000/api/config/get</code> </div>
|
||||||
<div><code>curl localhost:8000/api/config/set -d 'value1=7&value2=hello'</code> - set device configuration</div>
|
<div><code>curl localhost:8000/api/config/set -d 'value1=7&value2=hello'</code> </div>
|
||||||
<div><code>curl localhost:8000/api/message/send -d 'msg=hello'</code> - send MQTT message</div>
|
<div><code>curl localhost:8000/api/message/send -d 'msg=hello'</code> </div>
|
||||||
<div><code>curl localhost:8000/api/watch</code> - get notifications: MQTT messages, configuration, sensor data</div>
|
|
||||||
|
<p>
|
||||||
|
A device can send notifications to this dashboard at anytime. Notifications
|
||||||
|
are sent over Websocket at URI <code>curl localhost:8000/api/watch</code>
|
||||||
|
as JSON strings <code>{"name": "..", "data": ...}</code>
|
||||||
|
</p>
|
||||||
|
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
|
const Login = function(props) {
|
||||||
|
const [user, setUser] = useState('');
|
||||||
|
const [pass, setPass] = useState('');
|
||||||
|
const login = ev =>
|
||||||
|
fetch(
|
||||||
|
'/api/login',
|
||||||
|
{headers: {Authorization: 'Basic ' + btoa(user + ':' + pass)}})
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(r => r && props.login(r))
|
||||||
|
.catch(err => err);
|
||||||
|
return html`
|
||||||
|
<div class="rounded border" style="max-width: 480px; margin: 0 auto; margin-top: 5em; background: #eee; ">
|
||||||
|
<div style="padding: 2em; ">
|
||||||
|
<h1 style="color: #666;">Device Dashboard Login </h1>
|
||||||
|
<div style="margin: 0.5em 0;">
|
||||||
|
<input type='text' placeholder='Name' style="width: 100%;"
|
||||||
|
oninput=${ev => setUser(ev.target.value)} value=${user} />
|
||||||
|
</div>
|
||||||
|
<div style="margin: 0.5em 0;">
|
||||||
|
<input type="password" placeholder="Password" style="width: 100%;"
|
||||||
|
oninput=${ev => setPass(ev.target.value)} value=${pass}
|
||||||
|
onchange=${login} />
|
||||||
|
</div>
|
||||||
|
<div style="margin: 1em 0;">
|
||||||
|
<button class="btn" style="width: 100%; background: #8aa;"
|
||||||
|
disabled=${!user || !pass} onclick=${login}> Login </button>
|
||||||
|
</div>
|
||||||
|
<div style="color: #777; margin-top: 2em;">
|
||||||
|
Valid logins: admin:pass0, user1:pass1, user2:pass2
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
const Configuration = function(props) {
|
const Configuration = function(props) {
|
||||||
const [url, setUrl] = useState('');
|
const [url, setUrl] = useState('');
|
||||||
const [pub, setPub] = useState('');
|
const [pub, setPub] = useState('');
|
||||||
const [sub, setSub] = useState('');
|
const [sub, setSub] = useState('');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setUrl(props.config.url);
|
const id = PubSub.subscribe(function(msg) {
|
||||||
setPub(props.config.pub);
|
if (msg.name == 'newconfig') {
|
||||||
setSub(props.config.sub);
|
setUrl(msg.data.url);
|
||||||
}, [props.config.url, props.config.pub, props.config.sub])
|
setPub(msg.data.pub);
|
||||||
|
setSub(msg.data.sub);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return PubSub.unsubscribe(id);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const update = (name, val) => fetch('/api/config/set', {
|
const update = (name, val) => fetch('/api/config/set', {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: `${name}=${encodeURIComponent(val)}`
|
body: `${name}=${encodeURIComponent(val)}`
|
||||||
@@ -65,6 +128,7 @@ const Configuration = function(props) {
|
|||||||
const updateurl = ev => update('url', url);
|
const updateurl = ev => update('url', url);
|
||||||
const updatepub = ev => update('pub', pub);
|
const updatepub = ev => update('pub', pub);
|
||||||
const updatesub = ev => update('sub', sub);
|
const updatesub = ev => update('sub', sub);
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div style="margin: 0 0.3em;">
|
<div style="margin: 0 0.3em;">
|
||||||
<h3 style="background: #c03434; color: #fff; padding: 0.4em;">
|
<h3 style="background: #c03434; color: #fff; padding: 0.4em;">
|
||||||
@@ -105,62 +169,44 @@ const Configuration = function(props) {
|
|||||||
</div>`;
|
</div>`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Login = function(props) {
|
|
||||||
const [user, setUser] = useState('');
|
|
||||||
const [pass, setPass] = useState('');
|
|
||||||
const login = ev =>
|
|
||||||
fetch(
|
|
||||||
'/api/login',
|
|
||||||
{headers: {Authorization: 'Basic ' + btoa(user + ':' + pass)}})
|
|
||||||
.then(r => r.json())
|
|
||||||
.then(r => r && props.login(r))
|
|
||||||
.catch(err => err);
|
|
||||||
return html`
|
|
||||||
<div class="rounded border" style="max-width: 480px; margin: 0 auto; margin-top: 5em; background: #eee; ">
|
|
||||||
<div style="padding: 2em; ">
|
|
||||||
<h1 style="color: #666;">Device Dashboard Login </h1>
|
|
||||||
<div style="margin: 0.5em 0;">
|
|
||||||
<input type='text' placeholder='Name' style="width: 100%;"
|
|
||||||
oninput=${ev => setUser(ev.target.value)} value=${user} />
|
|
||||||
</div>
|
|
||||||
<div style="margin: 0.5em 0;">
|
|
||||||
<input type="password" placeholder="Password" style="width: 100%;"
|
|
||||||
oninput=${ev => setPass(ev.target.value)} value=${pass}
|
|
||||||
onchange=${login} />
|
|
||||||
</div>
|
|
||||||
<div style="margin: 1em 0;">
|
|
||||||
<button class="btn" style="width: 100%; background: #8aa;"
|
|
||||||
disabled=${!user || !pass} onclick=${login}> Login </button>
|
|
||||||
</div>
|
|
||||||
<div style="color: #777; margin-top: 2em;">
|
|
||||||
Valid logins: admin:pass0, user1:pass1, user2:pass2
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const Message = text => html`<div style="margin: 0.5em 0;">${text}</div>`;
|
const Message = m => html`<div style="margin: 0.5em 0;">
|
||||||
|
<span class="topic">topic: ${m.message.topic} </span>
|
||||||
|
<span class="data">data: ${m.message.data}</span>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
const Messages = function(props) {
|
const Messages = function(props) {
|
||||||
const [message, setMessage] = useState('');
|
const [messages, setMessages] = useState([]);
|
||||||
|
const [cfg, setCfg] = useState({});
|
||||||
|
const [txt, setTxt] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const id = PubSub.subscribe(function(msg) {
|
||||||
|
if (msg.name == 'newconfig') setCfg(x => msg.data);
|
||||||
|
if (msg.name == 'message') setMessages(x => x.concat([msg.data]));
|
||||||
|
});
|
||||||
|
return PubSub.unsubscribe(id);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const sendmessage = ev => fetch('/api/message/send', {
|
const sendmessage = ev => fetch('/api/message/send', {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: `message=${encodeURIComponent(message)}`
|
body: `message=${encodeURIComponent(txt)}`
|
||||||
}).then(r => setMessage(''));
|
}).then(r => setTxt(''));
|
||||||
|
|
||||||
const messages = props.messages.map(
|
|
||||||
text => html`<div style="margin: 0.5em 0;">${text}</div>`);
|
|
||||||
return html`
|
return html`
|
||||||
<div style="margin: 0 0.3em;">
|
<div style="margin: 0 0.3em;">
|
||||||
<h3 style="background: #30c040; color: #fff; padding: 0.4em;">MQTT messages</h3>
|
<h3 style="background: #30c040; color: #fff; padding: 0.4em;">MQTT messages</h3>
|
||||||
|
<div>
|
||||||
|
MQTT server status: <b>${cfg.connected ? 'connected' : 'diconnected'}</b>
|
||||||
|
</div>
|
||||||
<div style="height: 10em; overflow: auto; padding: 0.5em; " class="border">
|
<div style="height: 10em; overflow: auto; padding: 0.5em; " class="border">
|
||||||
${messages}
|
${messages.map(message => h(Message, {message}))}
|
||||||
</div>
|
</div>
|
||||||
<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=${message} onchange=${sendmessage}
|
value=${txt} onchange=${sendmessage}
|
||||||
oninput=${ev => setMessage(ev.target.value)} />
|
oninput=${ev => setTxt(ev.target.value)} />
|
||||||
</div>
|
</div>
|
||||||
<div class="msg">
|
<div class="msg">
|
||||||
Message gets passed to the device via REST. Then a device sends it to
|
Message gets passed to the device via REST. Then a device sends it to
|
||||||
@@ -240,14 +286,22 @@ const SVG = function(props) {
|
|||||||
|
|
||||||
|
|
||||||
const Chart = function(props) {
|
const Chart = function(props) {
|
||||||
let xmax = 0, missing = MaxMetricsDataPoints - props.metrics.length;
|
const [data, setData] = useState([]);
|
||||||
|
useEffect(() => {
|
||||||
|
const id = PubSub.subscribe(function(msg) {
|
||||||
|
if (msg.name != 'metrics') return;
|
||||||
|
setData(x => x.concat([msg.data]).splice(-MaxMetricsDataPoints));
|
||||||
|
});
|
||||||
|
return PubSub.unsubscribe(id);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
let xmax = 0, missing = MaxMetricsDataPoints - data.length;
|
||||||
if (missing > 0) xmax = Math.round(Date.now() / 1000) + missing;
|
if (missing > 0) xmax = Math.round(Date.now() / 1000) + missing;
|
||||||
return html`
|
return html`
|
||||||
<div style="margin: 0 0.3em;">
|
<div style="margin: 0 0.3em;">
|
||||||
<h3 style="background: #ec3; color: #fff; padding: 0.4em;">Data Chart</h3>
|
<h3 style="background: #ec3; color: #fff; padding: 0.4em;">Data Chart</h3>
|
||||||
<div style="overflow: auto; padding: 0.5em;" class="">
|
<div style="overflow: auto; padding: 0.5em;" class="">
|
||||||
<${SVG} height=240 width=600 ymin=0 ymax=20
|
<${SVG} height=240 width=600 ymin=0 ymax=20 xmax=${xmax} data=${data} />
|
||||||
xmax=${xmax} data=${props.metrics} />
|
|
||||||
</div>
|
</div>
|
||||||
<div class="msg">
|
<div class="msg">
|
||||||
This chart plots live sensor data, sent by a device via /api/watch.
|
This chart plots live sensor data, sent by a device via /api/watch.
|
||||||
@@ -256,19 +310,17 @@ const Chart = function(props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const App = function(props) {
|
const App = function(props) {
|
||||||
const [messages, setMessages] = useState([]);
|
|
||||||
const [metrics, setMetrics] = useState([]);
|
|
||||||
const [user, setUser] = useState('');
|
const [user, setUser] = useState('');
|
||||||
const [config, setConfig] = useState({});
|
|
||||||
|
|
||||||
const refresh = () => fetch('/api/config/get', {headers: {Authorization: ''}})
|
const getconfig = () =>
|
||||||
.then(r => r.json())
|
fetch('/api/config/get', {headers: {Authorization: ''}})
|
||||||
.then(r => setConfig(r));
|
.then(r => r.json())
|
||||||
|
.then(r => PubSub.publish({name: 'newconfig', data: r}));
|
||||||
|
|
||||||
const login = function(u) {
|
const login = function(u) {
|
||||||
document.cookie = `access_token=${u.token};path=/;max-age=3600`;
|
document.cookie = `access_token=${u.token};path=/;max-age=3600`;
|
||||||
setUser(u.user);
|
setUser(u.user);
|
||||||
refresh();
|
return getconfig();
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = ev => {
|
const logout = ev => {
|
||||||
@@ -276,48 +328,50 @@ const App = function(props) {
|
|||||||
setUser('');
|
setUser('');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Watch for notifications. As soon as a notification arrives, pass it on
|
||||||
|
// to all subscribed components
|
||||||
const watch = function() {
|
const watch = function() {
|
||||||
var f = function(reader) {
|
var l = window.location, proto = l.protocol.replace('http', 'ws');
|
||||||
return reader.read().then(function(result) {
|
var tid, wsURI = proto + '//' + l.host + '/api/watch'
|
||||||
var data = String.fromCharCode.apply(null, result.value);
|
var reconnect = function() {
|
||||||
var msg = JSON.parse(data);
|
var ws = new WebSocket(wsURI);
|
||||||
if (msg.name == 'config') {
|
ws.onmessage = function(ev) {
|
||||||
refresh();
|
try {
|
||||||
} else if (msg.name == 'message') {
|
var msg = JSON.parse(ev.data);
|
||||||
setMessages(m => m.concat([msg.data]));
|
PubSub.publish(msg);
|
||||||
} else if (msg.name == 'metrics') {
|
if (msg.name != 'metrics') console.log('ws->', msg);
|
||||||
setMetrics(m => m.concat([msg.data]).splice(-MaxMetricsDataPoints));
|
} catch (e) {
|
||||||
|
console.log('Invalid ws frame:', ev.data); // eslint-disable-line
|
||||||
}
|
}
|
||||||
// console.log(msg);
|
};
|
||||||
if (!result.done) return f(reader);
|
ws.onclose = function() {
|
||||||
});
|
clearTimeout(tid);
|
||||||
|
tid = setTimeout(reconnect, 1000);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
fetch('/api/watch', {headers: {Authorization: ''}})
|
reconnect();
|
||||||
.then(r => r.body.getReader())
|
|
||||||
.then(f)
|
|
||||||
.catch(e => setTimeout(watch, 1000));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Called once at init time
|
||||||
fetch('/api/login', {headers: {Authorization: ''}})
|
fetch('/api/login', {headers: {Authorization: ''}})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(r => login(r))
|
.then(r => login(r))
|
||||||
.catch(err => setUser(''));
|
.catch(err => setUser(''));
|
||||||
refresh();
|
|
||||||
watch();
|
watch();
|
||||||
|
PubSub.subscribe(msg => msg.name == 'config' && getconfig());
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!user) return html`<${Login} login=${login} />`;
|
if (!user) return html`<${Login} login=${login} />`;
|
||||||
const admin = user == 'admin';
|
|
||||||
const cs = admin ? html`<${Configuration} config=${config} />` : '';
|
|
||||||
return html`
|
return html`
|
||||||
<${Nav} user=${user} logout=${logout} />
|
<${Nav} user=${user} logout=${logout} />
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<${Hero} />
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col c6"><${Chart} metrics=${metrics} /></div>
|
<div class="col col-6"><${Hero} /></div>
|
||||||
<div class="col c6">${cs}</div>
|
<div class="col col-6"><${Chart} /></div>
|
||||||
<div class="col c6"><${Messages} messages=${messages} /></div>
|
<div class="col col-6">${user == 'admin' && h(Configuration)}</div>
|
||||||
|
<div class="col col-6"><${Messages} /></div>
|
||||||
</div>
|
</div>
|
||||||
<${Footer} />
|
<${Footer} />
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|||||||
@@ -17,21 +17,24 @@ a, a:visited, a:active { color: #55f; }
|
|||||||
.border { border: 1px solid #ddd; }
|
.border { border: 1px solid #ddd; }
|
||||||
.rounded { border-radius: 0.5em; }
|
.rounded { border-radius: 0.5em; }
|
||||||
.nowrap { white-space: nowrap; }
|
.nowrap { white-space: nowrap; }
|
||||||
.msg { background: #def; border-left: 5px solid #59d; padding: 1em; margin: 1em 0; }
|
.msg { background: #def; border-left: 5px solid #59d; padding: 0.5em; font-size: 90%; margin: 1em 0; }
|
||||||
.row { margin: 1% 0; overflow: auto; }
|
.row { display: flex; flex-wrap: wrap; }
|
||||||
.col { float: left; }
|
.col { margin: 0; padding: 0; overflow: auto; }
|
||||||
.table, .c12 { width: 100%; }
|
.col-12 { width: 100%; }
|
||||||
.c11 { width: 91.66%; }
|
.col-11 { width: 91.66%; }
|
||||||
.c10 { width: 83.33%; }
|
.col-10 { width: 83.33%; }
|
||||||
.c9 { width: 75%; }
|
.col-9 { width: 75%; }
|
||||||
.c8 { width: 66.66%; }
|
.col-8 { width: 66.66%; }
|
||||||
.c7 { width: 58.33%; }
|
.col-7 { width: 58.33%; }
|
||||||
.c6 { width: 50%; }
|
.col-6 { width: 50%; }
|
||||||
.c5 { width: 41.66%; }
|
.col-5 { width: 41.66%; }
|
||||||
.c4 { width: 33.33%; }
|
.col-4 { width: 33.33%; }
|
||||||
.c3 { width: 25%; }
|
.col-3 { width: 25%; }
|
||||||
.c2 { width: 16.66%; }
|
.col-2 { width: 16.66%; }
|
||||||
.c1 { width: 8.33%; }
|
.col-1 { width: 8.33%; }
|
||||||
|
|
||||||
|
.topic { background: #fea; padding: 0.2em 1em; border-radius: 0.4em; }
|
||||||
|
.data { background: #aef; padding: 0.2em 1em; border-radius: 0.4em; }
|
||||||
|
|
||||||
@media (min-width: 1310px) { .container { margin: auto; width: 1270px; } }
|
@media (min-width: 1310px) { .container { margin: auto; width: 1270px; } }
|
||||||
@media (max-width: 870px) { .row .col { width: 100%; } }
|
@media (max-width: 920px) { .row .col { width: 100%; } }
|
||||||
Reference in new issue
Block a user