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 asyncio
|
||||||
import datetime
|
import datetime
|
||||||
from valveBackend import ValveBackend
|
from valveBackend import ValveBackend
|
||||||
|
import csv
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
if platform.system() in ["Darwin"]: # macOS or Windows
|
if platform.system() in ["Darwin"]: # macOS or Windows
|
||||||
from MockCAN import CANBackend
|
from MockCAN import CANBackend
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
from classCAN import CANBackend # Your real backend
|
from classCAN import CANBackend # Your real backend
|
||||||
|
logging.basicConfig(level=logging.ERROR)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.add_middleware(SessionMiddleware, secret_key="your_super_secret_key")
|
app.add_middleware(SessionMiddleware, secret_key="your_super_secret_key")
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
can_backend = CANBackend()
|
can_backend = CANBackend()
|
||||||
valve_backend = ValveBackend(eds_file="/home/hmi/Desktop/HMI/eds_file/inletvalveboard.eds")
|
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
|
# Global object to store the latest data
|
||||||
latest_data: Dict[str, Any] = {"PU_1": None, "PU_2": None, "PU_3": None}
|
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):
|
def format_data(data):
|
||||||
return {
|
return {
|
||||||
|
|
@ -206,7 +220,7 @@ async def update_latest_data():
|
||||||
while True:
|
while True:
|
||||||
for pu in [1, 2, 3]:
|
for pu in [1, 2, 3]:
|
||||||
data = can_backend.get_latest_data(pu_number=pu)
|
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)
|
latest_data[f"PU_{pu}"] = format_data(data)
|
||||||
await asyncio.sleep(0.2) # Update every 100ms
|
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}")
|
logging.info(f"Feed valve opening to {MV01_opening}")
|
||||||
return {"status": "ok"}
|
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)
|
app.include_router(router)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -240,11 +240,16 @@
|
||||||
<a href="/monitor-page?pu_number=3" target="_blank" class="monitor-link">
|
<a href="/monitor-page?pu_number=3" target="_blank" class="monitor-link">
|
||||||
<i class="fas fa-chart-line"></i> Monitor PU 3
|
<i class="fas fa-chart-line"></i> Monitor PU 3
|
||||||
</a>
|
</a>
|
||||||
|
<!-- New Record Button -->
|
||||||
|
<button id="recordButton" class="connect-button" onclick="toggleRecording()">
|
||||||
|
<i class="fas fa-circle"></i> Start Recording
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<button id="connectButton" class="connect-button" onclick="toggleConnection()">
|
<button id="connectButton" class="connect-button" onclick="toggleConnection()">
|
||||||
<i class="fas fa-power-off"></i> Connect
|
<i class="fas fa-power-off"></i> Connect
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="left-panel">
|
<div class="left-panel">
|
||||||
<div class="mode-block">
|
<div class="mode-block">
|
||||||
|
|
@ -330,6 +335,7 @@
|
||||||
function updatePloopSetpoint(value) {
|
function updatePloopSetpoint(value) {
|
||||||
document.getElementById('currentValue').textContent = value;
|
document.getElementById('currentValue').textContent = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function toggleConnection() {
|
async function toggleConnection() {
|
||||||
const response = await fetch('/connect_toggle', {method: 'POST'});
|
const response = await fetch('/connect_toggle', {method: 'POST'});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
@ -337,6 +343,27 @@ async function toggleConnection() {
|
||||||
connectButton.classList.toggle('connected', data.connected);
|
connectButton.classList.toggle('connected', data.connected);
|
||||||
connectButton.innerHTML = `<i class="fas fa-power-off"></i> ${data.connected ? 'Disconnect' : 'Connect'}`;
|
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 {
|
||||||
|
await fetch('/stop_recording', { method: 'POST' });
|
||||||
|
button.innerHTML = '<i class="fas fa-circle"></i> Start Recording';
|
||||||
|
button.classList.remove('connected');
|
||||||
|
}
|
||||||
|
isRecording = !isRecording;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Recording toggle failed:', error);
|
||||||
|
alert('Failed to toggle recording. Check connection.');
|
||||||
|
}
|
||||||
|
}
|
||||||
async function sendCommand(state, puNumber, buttonEl) {
|
async function sendCommand(state, puNumber, buttonEl) {
|
||||||
const ploopSetpoint = document.getElementById('ploopSetpoint').value;
|
const ploopSetpoint = document.getElementById('ploopSetpoint').value;
|
||||||
await fetch(`/command/${state}/pu/${puNumber}?ploop_setpoint=${ploopSetpoint}`, {method: 'POST'});
|
await fetch(`/command/${state}/pu/${puNumber}?ploop_setpoint=${ploopSetpoint}`, {method: 'POST'});
|
||||||
|
|
@ -398,6 +425,7 @@ async function sendCommand(state, puNumber, buttonEl) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setFeedValve(opening, buttonEl) {
|
async function setFeedValve(opening, buttonEl) {
|
||||||
await fetch(`/command/feed_valve?MV01_opening=${opening}`, {method: 'POST'});
|
await fetch(`/command/feed_valve?MV01_opening=${opening}`, {method: 'POST'});
|
||||||
document.querySelectorAll('.feed-valve-buttons button').forEach(btn => {
|
document.querySelectorAll('.feed-valve-buttons button').forEach(btn => {
|
||||||
|
|
@ -405,6 +433,7 @@ async function setFeedValve(opening, buttonEl) {
|
||||||
});
|
});
|
||||||
buttonEl.classList.add('active');
|
buttonEl.classList.add('active');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchPUStatus() {
|
async function fetchPUStatus() {
|
||||||
const response = await fetch("/api/pu_status");
|
const response = await fetch("/api/pu_status");
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
@ -412,8 +441,10 @@ async function fetchPUStatus() {
|
||||||
document.getElementById("pu2-status").textContent = data.PU2 || "Unknown";
|
document.getElementById("pu2-status").textContent = data.PU2 || "Unknown";
|
||||||
document.getElementById("pu3-status").textContent = data.PU3 || "Unknown";
|
document.getElementById("pu3-status").textContent = data.PU3 || "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchPUStatus();
|
fetchPUStatus();
|
||||||
setInterval(fetchPUStatus, 5000);
|
setInterval(fetchPUStatus, 5000);
|
||||||
|
|
||||||
async function updateMonitorData() {
|
async function updateMonitorData() {
|
||||||
const response = await fetch('/monitor');
|
const response = await fetch('/monitor');
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
@ -439,6 +470,7 @@ async function updateMonitorData() {
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateMonitorValues(id, values, unit) {
|
function updateMonitorValues(id, values, unit) {
|
||||||
const container = document.getElementById(id);
|
const container = document.getElementById(id);
|
||||||
const valueElements = container.querySelectorAll('.monitor-value');
|
const valueElements = container.querySelectorAll('.monitor-value');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user