Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 60th minute ness bug #26

Merged
merged 1 commit into from Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion nessclient/packet.py
Expand Up @@ -184,4 +184,22 @@ def is_user_interface_resp(start: int) -> bool:


def decode_timestamp(data: str) -> datetime.datetime:
return datetime.datetime.strptime(data, '%y%m%d%H%M%S')
"""
Decode timestamp using bespoke decoder.
Cannot use simple strptime since the ness panel contains a bug
that P199E zone and state updates emitted on the hour cause a minute
value of `60` to be sent, causing strptime to fail. This decoder handles
this edge case.
"""
year = 2000 + int(data[0:2])
month = int(data[2:4])
day = int(data[4:6])
hour = int(data[6:8])
minute = int(data[8:10])
second = int(data[10:12])
if minute == 60:
minute = 0
hour += 1

return datetime.datetime(year=year, month=month, day=day, hour=hour,
minute=minute, second=second)
11 changes: 11 additions & 0 deletions nessclient_tests/test_packet.py
Expand Up @@ -120,3 +120,14 @@ def test_decode_status_update_response(self):
self.assertEqual(pkt.data, '070000')
self.assertIsNone(pkt.timestamp)
# self.assertEqual(pkt.checksum, 0x14)

def test_bad_timestamp(self):
pkt = Packet.decode('8700036100070019022517600057')
self.assertEqual(pkt.start, 0x87)
self.assertEqual(pkt.address, 0x00)
self.assertEqual(pkt.length, 3)
self.assertEqual(pkt.seq, 0x00)
self.assertEqual(pkt.command, CommandType.SYSTEM_STATUS)
self.assertEqual(pkt.data, '000700')
self.assertEqual(pkt.timestamp, datetime.datetime(
year=2019, month=2, day=25, hour=18, minute=0, second=0))