This commit is contained in:
张成
2025-12-18 18:18:43 +08:00
parent 56e40efadb
commit 699c1d4f55
4 changed files with 56 additions and 30 deletions

View File

@@ -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

View File

@@ -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 = {

View File

@@ -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,

View File

@@ -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;
});