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:
@@ -50,17 +50,50 @@ class Varid(enum.Enum):
|
||||
|
||||
class Pololu1376:
|
||||
def __init__(self, serial_port):
|
||||
# Deferred-open pyserial idiom: serial_port is constructed with no
|
||||
# port set (see components/actor/stirrerpololu1376.py), so it isn't
|
||||
# actually opened until connect() is called - a missing/unplugged
|
||||
# device then doesn't crash the whole server at construction time.
|
||||
self.ser = serial_port
|
||||
self.ser.set_output_flow_control(False)
|
||||
self.firmware_version = None
|
||||
|
||||
def is_connected(self):
|
||||
return self.ser.is_open
|
||||
|
||||
def connect(self):
|
||||
if self.ser.is_open:
|
||||
return True
|
||||
try:
|
||||
self.ser.open()
|
||||
except serial.SerialException:
|
||||
try:
|
||||
self.ser.close()
|
||||
self.ser.open()
|
||||
except serial.SerialException as e:
|
||||
print(f"Pololu1376: connect failed: {e}")
|
||||
return False
|
||||
# set_output_flow_control() requires the port to already be open -
|
||||
# can only happen here, after open() above, not at construction time.
|
||||
self.ser.set_output_flow_control(False)
|
||||
try:
|
||||
self.stop()
|
||||
self.firmware_version = self.get_firmware_version()
|
||||
print("Pololu1376 F/W-Version:", self.firmware_version)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Pololu1376: connect failed: {e}")
|
||||
self.ser.close()
|
||||
self.ser.open()
|
||||
self.firmware_version = None
|
||||
return False
|
||||
|
||||
self.stop()
|
||||
|
||||
print("Pololu1376 F/W-Version:", self.get_firmware_version())
|
||||
def disconnect(self):
|
||||
if self.ser.is_open:
|
||||
try:
|
||||
self.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self.ser.close()
|
||||
self.firmware_version = None
|
||||
|
||||
def send(self, data):
|
||||
self.ser.write((data + '\r\n').encode())
|
||||
@@ -126,9 +159,12 @@ class Pololu1376:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ser = serial.Serial("/dev/ttyACM0", 115200)
|
||||
ser = serial.Serial()
|
||||
ser.port = "/dev/ttyACM0"
|
||||
ser.baudrate = 115200
|
||||
|
||||
drv = Pololu1376(serial_port=ser)
|
||||
drv.connect()
|
||||
drv.go()
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user