Skip to content

Commit

Permalink
Sleep log acquisition and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
crosser committed Nov 25, 2024
1 parent 9198047 commit 06a9ca8
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bluering/opsv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,54 @@ def result(self) -> str:
for dt, lo, hi in dated_recs
if lo or hi
)


class SleepLog(Opv2):
"""
Report history of sleep
"""

OPCODE = 0x27

sndbuf = b"\x01\x00\xff\x00\xff"

def result(self) -> str:
numdays = self.payload[0]
today = date.today()

def days() -> bytes:
rest = self.payload[1:]
while rest:
ago = rest[0]
sz = rest[1]
yield (ago, rest[2 : sz + 2])
rest = rest[sz + 2 :]

def timeof(ago, minoff) -> datetime:
return (
datetime(*today.timetuple()[:3])
- timedelta(days=ago)
+ timedelta(minutes=minoff)
)

def sleepmode(i: int) -> str:
return {2: "l", 3: "d", 4: "r", 5: "a"}.get(i, "?")

output = ""
for ago, cont in days():
sleepstart, sleepend = unpack("<HH", cont[:4])
if sleepstart > sleepend:
sleepstart -= 1440 # minutes in the day
beg = timeof(ago, sleepstart)
end = timeof(ago, sleepend)
hr, mi = divmod((end - beg).seconds // 60, 60)
output += (
f"{beg.isoformat()} - {end.isoformat()} ({hr}:{mi:02})\n\t"
+ ", ".join(
f"{cont[4:][i * 2 + 1]}{sleepmode(cont[4:][i * 2])}"
for i in range(0, (len(cont) - 4) // 2)
)
+ "\n"
)
output += "(Sleep start - end (total); minutes [l]ight/[d]eep sleep, [a]wake)"
return output

0 comments on commit 06a9ca8

Please sign in to comment.