Skip to content

Commit

Permalink
Add additional debug logging information for data inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
CAM-Gerlach committed May 11, 2020
1 parent 29b4fc7 commit 12bb4a4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/brokkr/inputs/adafruitadc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/brokkr/inputs/adafruiti2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion src/brokkr/inputs/smbusi2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

# Standard library imports
import logging
from pathlib import Path

# Third party imports
Expand All @@ -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):
Expand All @@ -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")
Expand All @@ -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


Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/brokkr/outputs/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 15 additions & 4 deletions src/brokkr/pipeline/baseinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down
24 changes: 11 additions & 13 deletions src/brokkr/pipeline/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Local imports
import brokkr.pipeline.datavalue
import brokkr.utils.misc
import brokkr.utils.output


NA_MARKER_DEFAULT = "NA"
Expand Down Expand Up @@ -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 = {}

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/brokkr/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 12bb4a4

Please sign in to comment.