From 12bb4a43dbc5d293679e51d65b45b9005e5f3250 Mon Sep 17 00:00:00 2001 From: "C.A.M. Gerlach" Date: Sun, 10 May 2020 18:15:15 -0500 Subject: [PATCH] Add additional debug logging information for data inputs --- src/brokkr/inputs/adafruitadc.py | 2 +- src/brokkr/inputs/adafruiti2c.py | 6 +++--- src/brokkr/inputs/smbusi2c.py | 9 ++++++++- src/brokkr/outputs/print.py | 2 +- src/brokkr/pipeline/baseinput.py | 19 +++++++++++++++---- src/brokkr/pipeline/decode.py | 24 +++++++++++------------- src/brokkr/utils/output.py | 2 +- 7 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/brokkr/inputs/adafruitadc.py b/src/brokkr/inputs/adafruitadc.py index 7033340..4203228 100644 --- a/src/brokkr/inputs/adafruitadc.py +++ b/src/brokkr/inputs/adafruitadc.py @@ -51,7 +51,7 @@ def __init__( def read_sensor_data(self, sensor_object=None): with busio.I2C(board.SCL, board.SDA, **self._i2c_kwargs) as i2c: sensor_object = self.init_sensor_object(i2c) - if not sensor_object: + if sensor_object is None: return None channel_object = self._analog_class( sensor_object, **self._adc_kwargs) diff --git a/src/brokkr/inputs/adafruiti2c.py b/src/brokkr/inputs/adafruiti2c.py index c7d38d3..05f6242 100644 --- a/src/brokkr/inputs/adafruiti2c.py +++ b/src/brokkr/inputs/adafruiti2c.py @@ -23,10 +23,10 @@ class AdafruitI2CInput(BaseAdafruitI2CInput): def read_sensor_data(self, sensor_object=None): with busio.I2C(board.SCL, board.SDA, **self._i2c_kwargs) as i2c: sensor_object = self.init_sensor_object(i2c) - if not sensor_object: + if sensor_object is None: return None - raw_data = super().read_sensor_data(sensor_object=sensor_object) - return raw_data + sensor_data = super().read_sensor_data(sensor_object=sensor_object) + return sensor_data class AdafruitPersistantI2CInput(BaseAdafruitI2CInput): diff --git a/src/brokkr/inputs/smbusi2c.py b/src/brokkr/inputs/smbusi2c.py index f0db663..e462386 100644 --- a/src/brokkr/inputs/smbusi2c.py +++ b/src/brokkr/inputs/smbusi2c.py @@ -3,6 +3,7 @@ """ # Standard library imports +import logging from pathlib import Path # Third party imports @@ -18,6 +19,8 @@ I2C_BLOCK_READ_FUNCTION = "read_i2c_block_data" DEFAULT_READ_FUNCTION = I2C_BLOCK_READ_FUNCTION +LOGGER = logging.getLogger(__name__) + class SMBusI2CDevice(brokkr.utils.misc.AutoReprMixin): def __init__(self, bus=None, force=None): @@ -28,6 +31,7 @@ def __init__(self, bus=None, force=None): for n_bus in range(0, MAX_I2C_BUS_N + 1): if Path(f"/dev/i2c-{n_bus}").exists(): bus = n_bus + LOGGER.debug("Found I2C device at bus %s", bus) break else: raise RuntimeError("Could not find I2C any bus device") @@ -38,9 +42,12 @@ def read(self, force=None, read_function=DEFAULT_READ_FUNCTION, **read_kwargs): if force is None: force = self.force + LOGGER.debug("Reading I2C data with function %s at bus %r, kwargs %r", + read_function, self.bus, read_kwargs) with smbus2.SMBus(self.bus, force=self.force) as i2c_bus: buffer = getattr(i2c_bus, read_function)( force=force, **read_kwargs) + LOGGER.debug("Read I2C data %r", buffer) return buffer @@ -77,7 +84,7 @@ def read_sensor_data(self, sensor_object=None): "%s reading data from I2C SMBus device with function %s " "of %s sensor object %s on step %s: %s", type(e).__name__, self._read_function, - type(self), type(self.object_class), self.name, e) + type(self), self.object_class, self.name, e) self.logger.info("Error details:", exc_info=True) sensor_data = None diff --git a/src/brokkr/outputs/print.py b/src/brokkr/outputs/print.py index f66e8e1..b5a0d4d 100644 --- a/src/brokkr/outputs/print.py +++ b/src/brokkr/outputs/print.py @@ -41,7 +41,7 @@ def __init__(self, in_place=True, **print_output_kwargs): super().__init__(in_place=in_place, **print_output_kwargs) def execute(self, input_data=None): - output_data = brokkr.utils.output.format_data(input_data) + output_data = brokkr.utils.output.format_data(input_data) + "\n" if self.in_place and self.ran_once: output_data = ( (CURSOR_UP_CHAR + ERASE_LINE_CHAR) * output_data.count("\n") diff --git a/src/brokkr/pipeline/baseinput.py b/src/brokkr/pipeline/baseinput.py index 4ade76b..7eedb99 100644 --- a/src/brokkr/pipeline/baseinput.py +++ b/src/brokkr/pipeline/baseinput.py @@ -35,7 +35,7 @@ def __init__( except AttributeError: # If data_type isn't already an object try: data_type["name"] - except TypeError: + except TypeError: # If data_types is a list, not a dict data_type_dict = data_types[data_type] data_type_dict["name"] = data_type data_type = data_type_dict @@ -88,7 +88,7 @@ def __init__( self.sensor_object = None self.cache_sensor_object = cache_sensor_object - if sensor_module: + if sensor_module is not None: module_object = importlib.import_module(sensor_module) sensor_class = getattr(module_object, sensor_class) self.object_class = sensor_class @@ -99,21 +99,28 @@ def init_sensor_object(self, *sensor_args, **sensor_kwargs): if not sensor_kwargs: sensor_kwargs = self.sensor_kwargs + self.logger.debug( + "Initializing sensor object %s with args %r, kwargs %s", + self.object_class, sensor_args, sensor_kwargs) try: sensor_object = self.object_class( *sensor_args, **sensor_kwargs) except Exception as e: self.logger.error( "%s initializing %s sensor object %s on step %s: %s", - type(e).__name__, type(self), type(self.object_class), + type(e).__name__, type(self), self.object_class, self.name, e) self.logger.info("Error details:", exc_info=True) self.logger.info("Sensor args: %r | Sensor kwargs: %r:", sensor_args, sensor_kwargs) sensor_object = None + self.logger.debug( + "Initialized sensor object %s to %r", + self.object_class, sensor_object) if self.cache_sensor_object: self.sensor_object = sensor_object + self.logger.debug("Cached sensor object %s", self.object_class) return sensor_object def get_sensor_object(self, sensor_object=None): @@ -154,9 +161,13 @@ def read_sensor_data(self, sensor_object=None): "%s on attribute %s from %s sensor object %s " "on step %s: %s", type(e).__name__, data_type.attribute_name, type(self), - type(self.object_class), self.name, e) + self.object_class, self.name, e) self.logger.info("Error details:", exc_info=True) data_value = None + else: + self.logger.debug( + "Read value %r from attribute %s of sensor %s", + data_value, data_type.attribute_name, self.object_class) sensor_data.append(data_value) return sensor_data diff --git a/src/brokkr/pipeline/decode.py b/src/brokkr/pipeline/decode.py index 672ff38..5103b77 100644 --- a/src/brokkr/pipeline/decode.py +++ b/src/brokkr/pipeline/decode.py @@ -16,6 +16,7 @@ # Local imports import brokkr.pipeline.datavalue import brokkr.utils.misc +import brokkr.utils.output NA_MARKER_DEFAULT = "NA" @@ -194,12 +195,6 @@ def output_na_values(self): return output_data def convert_data(self, raw_data): - # pylint: disable=too-many-branches - if not raw_data: - output_data = self.output_na_values() - LOGGER.debug("No data to convert, returning: %r", output_data) - return output_data - error_count = 0 output_data = {} @@ -210,9 +205,10 @@ def convert_data(self, raw_data): if not self.include_all_data_each: value = value[idx] if value is None: - LOGGER.debug("Data value is None decoding data_type %r to %s, " - "coercing to NA", - data_type.name, data_type.conversion) + LOGGER.debug("Data value is None decoding data_type %s to %s, " + "coercing to NA value %r", + data_type.name, data_type.conversion, + self.output_na_value(data_type)) output_data[data_type.name] = self.output_na_value(data_type) continue try: @@ -259,15 +255,17 @@ def convert_data(self, raw_data): LOGGER.warning("%s additional decode errors were suppressed.", error_count - 1) - LOGGER.debug("Converted data: {%s}", - ", ".join([f"{key!r}: {value!s}" - for key, value in output_data.items()])) + LOGGER.debug("Converted data: {%s}", brokkr.utils.output.format_data( + data=output_data, seperator=", ", include_raw=True)) return output_data def decode_data(self, data): if data is None: - LOGGER.debug("No data to decode") output_data = self.output_na_values() + LOGGER.debug( + "No data to decode, returning NAs: %r", + brokkr.utils.output.format_data( + data=output_data, seperator=", ", include_raw=False)) else: output_data = self.convert_data(data) return output_data diff --git a/src/brokkr/utils/output.py b/src/brokkr/utils/output.py index 548db51..85ce6a1 100644 --- a/src/brokkr/utils/output.py +++ b/src/brokkr/utils/output.py @@ -49,7 +49,7 @@ def format_data(data=None, seperator="\n", include_raw=False): data_item = " ".join(data_componets) output_data_list.append(data_item) - formatted_data = seperator.join(output_data_list) + "\n" + formatted_data = seperator.join(output_data_list) return formatted_data