fix is_awake
This commit is contained in:
parent
0df223bb29
commit
e65b32ca84
45
main.py
45
main.py
@ -173,21 +173,21 @@ class GadgetbridgeMQTT:
|
||||
day_start -= timedelta(days=1)
|
||||
return int(day_start.timestamp())
|
||||
|
||||
def get_day_midnight_timestamp_ms(self):
|
||||
"""Get midnight timestamp in milliseconds for daily summary queries"""
|
||||
def get_day_midnight_timestamp(self):
|
||||
"""Get midnight timestamp in seconds for daily summary queries"""
|
||||
now = datetime.now()
|
||||
today = now.date()
|
||||
midnight = datetime.combine(today, datetime.min.time())
|
||||
if now.hour < 4:
|
||||
midnight -= timedelta(days=1)
|
||||
return int(midnight.timestamp()) * 1000
|
||||
return int(midnight.timestamp())
|
||||
|
||||
def query_sensors(self, cursor):
|
||||
"""Query all sensor data from database - filtered by device_id"""
|
||||
data = {}
|
||||
day_start_ts = self.get_day_start_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)
|
||||
try:
|
||||
@ -242,7 +242,7 @@ class GadgetbridgeMQTT:
|
||||
try:
|
||||
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",
|
||||
(self.device_id, day_midnight_ms)
|
||||
(self.device_id, day_midnight)
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
@ -255,22 +255,35 @@ class GadgetbridgeMQTT:
|
||||
|
||||
# Sleep Data (filtered by device)
|
||||
try:
|
||||
day_ago_ms = (int(time.time()) - 24 * 3600) * 1000
|
||||
day_ago_ts = int(time.time()) - 24 * 3600
|
||||
|
||||
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()
|
||||
if row:
|
||||
if row[0]:
|
||||
data["sleep_duration"] = round(row[0] / 60, 2) # Convert to hours
|
||||
# Determine if awake
|
||||
is_awake = True
|
||||
if row[1] == 0:
|
||||
is_awake = False
|
||||
if row[2] and row[2] <= int(time.time()) * 1000:
|
||||
is_awake = True
|
||||
total_duration, is_awake_flag, wakeup_raw = row
|
||||
|
||||
# Convert duration to hours
|
||||
if total_duration is not None:
|
||||
data["sleep_duration"] = round(total_duration / 60.0, 2)
|
||||
# NULL means "not finalized", treat as False (not awake)
|
||||
# 0 means explicitly "still asleep"
|
||||
# 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
|
||||
|
||||
except Exception as e:
|
||||
logger.debug(f"Sleep query failed: {e}")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user