Full dashboard - make LEDs and graph overridable

This commit is contained in:
cpq committed 2026-12-02 11:24:54 +00:00
1 parent 364c979a1d
commit 37db92bfc2
7 files changed
+294 -69

No files matched your search

+49 -19
View File
@@ -11,10 +11,10 @@
auth: true,
data: {
graph1: {data: []},
graph2: {data: [], report: true},
graph: {x: 0, y: 0, z: 0},
actions: { action1: false, action2: false },
leds: { led1: true, led2: false, led3: false },
settings: { volume: 12, log_level: 3, name: 'Galway', enable_login: true, ota_version: '1.2.3', ota_status: 'No checks yet', ota_url: 'http://my-product.com/ota.json', ota_interval: 30 },
settings: { volume: 12, log_level: 3, name: 'Galway', enable_login: true, ota_version: '1.2.3', ota_status: 'No checks yet', ota_url: 'http://my-product.com/ota.json', ota_interval: 30, graph_report: true },
metrics: { temperature: 1.2, cpu: 4, ram: 5, version: '1.2.3' },
},
});
@@ -47,7 +47,7 @@
requestAnimationFrame(() => {
mkGraph(graphs.graph1);
mkGraph(graphs.graph2);
mkGraph(graphs.graph);
});
};
@@ -85,7 +85,7 @@
document.getElementById('auth').style.display = yes ? '' : 'none';
document.getElementById('app').style.display = yes ? 'none' : '';
if (!yes) requestAnimationFrame(() => { // App is visible now: size graphs
mkGraph(graphs.graph1), mkGraph(graphs.graph2);
mkGraph(graphs.graph1), mkGraph(graphs.graph);
});
if (yes) {
document.body.style.visibility = 'visible';
@@ -122,6 +122,8 @@
Dashboard.login().catch(err => showLogin(true));
// GRAPHS
const GRAPH_WINDOW = 10; // Real-time graph span, seconds
const GRAPH_GAP = 1; // No values for this long, seconds: break the line
const graphs = {
graph1: {
el: document.getElementById("graph1"),
@@ -139,17 +141,23 @@
],
},
},
graph2: {
el: document.getElementById("graph2"),
graph: {
el: document.getElementById("graph"),
plot: null,
opts: {
width: 10, height: 10,
legend: { show: false },
axes: [ { size: 25, gap: 2, }, { size: 40, gap: 2, }, ],
scales: { x: { time: false }, y: { range: [50, 200] }, },
axes: [
{ size: 25, gap: 2, values: (u, splits) => splits.map(s => `${s}s`) },
{ size: 40, gap: 2, },
],
// x is seconds before now, see drawGraph()
scales: { x: { time: false, range: () => [-GRAPH_WINDOW, 0] }, y: { range: [0, 100] }, },
series: [
{},
{stroke: '#058DC7FF', fill: '#058DC71A'}, // azure
{stroke: '#058DC7FF'}, // azure, x
{stroke: '#ED7E17FF'}, // orange, y
{stroke: '#22C55EFF'}, // green, z
],
}
},
@@ -171,17 +179,36 @@
]);
};
function updateGraph2(data) {
data && graphs.graph2.plot.setData([
data.map((_, i) => i),
data,
]);
// Real-time graph: the device pushes x, y, z one set at a time. Keep
// the ones that arrived in the last GRAPH_WINDOW seconds, stamped
// with their arrival time, as [time, x, y, z]
const graphPoints = [];
function addGraphPoint(v) {
const now = Date.now() / 1000, last = graphPoints[graphPoints.length - 1];
// Values stopped for a while: break the line instead of joining it
if (last && now - last[0] > GRAPH_GAP) graphPoints.push([last[0] + 0.001, null, null, null]);
graphPoints.push([now, v.x, v.y, v.z]);
while (graphPoints.length && graphPoints[0][0] < now - GRAPH_WINDOW) graphPoints.shift();
};
// Redraws the real-time graph every frame, so it keeps scrolling
// even when no values arrive
function drawGraph() {
const plot = graphs.graph.plot, now = Date.now() / 1000;
if (plot) plot.setData([
graphPoints.map(p => p[0] - now),
graphPoints.map(p => p[1]),
graphPoints.map(p => p[2]),
graphPoints.map(p => p[3]),
]);
requestAnimationFrame(drawGraph);
};
requestAnimationFrame(drawGraph);
// Intercept change notifications: re-plot graphs
Dashboard.on('change', args => {
args?.graph1?.data && updateGraph1(args?.graph1?.data);
args?.graph2?.data && updateGraph2(args?.graph2?.data);
args?.graph && addGraphPoint(args.graph);
});
// File manager: served directly by the built-in mg_dash file manager
@@ -325,12 +352,13 @@ a, a:visited, a:link { text-decoration: none; }
.field-value { min-width: 0; color: #334155; font-size: 0.9rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { color: #94a3b8; font-size: 0.8rem; font-weight: 700; text-align: right; white-space: nowrap; }
.file-actions { display: flex; align-items: center; justify-content: flex-end; gap: 0.45rem; }
#graph1, #graph2 { width: 100%; height: 100%; min-height: 10rem; }
#graph1, #graph { width: 100%; height: 100%; min-height: 10rem; }
.legend { display: flex; align-items: center; gap: 1rem; flex-shrink: 0; }
.legend-item { display: flex; align-items: center; gap: 0.35rem; font-size: 0.75rem; color: #5d7088; font-weight: 600; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.legend-dot-color2 { background: rgba(237, 126, 23, 1); }
.legend-dot-color1 { background: rgba(5, 141, 199, 1); }
.legend-dot-color3 { background: rgba(34, 197, 94, 1); }
.toggle { position: relative; width: 2.75rem; height: 1.6rem; margin: 0; vertical-align: top; border: 1px solid transparent; background: #d1d5db; border-radius: 0.75rem; outline: none; cursor: pointer; -webkit-appearance: none; -moz-appearance: none; appearance: none; }
.toggle::after { content: ""; position: absolute; left: 1px; top: 1px; width: 1.35rem; height: 1.35rem; background: #fff; border-radius: 50%; transition: all 0.3s; }
.toggle:checked { background: #10B981; }
@@ -527,12 +555,14 @@ a, a:visited, a:link { text-decoration: none; }
<span>Real-Time Graph</span>
<div class="legend">
<small>autoupdate:</small>
<input type="checkbox" data-bind="graph2.report" data-autosave="1" class="toggle" />
<div class="legend-item"><span class="legend-dot legend-dot-color1"></span>Motor Voltage</div>
<input type="checkbox" data-bind="settings.graph_report" data-autosave="1" class="toggle" />
<div class="legend-item"><span class="legend-dot legend-dot-color1"></span>x</div>
<div class="legend-item"><span class="legend-dot legend-dot-color2"></span>y</div>
<div class="legend-item"><span class="legend-dot legend-dot-color3"></span>z</div>
</div>
</div>
<div class="panel-body">
<div id="graph2"></div>
<div id="graph"></div>
</div>
</div>