From 04f48744ec8b5b152580345b77e8a7c57d8e6e66 Mon Sep 17 00:00:00 2001 From: maxthebuch <100023200+maxthebuch@users.noreply.github.com> Date: Sat, 12 Aug 2023 14:10:43 +0200 Subject: [PATCH 1/3] Update ads1x15.py Avoids race condition when reading a channel --- mqtt_io/modules/sensor/ads1x15.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mqtt_io/modules/sensor/ads1x15.py b/mqtt_io/modules/sensor/ads1x15.py index 17670a0a..beb68f24 100644 --- a/mqtt_io/modules/sensor/ads1x15.py +++ b/mqtt_io/modules/sensor/ads1x15.py @@ -1,7 +1,10 @@ """ ADS1x15 analog to digital converters """ +import threading + from typing import cast +from threading import Lock from ...types import CerberusSchemaType, ConfigType, SensorValueType from . import GenericSensor @@ -78,15 +81,22 @@ def setup_module(self) -> None: # Create single-ended input for each pin in config self.channels = {pin: AnalogIn(self.ads, pin) for pin in self.config["pins"]} + # initialize mutex lock + self.lock = threading.Lock() + def get_value(self, sens_conf: ConfigType) -> SensorValueType: """ Get the value or voltage from the sensor """ - sens_type = sens_conf["type"] - data = dict( - value=self.channels[sens_conf["pin"]].value, - voltage=self.channels[sens_conf["pin"]].voltage, - ) + # acquire the lock + with self.lock: + + sens_type = sens_conf["type"] + data = dict( + value=self.channels[sens_conf["pin"]].value, + voltage=self.channels[sens_conf["pin"]].voltage, + ) + return cast( float, data[sens_type], From 5d18a1806df1f1ff32ce59bee7094a825996d625 Mon Sep 17 00:00:00 2001 From: maxthebuch <100023200+maxthebuch@users.noreply.github.com> Date: Sat, 12 Aug 2023 14:10:43 +0200 Subject: [PATCH 2/3] Update ads1x15.py Avoids race condition when reading a channel --- mqtt_io/modules/sensor/ads1x15.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mqtt_io/modules/sensor/ads1x15.py b/mqtt_io/modules/sensor/ads1x15.py index 17670a0a..62dc73a3 100644 --- a/mqtt_io/modules/sensor/ads1x15.py +++ b/mqtt_io/modules/sensor/ads1x15.py @@ -1,6 +1,8 @@ """ ADS1x15 analog to digital converters """ +import threading + from typing import cast from ...types import CerberusSchemaType, ConfigType, SensorValueType @@ -78,15 +80,22 @@ def setup_module(self) -> None: # Create single-ended input for each pin in config self.channels = {pin: AnalogIn(self.ads, pin) for pin in self.config["pins"]} + # initialize mutex lock + self.lock = threading.Lock() + def get_value(self, sens_conf: ConfigType) -> SensorValueType: """ Get the value or voltage from the sensor """ - sens_type = sens_conf["type"] - data = dict( - value=self.channels[sens_conf["pin"]].value, - voltage=self.channels[sens_conf["pin"]].voltage, - ) + # acquire the lock + with self.lock: + + sens_type = sens_conf["type"] + data = dict( + value=self.channels[sens_conf["pin"]].value, + voltage=self.channels[sens_conf["pin"]].voltage, + ) + return cast( float, data[sens_type], From cc9d9eea5f818640141e001238c957a3eb69e4b2 Mon Sep 17 00:00:00 2001 From: maxthebuch <100023200+maxthebuch@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:39:08 +0100 Subject: [PATCH 3/3] Update ads1x15.py Formatting with Lint --- mqtt_io/modules/sensor/ads1x15.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mqtt_io/modules/sensor/ads1x15.py b/mqtt_io/modules/sensor/ads1x15.py index beb68f24..d32afbfb 100644 --- a/mqtt_io/modules/sensor/ads1x15.py +++ b/mqtt_io/modules/sensor/ads1x15.py @@ -4,7 +4,6 @@ import threading from typing import cast -from threading import Lock from ...types import CerberusSchemaType, ConfigType, SensorValueType from . import GenericSensor @@ -90,13 +89,12 @@ def get_value(self, sens_conf: ConfigType) -> SensorValueType: """ # acquire the lock with self.lock: - sens_type = sens_conf["type"] data = dict( value=self.channels[sens_conf["pin"]].value, voltage=self.channels[sens_conf["pin"]].voltage, ) - + return cast( float, data[sens_type],