NorthStar-HMI/static/monitor.html
2025-07-15 14:11:14 +02:00

255 lines
9.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Monitoring Dashboard</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.plot-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
}
.large-plot {
width: 45%;
height: 300px;
}
.small-plot {
width: 22%;
height: 200px;
}
h1 {
text-align: center;
}
#puSelector {
display: block;
margin: 10px auto 20px auto;
font-size: 16px;
padding: 5px 10px;
}
#recordButton {
background-color: #ff4444;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
margin: 10px;
}
</style>
</head>
<body>
<h1>Live Monitoring Dashboard</h1>
<label for="puSelector" style="text-align:center; display:block;">Select PU:</label>
<select id="puSelector">
<option value="1">PU 1</option>
<option value="2">PU 2</option>
<option value="3">PU 3</option>
</select>
<button id="recordButton" onclick="toggleRecording()">Record</button>
<div class="plot-container">
<div id="flow-plot" class="large-plot"></div>
<div id="pressure-plot" class="large-plot"></div>
<div id="mv02-plot" class="small-plot"></div>
<div id="mv03-plot" class="small-plot"></div>
<div id="mv04-05-plot" class="small-plot"></div>
<div id="mv06-plot" class="small-plot"></div>
<div id="mv07-plot" class="small-plot"></div>
<div id="mv08-plot" class="small-plot"></div>
</div>
<script>
let isRecording = false;
let recordedData = [];
let recordingInterval;
let csvFileName = '';
async function toggleRecording() {
const recordButton = document.getElementById('recordButton');
if (!isRecording) {
isRecording = true;
recordButton.style.backgroundColor = '#ff0000';
recordButton.textContent = 'Stop Recording';
recordedData = [];
csvFileName = `monitoring_data_${new Date().toISOString().replace(/[:.]/g, '-')}.csv`;
startRecording();
} else {
isRecording = false;
recordButton.style.backgroundColor = '#ff4444';
recordButton.textContent = 'Record';
stopRecording();
}
}
function startRecording() {
recordingInterval = setInterval(async () => {
const response = await fetch(`/monitor?pu_number=${document.getElementById('puSelector').value}`);
if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
return;
}
const pu = await response.json();
recordedData.push({
timestamp: new Date().toISOString(),
Qperm: pu.Qperm,
Qdilute: pu.Qdilute,
Qdrain: pu.Qdrain,
Qrecirc: pu.Qrecirc,
Pro: pu.Pro,
Pdilute: pu.Pdilute,
Prentate: pu.Prentate,
MV02: pu.MV02,
MV03: pu.MV03,
MV04: pu.MV04,
MV05: pu.MV05,
MV06: pu.MV06,
MV07: pu.MV07,
MV08: pu.MV08
});
}, 100);
}
async function stopRecording() {
clearInterval(recordingInterval);
if (recordedData.length > 0) {
const csvContent = "data:text/csv;charset=utf-8," +
"Timestamp,Qperm,Qdilute,Qdrain,Qrecirc,Pro,Pdilute,Prentate,MV02,MV03,MV04,MV05,MV06,MV07,MV08\n" +
recordedData.map(row =>
`${row.timestamp},${row.Qperm},${row.Qdilute},${row.Qdrain},${row.Qrecirc},${row.Pro},${row.Pdilute},${row.Prentate},${row.MV02},${row.MV03},${row.MV04},${row.MV05},${row.MV06},${row.MV07},${row.MV08}`
).join("\n");
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", csvFileName);
document.body.appendChild(link);
link.click();
}
}
window.onbeforeunload = function() {
if (isRecording) {
stopRecording();
}
};
// Existing plot initialization and update functions
const maxPoints = 100;
const time = () => new Date();
let selectedPU = 1;
document.getElementById("puSelector").addEventListener("change", function () {
selectedPU = parseInt(this.value);
});
function getLastMinuteRange() {
const now = new Date();
const oneMinuteAgo = new Date(now.getTime() - 60 * 1000);
return [oneMinuteAgo, now];
}
async function updatePlots() {
try {
const response = await fetch(`/monitor?pu_number=${selectedPU}`);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const pu = await response.json();
const t = time();
Plotly.extendTraces('flow-plot', {
x: [[t], [t], [t], [t]],
y: [[pu.Qperm], [pu.Qdilute], [pu.Qdrain], [pu.Qrecirc]]
}, [0, 1, 2, 3], maxPoints);
Plotly.extendTraces('pressure-plot', {
x: [[t], [t], [t]],
y: [[pu.Pro], [pu.Pdilute], [pu.Prentate]]
}, [0, 1, 2], maxPoints);
//
// Plotly.extendTraces('mv02-plot', { x: [[t]], y: [[pu.MV02]] }, [0]);
// Plotly.extendTraces('mv03-plot', { x: [[t]], y: [[pu.MV03]] }, [0]);
// Plotly.extendTraces('mv04-05-plot', {
// x: [[t], [t]],
// y: [[pu.MV04], [pu.MV05]]
// }, [0, 1]);
// Plotly.extendTraces('mv06-plot', { x: [[t]], y: [[pu.MV06]] }, [0]);
// Plotly.extendTraces('mv07-plot', { x: [[t]], y: [[pu.MV07]] }, [0]);
// Plotly.extendTraces('mv08-plot', { x: [[t]], y: [[pu.MV08]] }, [0]);
const range = getLastMinuteRange();
const plotIds = [
'flow-plot', 'pressure-plot', 'mv02-plot', 'mv03-plot',
'mv04-05-plot', 'mv06-plot', 'mv07-plot', 'mv08-plot'
];
plotIds.forEach(id => {
Plotly.relayout(id, { 'xaxis.range': range });
});
} catch (error) {
console.error("Error updating plots:", error);
}
}
function initPlots() {
const time0 = [time()];
Plotly.newPlot('flow-plot', [
{ x: time0, y: [0], name: 'Qperm', mode: 'lines', line: { color: 'blue' } },
{ x: time0, y: [0], name: 'Qdilute', mode: 'lines', line: { color: 'green' } },
{ x: time0, y: [0], name: 'Qdrain', mode: 'lines', line: { color: 'red' } },
{ x: time0, y: [0], name: 'Qrecirc', mode: 'lines', line: { color: 'orange' } }
], {
title: 'Flow Rates Over Time',
xaxis: { title: 'Time', type: 'date' },
yaxis: { title: 'Flow (L/h)', range: [0, 2000] }
});
Plotly.newPlot('pressure-plot', [
{ x: time0, y: [0], name: 'Pro', mode: 'lines', line: { color: 'purple' } },
{ x: time0, y: [0], name: 'Pdilute', mode: 'lines', line: { color: 'teal' } },
{ x: time0, y: [0], name: 'Prentate', mode: 'lines', line: { color: 'gray' } }
], {
title: 'Pressure Over Time',
xaxis: { title: 'Time', type: 'date' },
yaxis: { title: 'Pressure (bar)', range: [0, 15] }
});
// Plotly.newPlot('mv02-plot', [{
// x: time0, y: [0], name: 'MV02', mode: 'lines'
// }], {
// title: 'MV02 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
// Plotly.newPlot('mv03-plot', [{
// x: time0, y: [0], name: 'MV03', mode: 'lines'
// }], {
// title: 'MV03 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
// Plotly.newPlot('mv04-05-plot', [
// { x: time0, y: [0], name: 'MV04', mode: 'lines' },
// { x: time0, y: [0], name: 'MV05', mode: 'lines' }
// ], {
// title: 'MV04 + MV05 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
// Plotly.newPlot('mv06-plot', [{
// x: time0, y: [0], name: 'MV06', mode: 'lines'
// }], {
// title: 'MV06 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
// Plotly.newPlot('mv07-plot', [{
// x: time0, y: [0], name: 'MV07', mode: 'lines'
// }], {
// title: 'MV07 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
// Plotly.newPlot('mv08-plot', [{
// x: time0, y: [0], name: 'MV08', mode: 'lines'
// }], {
// title: 'MV08 (%)', yaxis: { range: [0, 100] }, xaxis: { type: 'date' }
// });
setInterval(updatePlots, 1000);
}
window.onload = initPlots;
</script>
</body>
</html>