From 9b8aeb22f46b74519c118438af3db57e781a2bae Mon Sep 17 00:00:00 2001 From: Kai Chappell Date: Sun, 9 Nov 2025 15:56:06 +0000 Subject: [PATCH] Fix dashboard: connect instruments on startup, remove broken reset, apply controls properly --- src/py_dvt_ate/app/dashboard/app.py | 75 ++++++++++------------------- 1 file changed, 25 insertions(+), 50 deletions(-) diff --git a/src/py_dvt_ate/app/dashboard/app.py b/src/py_dvt_ate/app/dashboard/app.py index 435669a..d8fea32 100644 --- a/src/py_dvt_ate/app/dashboard/app.py +++ b/src/py_dvt_ate/app/dashboard/app.py @@ -131,6 +131,15 @@ def init_session_state() -> None: ) st.session_state.instruments = InstrumentFactory.create(config) + # Connect instruments for dashboard use + try: + st.session_state.instruments.chamber.transport.connect() + st.session_state.instruments.psu.transport.connect() + st.session_state.instruments.dmm.transport.connect() + except Exception as e: + st.error(f"Failed to connect to instruments: {e}") + st.stop() + if "repository" not in st.session_state: # Create test repository (temporary file for dashboard demo) import os @@ -195,31 +204,6 @@ def step_simulation() -> None: pass -def sync_instruments_from_session_state() -> None: - """Sync instrument settings from session state via HAL (called by fragment).""" - instruments: InstrumentSet = st.session_state.instruments - - try: - # Control thermal chamber via HAL - instruments.chamber.set_temperature(st.session_state.get("temp_setpoint", 25.0)) - - # Control power supply via HAL - input_voltage = st.session_state.get("input_voltage", 5.0) - output_enabled = st.session_state.get("output_enabled", False) - instruments.psu.set_voltage(1, input_voltage) - instruments.psu.enable_output(1, output_enabled) - - # Note: Load current would typically be controlled by an electronic load - # instrument. For simulation, we set it directly on the physics engine. - # In production, you'd have a separate IElectronicLoad interface. - server: SimulationServer = st.session_state.server - engine: PhysicsEngine | None = server.physics_engine - if engine is not None: - load_current_a = st.session_state.get("load_current", 100.0) / 1000.0 - engine.set_load_current(load_current_a) - except OSError: - # Ignore communication errors - pass def display_controls() -> None: @@ -241,31 +225,11 @@ def display_controls() -> None: st.session_state.running = True st.session_state.last_update = time.time() - # Reset button - if st.sidebar.button("Reset", width="stretch"): - # Stop server and restart - server: SimulationServer = st.session_state.server - loop = asyncio.new_event_loop() - loop.run_until_complete(server.stop()) - loop.close() - - # Restart server - st.session_state.server, st.session_state.server_thread = start_embedded_server() - - # Reconnect instruments - config = InstrumentConfig( - backend="simulator", - simulator_host="127.0.0.1", - chamber_port=5001, - psu_port=5002, - dmm_port=5003, - ) - st.session_state.instruments = InstrumentFactory.create(config) - - # Reset history + # Clear Charts button (just clears history, doesn't restart server) + if st.sidebar.button("Clear Charts", width="stretch"): st.session_state.history = SimulationHistory() - st.session_state.running = False st.session_state.last_update = time.time() + st.rerun() st.sidebar.divider() @@ -352,8 +316,19 @@ def simulation_display() -> None: instruments: InstrumentSet = st.session_state.instruments history: SimulationHistory = st.session_state.history - # Sync instrument settings from UI controls via HAL - sync_instruments_from_session_state() + # Apply control settings to instruments + try: + temp_setpoint = st.session_state.get("temp_setpoint", 25.0) + input_voltage = st.session_state.get("input_voltage", 5.0) + output_enabled = st.session_state.get("output_enabled", False) + load_current_ma = st.session_state.get("load_current", 100.0) + + instruments.chamber.set_temperature(temp_setpoint) + instruments.psu.set_voltage(1, input_voltage) + instruments.psu.enable_output(1, output_enabled) + engine.set_load_current(load_current_ma / 1000.0) + except Exception: + pass # Ignore errors # Step simulation if running if st.session_state.running: