Skip to content

Commit

Permalink
argument to bypass decrypt method (issue #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfilimonov committed Jan 1, 2021
1 parent 7ca9419 commit 5d4187d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion co2meter/co2meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d4187d

Please sign in to comment.