1
This commit is contained in:
@@ -61,16 +61,53 @@ class BaseTask {
|
||||
* 检查设备是否在线、是否登录、是否忙碌
|
||||
*/
|
||||
async checkDeviceStatus(sn_code) {
|
||||
// 1. 检查设备是否在线
|
||||
const device = deviceManager.devices.get(sn_code);
|
||||
// 1. 优先检查内存中的设备状态
|
||||
let device = deviceManager.devices.get(sn_code);
|
||||
|
||||
// 2. 如果内存中没有,降级到数据库查询(可能是刚启动还没收到心跳)
|
||||
if (!device) {
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `设备 ${sn_code} 离线(从未发送心跳)`
|
||||
};
|
||||
try {
|
||||
const pla_account = db.getModel('pla_account');
|
||||
const dbDevice = await pla_account.findOne({
|
||||
where: { sn_code, is_delete: 0 },
|
||||
attributes: ['sn_code', 'is_online', 'is_logged_in']
|
||||
});
|
||||
|
||||
if (!dbDevice) {
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `设备 ${sn_code} 不存在`
|
||||
};
|
||||
}
|
||||
|
||||
// 检查数据库中的在线状态
|
||||
if (!dbDevice.is_online) {
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `设备 ${sn_code} 离线(数据库状态)`
|
||||
};
|
||||
}
|
||||
|
||||
// 检查数据库中的登录状态
|
||||
if (this.config.requiresLogin && !dbDevice.is_logged_in) {
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `设备 ${sn_code} 未登录平台账号(数据库状态)`
|
||||
};
|
||||
}
|
||||
|
||||
// 数据库检查通过,允许执行
|
||||
return { allowed: true };
|
||||
} catch (error) {
|
||||
console.error(`[${this.taskType}] 查询设备状态失败:`, error);
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `设备 ${sn_code} 状态查询失败`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 检查心跳超时
|
||||
// 3. 检查心跳超时
|
||||
const offlineThreshold = 3 * 60 * 1000; // 3分钟
|
||||
const now = Date.now();
|
||||
const lastHeartbeat = device.lastHeartbeat || 0;
|
||||
@@ -84,7 +121,7 @@ class BaseTask {
|
||||
};
|
||||
}
|
||||
|
||||
// 3. 检查登录状态(如果任务需要)
|
||||
// 4. 检查登录状态(如果任务需要)
|
||||
if (this.config.requiresLogin && !device.isLoggedIn) {
|
||||
return {
|
||||
allowed: false,
|
||||
|
||||
Reference in New Issue
Block a user