handle serial port unavailable

This commit is contained in:
raf 2023-11-29 03:36:33 +03:00
parent a22882a169
commit 5c7691ba6b
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29

View file

@ -17,7 +17,12 @@ class AirQualityMonitor:
SERIAL_DEVICE = os.environ.get("SERIAL_DEVICE", "/dev/ttyUSB0")
def __init__(self):
self.ser = serial.Serial(self.SERIAL_DEVICE)
# if path does not exist, error and exit
if not os.path.exists(self.SERIAL_DEVICE):
raise RuntimeError(f"Serial device {self.SERIAL_DEVICE} not found.")
else:
print(f"Serial device {self.SERIAL_DEVICE} found.")
self.ser = serial.Serial(self.SERIAL_DEVICE)
def get_measurement(self):
"""Fetches a measurement from the sensor and returns it."""