feat: add connect/disconnect status for real heater/stirrer hardware
Heater and Stirrer can be real serial hardware (hendi, Pololu1376), but there was no connection concept at all - the constructors opened the port and crashed the whole server if the device was missing, with no way to see connection status or firmware version and no way to reconnect without a restart. Adds an observable Connectable mixin (components/connectable.py) shared by AHeater/AStirrer; real devices defer opening the serial port to an explicit connect(), auto-connect on server startup, and surface Connected/FirmwareVersion/Simulated plus manual Connect/Disconnect over the web GUI. Heating/stirring and Sud Start are all gated on connection state, and a disconnect mid-brew force-stops the run via the same path as a manual Stop. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YaPLuRPpyjWcwhMvCvpHCL
This commit is contained in:
@@ -10,12 +10,27 @@ class StirrerPololu1376(AStirrer):
|
||||
return "Pololu1376"
|
||||
|
||||
def __init__(self, dt, params):
|
||||
AStirrer.__init__(self, dt)
|
||||
AStirrer.__init__(self, dt, simulated=False)
|
||||
|
||||
ser = serial.Serial(params['port'], params['speed'])
|
||||
ser.set_output_flow_control(False)
|
||||
# Deferred-open pyserial idiom - see Pololu1376's own constructor
|
||||
# comment: connect() is what actually opens params['port'] (and
|
||||
# sets output flow control, which itself requires an open port).
|
||||
ser = serial.Serial()
|
||||
ser.port = params['port']
|
||||
ser.baudrate = params['speed']
|
||||
self.drv = Pololu1376(ser)
|
||||
|
||||
def connect(self):
|
||||
ok = self.drv.connect()
|
||||
self.connected = ok
|
||||
self.firmware_version = self.drv.firmware_version if ok else None
|
||||
return ok
|
||||
|
||||
def disconnect(self):
|
||||
self.drv.disconnect()
|
||||
self.connected = False
|
||||
self.firmware_version = None
|
||||
|
||||
@contextmanager
|
||||
def remote_open(self):
|
||||
try:
|
||||
@@ -30,6 +45,8 @@ class StirrerPololu1376(AStirrer):
|
||||
return 0
|
||||
|
||||
def activate(self, enable):
|
||||
if not self.connected:
|
||||
return
|
||||
print("activate {}".format(enable))
|
||||
if enable:
|
||||
self.drv.go()
|
||||
@@ -37,10 +54,14 @@ class StirrerPololu1376(AStirrer):
|
||||
self.drv.stop()
|
||||
|
||||
def _on_set_speed(self, speed):
|
||||
if not self.connected:
|
||||
return
|
||||
self.drv.motor_forward(speed)
|
||||
print("Set speed to {} %".format(speed))
|
||||
|
||||
def _on_process(self):
|
||||
if not self.connected:
|
||||
return
|
||||
status = self.drv.get_variable(Varid.STATUS)
|
||||
limit_status = self.drv.get_variable(Varid.STATUS_LIMIT_STATUS)
|
||||
if status & 0x01 == 0x01:
|
||||
@@ -51,6 +72,7 @@ class StirrerPololu1376(AStirrer):
|
||||
|
||||
if __name__ == '__main__':
|
||||
s = StirrerPololu1376(dt=1.0, params={'port': "/dev/ttyACM0", 'speed': 115200})
|
||||
s.connect()
|
||||
|
||||
with s.remote_open():
|
||||
|
||||
|
||||
Reference in New Issue
Block a user