fix: 设备状态刷新改为全量设备列表广播
- refreshDeviceStatusToWebClients改为广播完整设备列表(devices_list_refresh事件) - 替代原来逐个设备广播device_status_update的方式 - 确保Web端能同步到所有设备的最新状态
This commit is contained in:
BIN
devices.db
BIN
devices.db
Binary file not shown.
33
src/index.ts
33
src/index.ts
@@ -2263,34 +2263,21 @@ class RemoteControlServer {
|
||||
try {
|
||||
const webClientCount = this.webClientManager.getClientCount()
|
||||
if (webClientCount === 0) {
|
||||
return // 没有Web客户端,跳过刷新
|
||||
return
|
||||
}
|
||||
|
||||
const onlineDevices = this.deviceManager.getAllDevices()
|
||||
|
||||
for (const device of onlineDevices) {
|
||||
// 验证设备Socket仍然连接
|
||||
const socket = this.io.sockets.sockets.get(device.socketId)
|
||||
if (socket && socket.connected) {
|
||||
// 广播设备在线状态
|
||||
this.webClientManager.broadcastToAll('device_status_update', {
|
||||
deviceId: device.id,
|
||||
status: {
|
||||
online: true,
|
||||
connected: true,
|
||||
lastSeen: Date.now(),
|
||||
inputBlocked: device.inputBlocked || false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (onlineDevices.length > 0) {
|
||||
this.logger.debug(`🔄 已刷新 ${onlineDevices.length} 个设备状态给 ${webClientCount} 个Web客户端`)
|
||||
// 获取完整设备列表(含历史设备)并广播给Web端
|
||||
const allDevices = this.getAllDevicesIncludingHistory()
|
||||
if (allDevices.length > 0) {
|
||||
this.webClientManager.broadcastToAll('devices_list_refresh', {
|
||||
devices: allDevices,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
this.logger.debug(`[Refresh] Sent ${allDevices.length} devices to ${webClientCount} web clients`)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('刷新设备状态失败:', error)
|
||||
this.logger.error('refreshDeviceStatusToWebClients failed:', error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user