From 5d4187dbd666163911601e0076336e42a4045afc Mon Sep 17 00:00:00 2001 From: Vladimir Filimonov Date: Fri, 1 Jan 2021 22:21:03 +0100 Subject: [PATCH] argument to bypass decrypt method (issue #16) --- README.md | 4 +++- co2meter/co2meter.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af96d32..ac80c73 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,9 @@ Read CO2 and temperature values from the device with the timestamp: mon.read_data() -If `pandas` is available, the output will be formated as `pandas.DataFrame` with columns `co2` and `temp` and datetime-index with the timestamp of measurement. Otherwise tuple `(timestamp, co2, temperature)` will be retured. +If `pandas` is available, the output will be formatted as `pandas.DataFrame` with columns `co2` and `temp` and datetime-index with the timestamp of measurement. Otherwise tuple `(timestamp, co2, temperature)` will be returned. + +**Note**: For certain CO2 meter models packages that are sent over USB are not encrypted. In this case `mon.read_data()` could hang without any data returning (see issue #16). If this happens, instantiating CO2monitor object as `mon = co2.CO2monitor(bypass_decrypt=True)` might solve the issue. ### Continuous monitoring diff --git a/co2meter/co2meter.py b/co2meter/co2meter.py index b213fa9..9b53b2d 100644 --- a/co2meter/co2meter.py +++ b/co2meter/co2meter.py @@ -68,9 +68,20 @@ def convert_temperature(val): # Class to operate with CO2 monitor ############################################################################# class CO2monitor: - def __init__(self): + def __init__(self, bypass_decrypt=False): """ Initialize the CO2monitor object and retrieve basic HID info. + + Args: + bypass_decrypt (bool): For certain CO2 meter models packages that + are sent over USB are not encrypted. In this case instance + of CO2monitor will return no data in .read_data(). + If this happens, setting bypass_decrypt to True might + solve the issue. + + See also: + https://github.com/vfilimonov/co2meter/issues/16 """ + self.bypass_decrypt = bypass_decrypt self._info = {'vendor_id': _CO2MON_HID_VENDOR_ID, 'product_id': _CO2MON_HID_PRODUCT_ID} self._h = hid.device() @@ -166,6 +177,8 @@ def is_alive(self): def _decrypt(self, message): """ Decode message received from CO2 monitor. """ + if self.bypass_decrypt: + return message # Rearrange message and convert to long int msg = list_to_longint([message[i] for i in [2, 4, 0, 7, 1, 6, 5, 3]]) # XOR with magic_table