diff --git a/api/controller_admin/device_monitor.js b/api/controller_admin/device_monitor.js index 3326606..e8b2633 100644 --- a/api/controller_admin/device_monitor.js +++ b/api/controller_admin/device_monitor.js @@ -62,7 +62,6 @@ module.exports = { 'POST /device/detail': async (ctx) => { const models = Framework.getModels(); const { pla_account } = models; - const deviceManager = require('../middleware/schedule/deviceManager'); const body = ctx.getBody(); const { deviceSn } = body; @@ -70,7 +69,7 @@ module.exports = { return ctx.fail('设备SN码不能为空'); } - // 从 pla_account 获取账号信息 + // 从 pla_account 获取账号信息(包含 is_online 和 is_logged_in) const account = await pla_account.findOne({ where: { sn_code: deviceSn } }); @@ -81,21 +80,17 @@ module.exports = { const accountData = account.toJSON(); - // 从 deviceManager 获取在线状态 - const deviceStatus = deviceManager.getAllDevicesStatus(); - const onlineStatus = deviceStatus[deviceSn] || { isOnline: false }; - - // 组合返回数据 + // 组合返回数据(直接从数据库读取 is_online 和 is_logged_in) const deviceData = { sn_code: accountData.sn_code, device_id: accountData.device_id, deviceName: accountData.name || accountData.sn_code, platform: accountData.platform_type, - isOnline: onlineStatus.isOnline || false, - is_online: onlineStatus.isOnline || false, // 前端使用的字段名 - is_logged_in: onlineStatus.isLoggedIn || false, // 从 deviceManager 内存中获取登录状态 + isOnline: accountData.is_online === 1, + is_online: accountData.is_online === 1, + is_logged_in: accountData.is_logged_in === 1, isRunning: false, // 不再维护运行状态 - lastHeartbeatTime: onlineStatus.lastHeartbeat ? new Date(onlineStatus.lastHeartbeat) : null, + lastHeartbeatTime: null, // 不再从内存读取 accountName: accountData.name, platform_type: accountData.platform_type, is_enabled: accountData.is_enabled diff --git a/api/middleware/mqtt/mqttDispatcher.js b/api/middleware/mqtt/mqttDispatcher.js index 8355bc5..d10b20d 100644 --- a/api/middleware/mqtt/mqttDispatcher.js +++ b/api/middleware/mqtt/mqttDispatcher.js @@ -276,17 +276,20 @@ class MqttDispatcher { } } - // 移除 device_status 更新逻辑 - // 如果需要在 pla_account 表中添加在线状态字段,可以在这里更新 - console.log(`[MQTT心跳] 设备 ${sn_code} 心跳已接收 - 登录: ${updateData.isLoggedIn || false}`); - - // if (device) { - // await device_status.update(updateData, { where: { sn_code } }); - // console.log(`[MQTT心跳] 设备 ${sn_code} 状态已更新 - 在线: true, 登录: ${updateData.isLoggedIn}`); - // } else { - // logProxy.error('[MQTT心跳] 设备 ${sn_code} 不存在', { sn_code }); - // return; - // } + // 更新 pla_account 表中的在线和登录状态 + try { + const models = db.getModels(); + await models.pla_account.update( + { + is_online: 1, + is_logged_in: updateData.isLoggedIn ? 1 : 0 + }, + { where: { sn_code } } + ); + console.log(`[MQTT心跳] 设备 ${sn_code} 状态已更新到数据库 - 在线: true, 登录: ${updateData.isLoggedIn || false}`); + } catch (error) { + console.error(`[MQTT心跳] 更新数据库状态失败:`, error); + } // 记录心跳到设备管理器(包含登录状态) const heartbeatPayload = { diff --git a/api/model/pla_account.js b/api/model/pla_account.js index 9f37a84..b23aecd 100644 --- a/api/model/pla_account.js +++ b/api/model/pla_account.js @@ -52,6 +52,18 @@ module.exports = (db) => { allowNull: false, defaultValue: 1 }, + is_online: { + comment: '设备在线状态(1=在线,0=离线)', + type: Sequelize.TINYINT(1), + allowNull: false, + defaultValue: 0 + }, + is_logged_in: { + comment: '平台登录状态(1=已登录,0=未登录)', + type: Sequelize.TINYINT(1), + allowNull: false, + defaultValue: 0 + }, job_type_id: { comment: '职位类型ID(关联 job_types 表)', type: Sequelize.INTEGER, diff --git a/api/services/pla_account_service.js b/api/services/pla_account_service.js index 74655d3..919c6b3 100644 --- a/api/services/pla_account_service.js +++ b/api/services/pla_account_service.js @@ -24,9 +24,14 @@ class PlaAccountService { const accountData = account.get({ plain: true }); - // 移除 device_status 依赖,在线状态和登录状态设为默认值 - accountData.is_online = false; - accountData.is_logged_in = false; + // is_online 和 is_logged_in 字段已存在于数据库中,直接返回 + // 如果字段不存在,设置默认值 + if (accountData.is_online === undefined) { + accountData.is_online = false; + } + if (accountData.is_logged_in === undefined) { + accountData.is_logged_in = false; + } return accountData; } @@ -57,9 +62,14 @@ class PlaAccountService { const accountData = account.get({ plain: true }); - // 移除 device_status 依赖,在线状态和登录状态设为默认值 - accountData.is_online = false; - accountData.is_logged_in = false; + // is_online 和 is_logged_in 字段已存在于数据库中,直接返回 + // 如果字段不存在,设置默认值 + if (accountData.is_online === undefined) { + accountData.is_online = false; + } + if (accountData.is_logged_in === undefined) { + accountData.is_logged_in = false; + } return accountData; } @@ -102,10 +112,16 @@ class PlaAccountService { order: [['id', 'DESC']] }); - // 处理返回数据,is_online 设为默认值 false + // 处理返回数据,is_online 和 is_logged_in 从数据库读取 const rows = result.rows.map(account => { const accountData = account.get({ plain: true }); - accountData.is_online = false; + // 如果字段不存在,设置默认值 + if (accountData.is_online === undefined) { + accountData.is_online = false; + } + if (accountData.is_logged_in === undefined) { + accountData.is_logged_in = false; + } return accountData; });