From e5e18ce738e662ac2c57464495405388ce71b12a Mon Sep 17 00:00:00 2001 From: CamDavidsonPilon Date: Sat, 11 Nov 2023 14:45:10 -0500 Subject: [PATCH] what is actually listened to here? --- .../leader/mqtt_to_db_streaming.py | 3 +- pioreactor/utils/pwm.py | 29 ++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pioreactor/background_jobs/leader/mqtt_to_db_streaming.py b/pioreactor/background_jobs/leader/mqtt_to_db_streaming.py index 2c9dbc8a..2302f3a6 100644 --- a/pioreactor/background_jobs/leader/mqtt_to_db_streaming.py +++ b/pioreactor/background_jobs/leader/mqtt_to_db_streaming.py @@ -114,7 +114,7 @@ def callback(message: pt.MQTTMessage) -> None: try: new_rows = parser(message.topic, message.payload) except Exception as e: - self.logger.error(e) + self.logger.error(f"Encountered error in saving to DB: {e}. See logs.") self.logger.debug( f"Error in {parser.__name__}. message.payload that caused error: `{message.payload.decode()}`", exc_info=True, @@ -145,6 +145,7 @@ def callback(message: pt.MQTTMessage) -> None: def initialize_callbacks(self, topics_and_callbacks: list[TopicToCallback]) -> None: for topic_and_callback in topics_and_callbacks: + print(str(topic_and_callback.topic)) self.subscribe_and_callback( topic_and_callback.callback, str(topic_and_callback.topic), diff --git a/pioreactor/utils/pwm.py b/pioreactor/utils/pwm.py index 7ad056b4..04325a04 100644 --- a/pioreactor/utils/pwm.py +++ b/pioreactor/utils/pwm.py @@ -10,6 +10,7 @@ import lgpio +from pioreactor import types as pt from pioreactor.exc import PWMError from pioreactor.logging import create_logger from pioreactor.logging import Logger @@ -36,7 +37,9 @@ class HardwarePWMOutputDevice(HardwarePWM): HARDWARE_PWM_CHANNELS: dict[GpioPin, int] = {12: 0, 13: 1} - def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100): + def __init__( + self, pin: GpioPin, initial_dc: pt.FloatBetween0and100 = 0.0, frequency: float = 100 + ) -> None: if ( pin not in self.HARDWARE_PWM_CHANNELS ): # Only GPIO pins 18 and 19 are supported for hardware PWM @@ -48,28 +51,28 @@ def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100): super().__init__(pwm_channel, hz=frequency) self._dc = initial_dc - def start(self): + def start(self) -> None: super().start(self.dc) - def off(self): + def off(self) -> None: self.dc = 0.0 @property - def dc(self) -> float: + def dc(self) -> pt.FloatBetween0and100: return self._dc @dc.setter - def dc(self, dc: float) -> None: + def dc(self, dc: pt.FloatBetween0and100) -> None: dc = clamp(0.0, dc, 100.0) self.change_duty_cycle(dc) self._dc = dc - def close(self): + def close(self) -> None: pass class SoftwarePWMOutputDevice: - def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100): + def __init__(self, pin: GpioPin, initial_dc: pt.FloatBetween0and100 = 0.0, frequency=100): self.pin = pin self._dc = initial_dc self.frequency = frequency @@ -91,11 +94,11 @@ def off(self): pass @property - def dc(self) -> float: + def dc(self) -> pt.FloatBetween0and100: return self._dc @dc.setter - def dc(self, dc: float) -> None: + def dc(self, dc: pt.FloatBetween0and100) -> None: dc = clamp(0.0, dc, 100.0) self._dc = dc if self._started: @@ -239,18 +242,18 @@ def _serialize(self): f"pioreactor/{self.unit}/{self.experiment}/pwms/dc", dumps(current_values), retain=True ) - def start(self, initial_duty_cycle: float) -> None: - if not (0.0 <= initial_duty_cycle <= 100.0): + def start(self, duty_cycle: pt.FloatBetween0and100) -> None: + if not (0.0 <= duty_cycle <= 100.0): raise PWMError("duty_cycle should be between 0 and 100, inclusive.") - self.change_duty_cycle(initial_duty_cycle) + self.change_duty_cycle(duty_cycle) self._pwm.start() def stop(self) -> None: self._pwm.off() self.change_duty_cycle(0.0) - def change_duty_cycle(self, duty_cycle: float) -> None: + def change_duty_cycle(self, duty_cycle: pt.FloatBetween0and100) -> None: if not (0.0 <= duty_cycle <= 100.0): raise PWMError("duty_cycle should be between 0 and 100, inclusive.")