1
This commit is contained in:
@@ -189,17 +189,17 @@ class DeviceManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查心跳状态(异步更新数据库)
|
||||
* 检查心跳状态(仅更新内存状态,device_status 表已移除)
|
||||
*/
|
||||
async checkHeartbeatStatus() {
|
||||
try {
|
||||
const now = Date.now();
|
||||
const device_status = db.getModel('device_status');
|
||||
const offlineDevices = [];
|
||||
|
||||
// 检查内存中的设备状态
|
||||
for (const [sn_code, device] of this.devices.entries()) {
|
||||
if (now - device.lastHeartbeat > config.monitoring.heartbeatTimeout) {
|
||||
// 如果之前是在线状态,现在检测到离线,需要更新数据库
|
||||
// 如果之前是在线状态,现在检测到离线
|
||||
if (device.isOnline) {
|
||||
device.isOnline = false;
|
||||
offlineDevices.push(sn_code);
|
||||
@@ -207,49 +207,10 @@ class DeviceManager {
|
||||
}
|
||||
}
|
||||
|
||||
// 批量更新数据库中的离线设备状态
|
||||
// 记录离线设备(仅日志,不再更新数据库)
|
||||
if (offlineDevices.length > 0) {
|
||||
await device_status.update(
|
||||
{ isOnline: false },
|
||||
{
|
||||
where: {
|
||||
sn_code: {
|
||||
[Sequelize.Op.in]: offlineDevices
|
||||
},
|
||||
isOnline: true // 只更新当前在线的设备,避免重复更新
|
||||
}
|
||||
}
|
||||
);
|
||||
console.log(`[设备管理器] 检测到 ${offlineDevices.length} 个设备心跳超时,已同步到数据库: ${offlineDevices.join(', ')}`);
|
||||
}
|
||||
|
||||
// 同时检查数据库中的设备状态(处理内存中没有但数据库中有心跳超时的情况)
|
||||
const heartbeatTimeout = config.monitoring.heartbeatTimeout;
|
||||
const heartbeatThreshold = new Date(now - heartbeatTimeout);
|
||||
|
||||
const timeoutDevices = await device_status.findAll({
|
||||
where: {
|
||||
isOnline: true,
|
||||
lastHeartbeatTime: {
|
||||
[Sequelize.Op.lt]: heartbeatThreshold
|
||||
}
|
||||
},
|
||||
attributes: ['sn_code', 'lastHeartbeatTime']
|
||||
});
|
||||
|
||||
if (timeoutDevices.length > 0) {
|
||||
const timeoutSnCodes = timeoutDevices.map(dev => dev.sn_code);
|
||||
await device_status.update(
|
||||
{ isOnline: false },
|
||||
{
|
||||
where: {
|
||||
sn_code: {
|
||||
[Sequelize.Op.in]: timeoutSnCodes
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
console.log(`[设备管理器] 从数据库检测到 ${timeoutSnCodes.length} 个心跳超时设备,已更新为离线: ${timeoutSnCodes.join(', ')}`);
|
||||
console.log(`[设备管理器] 检测到 ${offlineDevices.length} 个设备心跳超时: ${offlineDevices.join(', ')}`);
|
||||
// 注意:device_status 表已移除,设备状态仅在内存中维护
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[设备管理器] 检查心跳状态失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user