NorthStar-HMI/static/monitor.html
2025-07-16 09:15:00 +02:00

280 lines
10 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: 30%;
height: 250px;
}
h1 {
text-align: center;
}
#recordButton {
background-color: #ff4444;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
margin: 10px;
}
.status-container {
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
margin: 10px auto;
text-align: center;
font-size: 18px;
}
</style>
</head>
<body>
<h1 id="pageTitle">Live Monitoring Dashboard</h1>
<div class="status-container">
<p>Current Status: <span id="currentStatus">Loading...</span></p>
</div>
<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>
// Extract PU number from URL
const urlParams = new URLSearchParams(window.location.search);
const puNumber = urlParams.get('pu_number') || '1'; // Default to PU 1 if not specified
document.getElementById('pageTitle').textContent = `Live Monitoring Dashboard - PU ${puNumber}`;
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_PU${puNumber}_${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');
if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
return;
}
const allData = await response.json();
const puData = allData[`PU_${puNumber}`];
recordedData.push({
timestamp: new Date().toISOString(),
Qperm: puData.Qperm,
Qdilute: puData.Qdilute,
Qdrain: puData.Qdrain,
Qrecirc: puData.Qrecirc,
Pro: puData.Pro,
Pdilute: puData.Pdilute,
Prentate: puData.Prentate,
MV02: puData.MV02,
MV03: puData.MV03,
MV04: puData.MV04,
MV05: puData.MV05,
MV06: puData.MV06,
MV07: puData.MV07,
MV08: puData.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();
}
};
const maxPoints = 100;
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');
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const allData = await response.json();
const puData = allData[`PU_${puNumber}`];
// Convert timestamp string to Date object
const timestamp = new Date(puData.timestamp);
Plotly.extendTraces('flow-plot', {
x: [[timestamp], [timestamp], [timestamp], [timestamp]],
y: [[puData.Qperm], [puData.Qdilute], [puData.Qdrain], [puData.Qrecirc]]
}, [0, 1, 2, 3], maxPoints);
Plotly.extendTraces('pressure-plot', {
x: [[timestamp], [timestamp], [timestamp]],
y: [[puData.Pro], [puData.Pdilute], [puData.Prentate]]
}, [0, 1, 2], maxPoints);
Plotly.extendTraces('mv02-plot', { x: [[timestamp]], y: [[puData.MV02]] }, [0], maxPoints);
Plotly.extendTraces('mv03-plot', { x: [[timestamp]], y: [[puData.MV03]] }, [0], maxPoints);
Plotly.extendTraces('mv04-05-plot', {
x: [[timestamp], [timestamp]],
y: [[puData.MV04], [puData.MV05]]
}, [0, 1], maxPoints);
Plotly.extendTraces('mv06-plot', { x: [[timestamp]], y: [[puData.MV06]] }, [0], maxPoints);
Plotly.extendTraces('mv07-plot', { x: [[timestamp]], y: [[puData.MV07]] }, [0], maxPoints);
Plotly.extendTraces('mv08-plot', { x: [[timestamp]], y: [[puData.MV08]] }, [0], maxPoints);
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);
}
}
async function fetchPUStatus() {
try {
const response = await fetch("/api/pu_status");
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
const status = data[`PU${puNumber}`] || "Unknown";
document.getElementById("currentStatus").textContent = status;
} catch (error) {
console.error("Error fetching PU status:", error);
document.getElementById("currentStatus").textContent = "Error fetching status";
}
}
function initPlots() {
const time0 = [new Date()]; // Use current time for initialization
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, 500);
}
window.onload = function() {
initPlots();
fetchPUStatus();
setInterval(fetchPUStatus, 5000); // Update status every 5 seconds
};
</script>
</body>
</html>