fix is_awake

This commit is contained in:
Oliver 2025-12-08 11:43:04 +00:00
parent 0df223bb29
commit e65b32ca84

45
main.py
View File

@ -173,21 +173,21 @@ class GadgetbridgeMQTT:
day_start -= timedelta(days=1) day_start -= timedelta(days=1)
return int(day_start.timestamp()) return int(day_start.timestamp())
def get_day_midnight_timestamp_ms(self): def get_day_midnight_timestamp(self):
"""Get midnight timestamp in milliseconds for daily summary queries""" """Get midnight timestamp in seconds for daily summary queries"""
now = datetime.now() now = datetime.now()
today = now.date() today = now.date()
midnight = datetime.combine(today, datetime.min.time()) midnight = datetime.combine(today, datetime.min.time())
if now.hour < 4: if now.hour < 4:
midnight -= timedelta(days=1) midnight -= timedelta(days=1)
return int(midnight.timestamp()) * 1000 return int(midnight.timestamp())
def query_sensors(self, cursor): def query_sensors(self, cursor):
"""Query all sensor data from database - filtered by device_id""" """Query all sensor data from database - filtered by device_id"""
data = {} data = {}
day_start_ts = self.get_day_start_timestamp() day_start_ts = self.get_day_start_timestamp()
now_ts = int(datetime.now().timestamp()) now_ts = int(datetime.now().timestamp())
day_midnight_ms = self.get_day_midnight_timestamp_ms() day_midnight = self.get_day_midnight_timestamp()
# Daily Steps (filtered by device) # Daily Steps (filtered by device)
try: try:
@ -242,7 +242,7 @@ class GadgetbridgeMQTT:
try: try:
cursor.execute( cursor.execute(
"SELECT HR_RESTING, HR_MAX, HR_AVG, CALORIES FROM XIAOMI_DAILY_SUMMARY_SAMPLE WHERE DEVICE_ID = ? AND TIMESTAMP >= ? ORDER BY TIMESTAMP DESC LIMIT 1", "SELECT HR_RESTING, HR_MAX, HR_AVG, CALORIES FROM XIAOMI_DAILY_SUMMARY_SAMPLE WHERE DEVICE_ID = ? AND TIMESTAMP >= ? ORDER BY TIMESTAMP DESC LIMIT 1",
(self.device_id, day_midnight_ms) (self.device_id, day_midnight)
) )
row = cursor.fetchone() row = cursor.fetchone()
if row: if row:
@ -255,22 +255,35 @@ class GadgetbridgeMQTT:
# Sleep Data (filtered by device) # Sleep Data (filtered by device)
try: try:
day_ago_ms = (int(time.time()) - 24 * 3600) * 1000 day_ago_ts = int(time.time()) - 24 * 3600
cursor.execute( cursor.execute(
"SELECT TOTAL_DURATION, IS_AWAKE, WAKEUP_TIME FROM XIAOMI_SLEEP_TIME_SAMPLE WHERE DEVICE_ID = ? AND TIMESTAMP >= ? ORDER BY TIMESTAMP DESC LIMIT 1", """
(self.device_id, day_ago_ms) SELECT TOTAL_DURATION, IS_AWAKE, WAKEUP_TIME
FROM XIAOMI_SLEEP_TIME_SAMPLE
WHERE DEVICE_ID = ? AND TIMESTAMP >= ?
ORDER BY TIMESTAMP DESC
LIMIT 1
""",
(self.device_id, day_ago_ts)
) )
row = cursor.fetchone() row = cursor.fetchone()
if row: if row:
if row[0]: total_duration, is_awake_flag, wakeup_raw = row
data["sleep_duration"] = round(row[0] / 60, 2) # Convert to hours
# Determine if awake # Convert duration to hours
is_awake = True if total_duration is not None:
if row[1] == 0: data["sleep_duration"] = round(total_duration / 60.0, 2)
is_awake = False # NULL means "not finalized", treat as False (not awake)
if row[2] and row[2] <= int(time.time()) * 1000: # 0 means explicitly "still asleep"
is_awake = True # 1 means explicitly "woke up"
if is_awake_flag is None:
is_awake = False # No data = not awake
else:
is_awake = (is_awake_flag == 1)
data["is_awake"] = is_awake data["is_awake"] = is_awake
except Exception as e: except Exception as e:
logger.debug(f"Sleep query failed: {e}") logger.debug(f"Sleep query failed: {e}")