Skip to content

Commit

Permalink
Corrected unpack_storm_date
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Feb 9, 2024
1 parent 1894d40 commit 92bb125
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "PyVantagePro_MarcoGos"
version = "0.3.12"
version = "0.3.13"
authors = [
{ name="Salem Harrache", email="[email protected]" },
{ name="Marco Gosselink", email="[email protected]" },
Expand Down
21 changes: 12 additions & 9 deletions pyvantagepro/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .compat import bytes
from .logger import LOGGER
from .utils import (cached_property, bytes_to_hex, Dict, bytes_to_binary,
binary_to_int)
binary_to_int, word_to_binary)


class VantageProCRC(object):
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(self, data, dtime):
self['RainStorm'] = self['RainStorm'] / 100
self['UV'] = self['UV'] / 10
# Given a packed storm date field, unpack and return date
self['StormStartDate'] = self.unpack_storm_date()
self['StormStartDate'] = self.unpack_storm_date(self['StormStartDate'])
# rain totals
self['RainDay'] = self['RainDay'] / 100
self['RainMonth'] = self['RainMonth'] / 100
Expand Down Expand Up @@ -248,13 +248,16 @@ def __init__(self, data, dtime):
self.tuple_to_dict("LeafWetness")
self.tuple_to_dict("SoilMoist")

def unpack_storm_date(self):
'''Given a packed storm date field, unpack and return date.'''
date = bytes_to_binary(self.raw_bytes[48:50])
year = binary_to_int(date, 0, 7) + 2000
day = binary_to_int(date, 7, 12)
month = binary_to_int(date, 12, 16)
return "%s-%s-%s" % (year, month, day)
def unpack_storm_date(self, date):
'''Given a packed storm date field, unpack and return date.'''
b = word_to_binary(date)

year = binary_to_int(b, 0, 7) + 2000
month = binary_to_int(b, 12, 16)
day = binary_to_int(b, 7, 12)

return "%d-%0.2d-%0.2d" % (year, month, day)


def unpack_time(self, time):
'''Given a packed time field, unpack and return "HH:MM" string.'''
Expand Down
8 changes: 8 additions & 0 deletions pyvantagepro/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def byte_to_binary(byte):
'''
return ''.join(str((byte & (1 << i)) and 1) for i in reversed(range(8)))

def word_to_binary(word):
'''Convert byte to binary string representation.
E.g.
>>> byte_to_binary("\x4A")
'0000000001001010'
'''
return ''.join(str((word & (1 << i)) and 1) for i in reversed(range(16)))


def bytes_to_binary(values):
'''Convert bytes to binary string representation.
Expand Down

0 comments on commit 92bb125

Please sign in to comment.