diff --git a/server/src/ws/device.ts b/server/src/ws/device.ts index a8a14f9..5858a4b 100644 --- a/server/src/ws/device.ts +++ b/server/src/ws/device.ts @@ -327,6 +327,34 @@ export async function handleDeviceMessage( break; } + case "heartbeat": { + const persistentDeviceId = ws.data.persistentDeviceId; + const userId = ws.data.userId; + if (persistentDeviceId && userId) { + // Update deviceInfo in DB with latest battery + db.update(device) + .set({ + deviceInfo: { + ...(await db.select({ info: device.deviceInfo }).from(device).where(eq(device.id, persistentDeviceId)).limit(1).then(r => (r[0]?.info as Record) ?? {})), + batteryLevel: msg.batteryLevel, + isCharging: msg.isCharging, + }, + lastSeen: new Date(), + }) + .where(eq(device.id, persistentDeviceId)) + .catch((err) => console.error(`[DB] Failed to update heartbeat: ${err}`)); + + // Broadcast to dashboard + sessions.notifyDashboard(userId, { + type: "device_status", + deviceId: persistentDeviceId, + batteryLevel: msg.batteryLevel, + isCharging: msg.isCharging, + }); + } + break; + } + default: { console.warn( `Unknown message type from device ${ws.data.deviceId}:`,