Adds a local recorder
This commit is contained in:
parent
201f8b7f23
commit
ff4050e180
83
main.py
83
main.py
|
|
@ -18,17 +18,21 @@ from fastapi import Query
|
|||
import asyncio
|
||||
import datetime
|
||||
from valveBackend import ValveBackend
|
||||
import csv
|
||||
from collections import deque
|
||||
|
||||
if platform.system() in ["Darwin"]: # macOS or Windows
|
||||
from MockCAN import CANBackend
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
else:
|
||||
from classCAN import CANBackend # Your real backend
|
||||
logging.basicConfig(level=logging.ERROR)
|
||||
|
||||
app = FastAPI()
|
||||
app.add_middleware(SessionMiddleware, secret_key="your_super_secret_key")
|
||||
router = APIRouter()
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
can_backend = CANBackend()
|
||||
valve_backend = ValveBackend(eds_file="/home/hmi/Desktop/HMI/eds_file/inletvalveboard.eds")
|
||||
|
||||
|
|
@ -39,6 +43,16 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|||
# Global object to store the latest data
|
||||
latest_data: Dict[str, Any] = {"PU_1": None, "PU_2": None, "PU_3": None}
|
||||
|
||||
# RECORDER
|
||||
recording_flag = False
|
||||
recording_task = None
|
||||
recording_writer = None
|
||||
recording_file = None
|
||||
write_buffer = deque()
|
||||
flush_interval = 1.0 # flush every 1 second
|
||||
last_flush_time = datetime.datetime.now()
|
||||
|
||||
|
||||
|
||||
def format_data(data):
|
||||
return {
|
||||
|
|
@ -206,7 +220,7 @@ async def update_latest_data():
|
|||
while True:
|
||||
for pu in [1, 2, 3]:
|
||||
data = can_backend.get_latest_data(pu_number=pu)
|
||||
logging.info(f"[MONITOR BUFFER] PU{pu}: {data}")
|
||||
logging.info(f"[MONITOR BUFFER] PU{pu}: ") # {data}
|
||||
latest_data[f"PU_{pu}"] = format_data(data)
|
||||
await asyncio.sleep(0.2) # Update every 100ms
|
||||
|
||||
|
|
@ -238,6 +252,71 @@ def feedvalve_control(MV01_opening: int = Query(...)):
|
|||
logging.info(f"Feed valve opening to {MV01_opening}")
|
||||
return {"status": "ok"}
|
||||
|
||||
#LOCAL RECORDER
|
||||
@app.post("/start_recording")
|
||||
async def start_recording():
|
||||
global recording_flag, recording_task, recording_file, recording_writer
|
||||
|
||||
if recording_flag:
|
||||
raise HTTPException(status_code=400, detail="Already recording.")
|
||||
|
||||
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"recording_{now}.csv"
|
||||
os.makedirs("recordings", exist_ok=True)
|
||||
filepath = os.path.join("recordings", filename)
|
||||
|
||||
recording_file = open(filepath, "w", newline="")
|
||||
fieldnames = ["timestamp", "pu"] + list(format_data({}).keys())
|
||||
recording_writer = csv.DictWriter(recording_file, fieldnames=fieldnames)
|
||||
recording_writer.writeheader()
|
||||
|
||||
recording_flag = True
|
||||
recording_task = asyncio.create_task(record_data_loop())
|
||||
logging.info(f"[RECORDING STARTED] File: {filepath}")
|
||||
return {"status": "recording started", "file": filename}
|
||||
|
||||
|
||||
@app.post("/stop_recording")
|
||||
async def stop_recording():
|
||||
global recording_flag, recording_task, recording_file
|
||||
|
||||
if not recording_flag:
|
||||
raise HTTPException(status_code=400, detail="Not recording.")
|
||||
|
||||
recording_flag = False
|
||||
if recording_task:
|
||||
await recording_task
|
||||
recording_task = None
|
||||
|
||||
if recording_file:
|
||||
recording_file.close()
|
||||
recording_file = None
|
||||
|
||||
logging.info("[RECORDING STOPPED]")
|
||||
return {"status": "recording stopped"}
|
||||
|
||||
|
||||
|
||||
async def record_data_loop():
|
||||
global recording_writer, recording_file, write_buffer, last_flush_time
|
||||
|
||||
while recording_flag:
|
||||
timestamp = datetime.datetime.now().isoformat()
|
||||
for pu, data in latest_data.items():
|
||||
if data:
|
||||
row = {
|
||||
"timestamp": timestamp,
|
||||
"pu": pu,
|
||||
**data
|
||||
}
|
||||
recording_writer.writerow(row)
|
||||
|
||||
# Flush every flush_interval seconds
|
||||
if (datetime.datetime.now() - last_flush_time).total_seconds() >= flush_interval:
|
||||
recording_file.flush()
|
||||
last_flush_time = datetime.datetime.now()
|
||||
|
||||
await asyncio.sleep(0.1) # 10 Hz
|
||||
app.include_router(router)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -240,11 +240,16 @@
|
|||
<a href="/monitor-page?pu_number=3" target="_blank" class="monitor-link">
|
||||
<i class="fas fa-chart-line"></i> Monitor PU 3
|
||||
</a>
|
||||
<!-- New Record Button -->
|
||||
<button id="recordButton" class="connect-button" onclick="toggleRecording()">
|
||||
<i class="fas fa-circle"></i> Start Recording
|
||||
</button>
|
||||
</div>
|
||||
<button id="connectButton" class="connect-button" onclick="toggleConnection()">
|
||||
<i class="fas fa-power-off"></i> Connect
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div class="left-panel">
|
||||
<div class="mode-block">
|
||||
|
|
@ -327,100 +332,126 @@
|
|||
</div>
|
||||
</div>
|
||||
<script>
|
||||
function updatePloopSetpoint(value) {
|
||||
document.getElementById('currentValue').textContent = value;
|
||||
}
|
||||
async function toggleConnection() {
|
||||
const response = await fetch('/connect_toggle', { method: 'POST' });
|
||||
const data = await response.json();
|
||||
const connectButton = document.getElementById('connectButton');
|
||||
connectButton.classList.toggle('connected', data.connected);
|
||||
connectButton.innerHTML = `<i class="fas fa-power-off"></i> ${data.connected ? 'Disconnect' : 'Connect'}`;
|
||||
}
|
||||
async function sendCommand(state, puNumber, buttonEl) {
|
||||
const ploopSetpoint = document.getElementById('ploopSetpoint').value;
|
||||
await fetch(`/command/${state}/pu/${puNumber}?ploop_setpoint=${ploopSetpoint}`, { method: 'POST' });
|
||||
document.querySelectorAll('button').forEach(btn => {
|
||||
btn.classList.remove('in-progress', 'ready', 'production');
|
||||
});
|
||||
if (state === 'PRE-PRODUCTION') {
|
||||
buttonEl.classList.add('in-progress');
|
||||
buttonEl.textContent = `Waiting... PU ${puNumber}`;
|
||||
buttonEl.disabled = true;
|
||||
const checkReady = async () => {
|
||||
const res = await fetch(`/api/pu_status`);
|
||||
const states = await res.json();
|
||||
const currentState = states[`PU${puNumber}`];
|
||||
if (currentState === 'SYSTEM_MODE_READY') {
|
||||
buttonEl.classList.remove('in-progress');
|
||||
buttonEl.classList.add('ready');
|
||||
buttonEl.textContent = `START PRODUCTION PU ${puNumber}`;
|
||||
buttonEl.disabled = false;
|
||||
buttonEl.onclick = async () => {
|
||||
await sendCommand("PRODUCTION", puNumber, buttonEl);
|
||||
buttonEl.classList.remove('ready');
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `PRODUCTION ON PU ${puNumber}`;
|
||||
buttonEl.disabled = true;
|
||||
};
|
||||
function updatePloopSetpoint(value) {
|
||||
document.getElementById('currentValue').textContent = value;
|
||||
}
|
||||
|
||||
async function toggleConnection() {
|
||||
const response = await fetch('/connect_toggle', {method: 'POST'});
|
||||
const data = await response.json();
|
||||
const connectButton = document.getElementById('connectButton');
|
||||
connectButton.classList.toggle('connected', data.connected);
|
||||
connectButton.innerHTML = `<i class="fas fa-power-off"></i> ${data.connected ? 'Disconnect' : 'Connect'}`;
|
||||
}
|
||||
|
||||
let isRecording = false;
|
||||
|
||||
async function toggleRecording() {
|
||||
const button = document.getElementById('recordButton');
|
||||
try {
|
||||
if (!isRecording) {
|
||||
await fetch('/start_recording', { method: 'POST' });
|
||||
button.innerHTML = '<i class="fas fa-stop-circle"></i> Stop Recording';
|
||||
button.classList.add('connected'); // Optional: green background
|
||||
} else {
|
||||
setTimeout(checkReady, 1000);
|
||||
await fetch('/stop_recording', { method: 'POST' });
|
||||
button.innerHTML = '<i class="fas fa-circle"></i> Start Recording';
|
||||
button.classList.remove('connected');
|
||||
}
|
||||
};
|
||||
checkReady();
|
||||
} else if (state === 'PRODUCTION') {
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `PRODUCTION ON PU ${puNumber}`;
|
||||
} else if (state === 'IDLE' || state === 'FIRST_START') {
|
||||
buttonEl.classList.remove('in-progress', 'ready', 'production');
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `${state.replace('_', ' ')} PU ${puNumber}`;
|
||||
const preProdBtn = document.querySelector(`button[data-action="PRE-PRODUCTION"][data-pu="${puNumber}"]`);
|
||||
if (preProdBtn) {
|
||||
preProdBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
preProdBtn.innerHTML = `<i class="fas fa-play"></i> PRE-PROD PU ${puNumber}`;
|
||||
preProdBtn.disabled = false;
|
||||
preProdBtn.onclick = () => sendCommand("PRE-PRODUCTION", puNumber, preProdBtn);
|
||||
}
|
||||
const idleBtn = document.querySelector(`button[data-action="IDLE"][data-pu="${puNumber}"]`);
|
||||
if (idleBtn && idleBtn !== buttonEl) {
|
||||
idleBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
idleBtn.innerHTML = `<i class="fas fa-power-off"></i> IDLE PU ${puNumber}`;
|
||||
idleBtn.disabled = false;
|
||||
idleBtn.onclick = () => sendCommand("IDLE", puNumber, idleBtn);
|
||||
}
|
||||
const firstStartBtn = document.querySelector(`button[data-action="FIRST_START"][data-pu="${puNumber}"]`);
|
||||
if (firstStartBtn && firstStartBtn !== buttonEl) {
|
||||
firstStartBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
firstStartBtn.innerHTML = `<i class="fas fa-power-off"></i> FIRST START PU ${puNumber}`;
|
||||
firstStartBtn.disabled = false;
|
||||
firstStartBtn.onclick = () => sendCommand("FIRST_START", puNumber, firstStartBtn);
|
||||
isRecording = !isRecording;
|
||||
} catch (error) {
|
||||
console.error('Recording toggle failed:', error);
|
||||
alert('Failed to toggle recording. Check connection.');
|
||||
}
|
||||
}
|
||||
}
|
||||
async function setFeedValve(opening, buttonEl) {
|
||||
await fetch(`/command/feed_valve?MV01_opening=${opening}`, { method: 'POST' });
|
||||
document.querySelectorAll('.feed-valve-buttons button').forEach(btn => {
|
||||
btn.classList.remove('active');
|
||||
});
|
||||
buttonEl.classList.add('active');
|
||||
}
|
||||
async function fetchPUStatus() {
|
||||
const response = await fetch("/api/pu_status");
|
||||
const data = await response.json();
|
||||
document.getElementById("pu1-status").textContent = data.PU1 || "Unknown";
|
||||
document.getElementById("pu2-status").textContent = data.PU2 || "Unknown";
|
||||
document.getElementById("pu3-status").textContent = data.PU3 || "Unknown";
|
||||
}
|
||||
fetchPUStatus();
|
||||
setInterval(fetchPUStatus, 5000);
|
||||
async function updateMonitorData() {
|
||||
const response = await fetch('/monitor');
|
||||
const data = await response.json();
|
||||
for (const [puId, puData] of Object.entries(data)) {
|
||||
const container = document.getElementById(puId);
|
||||
if (!container) continue;
|
||||
container.innerHTML = `
|
||||
async function sendCommand(state, puNumber, buttonEl) {
|
||||
const ploopSetpoint = document.getElementById('ploopSetpoint').value;
|
||||
await fetch(`/command/${state}/pu/${puNumber}?ploop_setpoint=${ploopSetpoint}`, {method: 'POST'});
|
||||
document.querySelectorAll('button').forEach(btn => {
|
||||
btn.classList.remove('in-progress', 'ready', 'production');
|
||||
});
|
||||
if (state === 'PRE-PRODUCTION') {
|
||||
buttonEl.classList.add('in-progress');
|
||||
buttonEl.textContent = `Waiting... PU ${puNumber}`;
|
||||
buttonEl.disabled = true;
|
||||
const checkReady = async () => {
|
||||
const res = await fetch(`/api/pu_status`);
|
||||
const states = await res.json();
|
||||
const currentState = states[`PU${puNumber}`];
|
||||
if (currentState === 'SYSTEM_MODE_READY') {
|
||||
buttonEl.classList.remove('in-progress');
|
||||
buttonEl.classList.add('ready');
|
||||
buttonEl.textContent = `START PRODUCTION PU ${puNumber}`;
|
||||
buttonEl.disabled = false;
|
||||
buttonEl.onclick = async () => {
|
||||
await sendCommand("PRODUCTION", puNumber, buttonEl);
|
||||
buttonEl.classList.remove('ready');
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `PRODUCTION ON PU ${puNumber}`;
|
||||
buttonEl.disabled = true;
|
||||
};
|
||||
} else {
|
||||
setTimeout(checkReady, 1000);
|
||||
}
|
||||
};
|
||||
checkReady();
|
||||
} else if (state === 'PRODUCTION') {
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `PRODUCTION ON PU ${puNumber}`;
|
||||
} else if (state === 'IDLE' || state === 'FIRST_START') {
|
||||
buttonEl.classList.remove('in-progress', 'ready', 'production');
|
||||
buttonEl.classList.add('production');
|
||||
buttonEl.textContent = `${state.replace('_', ' ')} PU ${puNumber}`;
|
||||
const preProdBtn = document.querySelector(`button[data-action="PRE-PRODUCTION"][data-pu="${puNumber}"]`);
|
||||
if (preProdBtn) {
|
||||
preProdBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
preProdBtn.innerHTML = `<i class="fas fa-play"></i> PRE-PROD PU ${puNumber}`;
|
||||
preProdBtn.disabled = false;
|
||||
preProdBtn.onclick = () => sendCommand("PRE-PRODUCTION", puNumber, preProdBtn);
|
||||
}
|
||||
const idleBtn = document.querySelector(`button[data-action="IDLE"][data-pu="${puNumber}"]`);
|
||||
if (idleBtn && idleBtn !== buttonEl) {
|
||||
idleBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
idleBtn.innerHTML = `<i class="fas fa-power-off"></i> IDLE PU ${puNumber}`;
|
||||
idleBtn.disabled = false;
|
||||
idleBtn.onclick = () => sendCommand("IDLE", puNumber, idleBtn);
|
||||
}
|
||||
const firstStartBtn = document.querySelector(`button[data-action="FIRST_START"][data-pu="${puNumber}"]`);
|
||||
if (firstStartBtn && firstStartBtn !== buttonEl) {
|
||||
firstStartBtn.classList.remove('in-progress', 'ready', 'production');
|
||||
firstStartBtn.innerHTML = `<i class="fas fa-power-off"></i> FIRST START PU ${puNumber}`;
|
||||
firstStartBtn.disabled = false;
|
||||
firstStartBtn.onclick = () => sendCommand("FIRST_START", puNumber, firstStartBtn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function setFeedValve(opening, buttonEl) {
|
||||
await fetch(`/command/feed_valve?MV01_opening=${opening}`, {method: 'POST'});
|
||||
document.querySelectorAll('.feed-valve-buttons button').forEach(btn => {
|
||||
btn.classList.remove('active');
|
||||
});
|
||||
buttonEl.classList.add('active');
|
||||
}
|
||||
|
||||
async function fetchPUStatus() {
|
||||
const response = await fetch("/api/pu_status");
|
||||
const data = await response.json();
|
||||
document.getElementById("pu1-status").textContent = data.PU1 || "Unknown";
|
||||
document.getElementById("pu2-status").textContent = data.PU2 || "Unknown";
|
||||
document.getElementById("pu3-status").textContent = data.PU3 || "Unknown";
|
||||
}
|
||||
|
||||
fetchPUStatus();
|
||||
setInterval(fetchPUStatus, 5000);
|
||||
|
||||
async function updateMonitorData() {
|
||||
const response = await fetch('/monitor');
|
||||
const data = await response.json();
|
||||
for (const [puId, puData] of Object.entries(data)) {
|
||||
const container = document.getElementById(puId);
|
||||
if (!container) continue;
|
||||
container.innerHTML = `
|
||||
<h3>${puId}</h3>
|
||||
<div class="monitor-value">Q_perm<br>${puData.Qperm.toFixed(1)} L/h</div>
|
||||
<div class="monitor-value">Q_dilute<br>${puData.Qdilute.toFixed(1)} L/h</div>
|
||||
|
|
@ -437,33 +468,34 @@ async function updateMonitorData() {
|
|||
<div class="monitor-value">MV07<br>${puData.MV07.toFixed(1)} % (sp: ${puData.MV07_sp.toFixed(1)})</div>
|
||||
<div class="monitor-value">MV08<br>${puData.MV08.toFixed(1)} % (sp: ${puData.MV08_sp.toFixed(1)})</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
function updateMonitorValues(id, values, unit) {
|
||||
const container = document.getElementById(id);
|
||||
const valueElements = container.querySelectorAll('.monitor-value');
|
||||
valueElements.forEach((element, index) => {
|
||||
if (index < values.length) {
|
||||
element.innerHTML = `#${index + 1}<br>${values[index]} ${unit}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateMonitorValues(id, values, unit) {
|
||||
const container = document.getElementById(id);
|
||||
const valueElements = container.querySelectorAll('.monitor-value');
|
||||
valueElements.forEach((element, index) => {
|
||||
if (index < values.length) {
|
||||
element.innerHTML = `#${index + 1}<br>${values[index]} ${unit}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
setInterval(updateMonitorData, 1000);
|
||||
async function fetchMonitorData() {
|
||||
try {
|
||||
const puMap = {
|
||||
"PU_1": 1,
|
||||
"PU_2": 2,
|
||||
"PU_3": 3
|
||||
};
|
||||
const fields = {
|
||||
"Qperm": "L/h",
|
||||
"Pdilute": "bar",
|
||||
"Conductivity": "µS/cm",
|
||||
"Pro": "bar"
|
||||
};
|
||||
const dataResponse = await fetch('/monitor');
|
||||
const allData = await dataResponse.json();
|
||||
try {
|
||||
const puMap = {
|
||||
"PU_1": 1,
|
||||
"PU_2": 2,
|
||||
"PU_3": 3
|
||||
};
|
||||
const fields = {
|
||||
"Qperm": "L/h",
|
||||
"Pdilute": "bar",
|
||||
"Conductivity": "µS/cm",
|
||||
"Pro": "bar"
|
||||
};
|
||||
const dataResponse = await fetch('/monitor');
|
||||
const allData = await dataResponse.json();
|
||||
for (const [fieldId, unit] of Object.entries(fields)) {
|
||||
const container = document.getElementById(fieldId);
|
||||
if (!container) continue;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user