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);
|
||||
|
||||
@@ -219,7 +219,18 @@ class TaskQueue {
|
||||
|
||||
const enabledSnCodes = new Set(enabledAccounts.map(acc => acc.sn_code));
|
||||
|
||||
// 检查设备在线状态(需要同时满足:isOnline = true 且心跳未超时)
|
||||
// 移除 device_status 依赖,不再检查设备在线状态
|
||||
// 如果需要在线状态检查,可以从 deviceManager 获取
|
||||
const deviceManager = require('./deviceManager');
|
||||
const deviceStatus = deviceManager.getAllDevicesStatus();
|
||||
const onlineSnCodes = new Set(
|
||||
Object.entries(deviceStatus)
|
||||
.filter(([sn_code, status]) => status.isOnline)
|
||||
.map(([sn_code]) => sn_code)
|
||||
);
|
||||
|
||||
// 原有代码已移除,改为使用 deviceManager
|
||||
/* 原有代码已注释
|
||||
const device_status = db.getModel('device_status');
|
||||
const heartbeatTimeout = require('./config.js').monitoring.heartbeatTimeout;
|
||||
const now = new Date();
|
||||
@@ -235,6 +246,7 @@ class TaskQueue {
|
||||
attributes: ['sn_code']
|
||||
});
|
||||
const onlineSnCodes = new Set(onlineDevices.map(dev => dev.sn_code));
|
||||
*/
|
||||
|
||||
let processedCount = 0;
|
||||
let queuedCount = 0;
|
||||
|
||||
Reference in New Issue
Block a user