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
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.
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`
`;
};
const Chart = function(props) {
let xmax = 0, missing = MaxMetricsDataPoints - props.metrics.length;
if (missing > 0) xmax = Math.round(Date.now() / 1000) + missing;
return html`