kjoconnor / pyhatchbabyrest

Python library to control the Hatch Baby Rest device.
MIT License
19 stars 9 forks source link

added time functions #9

Open trail-coffee opened 8 months ago

trail-coffee commented 8 months ago

Added time functions based on these captures

Set command (ignore the "R..", part of packet header):

Dec 13 9:06:13.202
00000000: 5212 0053 5432 3032 3331 3231 3330 3930  R..ST20231213090
00000010: 3631 3255                                612U

Packet used in refresh_data function:

Dec 13 9:06:14.108
00000000: 1B18 0054 6579 7405 43FE FEFE FF53 092C  ...Teyt.C....S.,
00000010: 50C3 6500 0000 00                        P.e....

0x54 is the time header, 6579 7405 is the time, convert that to decimal is 1702458373, which would be December 13, 2023 9:06:13 AM. Hatch looks like it's in epoch seconds in local time and not UTC, so python is a bit confused by it.

ViViDboarder commented 8 months ago

Oh, interesting. I had parsed the time using

timestamp = int.from_bytes(bytes(raw_char_read[1:5]))
self.time = datetime.datetime.fromtimestamp(timestamp, datetime.UTC)

This appears to print the correct time for me.

Oh, yours is basically the exact same thing except with struct_time rather than datetime.

ViViDboarder commented 8 months ago

It's interesting that the value is stored as epoch but set as a string.

ViViDboarder commented 8 months ago

Works for me! I have no problems with using struct_time here rather than datetime, so I'm happy to say ship it.

trail-coffee commented 8 months ago

Works for me! I have no problems with using struct_time here rather than datetime, so I'm happy to say ship it.

I'm new to python so if struct_time is going to be weird to python people, i'll change it.