'use strict'; import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js'; const MaxMetricsDataPoints = 50; const Nav = props => html`
Your Product
Logged in as: ${props.user} logout
`; const Footer = props => html`
Copyright (c) Your Company
`; const Hero = props => html`

Interactive Device Dashboard

This device dashboard is developed with modern and compact Preact framework, in order to fit on a very small devices. This is a hybrid server which provides both static and dynamic content. Static files, like CSS/JS/HTML or images, are compiled into the server binary. This UI uses the REST API implemented by the device, which you can examine using curl command-line utility:

curl localhost:8000/api/config/get - get current device configuration
curl localhost:8000/api/config/set -d 'value1=7&value2=hello' - set device configuration
curl localhost:8000/api/message/send -d 'msg=hello' - send MQTT message
curl localhost:8000/api/watch - get notifications: MQTT messages, configuration, sensor data
`; const Configuration = function(props) { const [url, setUrl] = useState(''); const [pub, setPub] = useState(''); const [sub, setSub] = useState(''); useEffect(() => { setUrl(props.config.url); setPub(props.config.pub); setSub(props.config.sub); }, [props.config.url, props.config.pub, props.config.sub]) const update = (name, val) => fetch('/api/config/set', { method: 'post', body: `${name}=${encodeURIComponent(val)}` }).catch(err => err); const updateurl = ev => update('url', url); const updatepub = ev => update('pub', pub); const updatesub = ev => update('sub', sub); return html`

Device Configuration

MQTT server: setUrl(ev.target.value)} />
Subscribe topic: setSub(ev.target.value)} />
Publish topic: setPub(ev.target.value)} />
Device keeps a persistent connection to the configured MQTT server. Changes to this configuration are propagated to all dashboards: try changing them in this dashboard and observe changes in the other opened dashboard.
Note: administrators can see this section and can change device configuration, whilst users cannot.
`; }; 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`

Device Dashboard Login

setUser(ev.target.value)} value=${user} />
setPass(ev.target.value)} value=${pass} onchange=${login} />
Valid logins: admin:pass0, user1:pass1, user2:pass2
`; }; const Message = text => html`
${text}
`; const Messages = function(props) { const [message, setMessage] = useState(''); const sendmessage = ev => fetch('/api/message/send', { method: 'post', body: `message=${encodeURIComponent(message)}` }).then(r => setMessage('')); const messages = props.messages.map( text => html`
${text}
`); return html`

MQTT messages

${messages}
Publish message: setMessage(ev.target.value)} />
Message gets passed to the device via REST. Then a device sends it to the MQTT server over MQTT. All MQTT messages on a subscribed topic received by a device, are propagated to this dashboard via the /api/watch.
`; }; // Expected arguments: // data: timeseries, e.g. [ [1654361352, 19], [1654361353, 18], ... ] // width, height, yticks, xticks, ymin, ymax, xmin, xmax const SVG = function(props) { // w // +---------------------+ // | h1 | // | +-----------+ | // | | | | h // | w1 | | w2 | // | +-----------+ | // | h2 | // +---------------------+ // let w = props.width, h = props.height, w1 = 30, w2 = 0, h1 = 8, h2 = 18; let yticks = props.yticks || 4, xticks = props.xticks || 5; let data = props.data || []; let ymin = props.ymin || 0; let ymax = props.ymax || Math.max.apply(null, data.map(p => p[1])); let xmin = props.xmin || Math.min.apply(null, data.map(p => p[0])); let xmax = props.xmax || Math.max.apply(null, data.map(p => p[0])); // Y-axis tick lines and labels let yta = (new Array(yticks + 1)).fill(0).map((_, i) => i); // indices let yti = i => h - h2 - (h - h1 - h2) * i / yticks; // index's Y let ytv = i => (ymax - ymin) * i / yticks; let ytl = y => html``; let ytt = (y, v) => html`${v}`; // X-axis tick lines and labels let datefmt = unix => (new Date(unix * 1000)).toISOString().substr(14, 5); let xta = (new Array(xticks + 1)).fill(0).map((_, i) => i); // indices let xti = i => w1 + (w - w1 - w2) * i / xticks; // index's X let xtv = i => datefmt(xmin + (xmax - xmin) * i / xticks); let xtl = x => html``; let xtt = (x, v) => html`${v}`; // Transform data points array into coordinate let dx = v => w1 + (v - xmin) / ((xmax - xmin) || 1) * (w - w1 - w2); let dy = v => h - h2 - (v - ymin) / ((ymax - ymin) || 1) * (h - h1 - h2); let dd = data.map(p => [Math.round(dx(p[0])), Math.round(dy(p[1]))]); let ddl = dd.length; // And plot the data as element let begin0 = ddl ? `M ${dd[0][0]},${dd[0][1]}` : `M 0,0`; let begin = `M ${w1},${h - h2}`; // Initial point let end = ddl ? `L ${dd[ddl - 1][0]},${h - h2}` : `L ${w1},${h - h2}`; let series = ddl ? dd.map(p => `L ${p[0]} ${p[1]}`) : []; return html` ${yta.map(i => ytl(yti(i)))} ${yta.map(i => ytt(yti(i), ytv(i)))} ${xta.map(i => xtl(xti(i)))} ${data.length ? xta.map(i => xtt(xti(i), xtv(i))) : ''} `; }; const Chart = function(props) { let xmax = 0, missing = MaxMetricsDataPoints - props.metrics.length; if (missing > 0) xmax = Math.round(Date.now() / 1000) + missing; return html`

Data Chart

<${SVG} height=240 width=600 ymin=0 ymax=20 xmax=${xmax} data=${props.metrics} />
This chart plots live sensor data, sent by a device via /api/watch.
`; }; const App = function(props) { const [messages, setMessages] = useState([]); const [metrics, setMetrics] = useState([]); const [user, setUser] = useState(''); const [config, setConfig] = useState({}); const refresh = () => fetch('/api/config/get', {headers: {Authorization: ''}}) .then(r => r.json()) .then(r => setConfig(r)); const login = function(u) { document.cookie = `access_token=${u.token};path=/;max-age=3600`; setUser(u.user); refresh(); }; const logout = ev => { document.cookie = `access_token=;path=/;max-age=0`; setUser(''); }; const watch = function() { var f = function(reader) { return reader.read().then(function(result) { var data = String.fromCharCode.apply(null, result.value); var msg = JSON.parse(data); if (msg.name == 'config') { refresh(); } else if (msg.name == 'message') { setMessages(m => m.concat([msg.data])); } else if (msg.name == 'metrics') { setMetrics(m => m.concat([msg.data]).splice(-MaxMetricsDataPoints)); } // console.log(msg); if (!result.done) return f(reader); }); }; fetch('/api/watch', {headers: {Authorization: ''}}) .then(r => r.body.getReader()) .then(f) .catch(e => setTimeout(watch, 1000)); }; useEffect(() => { fetch('/api/login', {headers: {Authorization: ''}}) .then(r => r.json()) .then(r => login(r)) .catch(err => setUser('')); refresh(); watch(); }, []); if (!user) return html`<${Login} login=${login} />`; const admin = user == 'admin'; const cs = admin ? html`<${Configuration} config=${config} />` : ''; return html` <${Nav} user=${user} logout=${logout} />
<${Hero} />
<${Chart} metrics=${metrics} />
${cs}
<${Messages} messages=${messages} />
<${Footer} />
`; }; window.onload = () => render(h(App), document.body);