Fix dashboard: connect instruments on startup, remove broken reset, apply controls properly

This commit is contained in:
2025-11-09 15:56:06 +00:00
parent bd0071e88f
commit 5152f85c8e

View File

@@ -131,6 +131,15 @@ def init_session_state() -> None:
) )
st.session_state.instruments = InstrumentFactory.create(config) 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: if "repository" not in st.session_state:
# Create test repository (temporary file for dashboard demo) # Create test repository (temporary file for dashboard demo)
import os import os
@@ -195,31 +204,6 @@ def step_simulation() -> None:
pass 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: def display_controls() -> None:
@@ -241,31 +225,11 @@ def display_controls() -> None:
st.session_state.running = True st.session_state.running = True
st.session_state.last_update = time.time() st.session_state.last_update = time.time()
# Reset button # Clear Charts button (just clears history, doesn't restart server)
if st.sidebar.button("Reset", width="stretch"): if st.sidebar.button("Clear Charts", 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
st.session_state.history = SimulationHistory() st.session_state.history = SimulationHistory()
st.session_state.running = False
st.session_state.last_update = time.time() st.session_state.last_update = time.time()
st.rerun()
st.sidebar.divider() st.sidebar.divider()
@@ -352,8 +316,19 @@ def simulation_display() -> None:
instruments: InstrumentSet = st.session_state.instruments instruments: InstrumentSet = st.session_state.instruments
history: SimulationHistory = st.session_state.history history: SimulationHistory = st.session_state.history
# Sync instrument settings from UI controls via HAL # Apply control settings to instruments
sync_instruments_from_session_state() 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 # Step simulation if running
if st.session_state.running: if st.session_state.running: