Merge branch 'main' of https://git.light120.com/zc/autoAiWorkSys
This commit is contained in:
@@ -18,6 +18,35 @@ module.exports = {
|
||||
const accountData = await plaAccountService.getAccountById(id);
|
||||
return ctx.success(accountData);
|
||||
},
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /admin_api/account/getBySnCode:
|
||||
* post:
|
||||
* summary: 通过设备SN码获取账号信息
|
||||
* description: 根据设备SN码直接获取对应的账号信息
|
||||
* tags: [后台-账号管理]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - sn_code
|
||||
* properties:
|
||||
* sn_code:
|
||||
* type: string
|
||||
* description: 设备SN码
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 获取成功
|
||||
*/
|
||||
'POST /account/getBySnCode': async (ctx) => {
|
||||
const { sn_code } = ctx.getBody();
|
||||
const accountData = await plaAccountService.getAccountBySnCode(sn_code);
|
||||
return ctx.success(accountData);
|
||||
},
|
||||
/**
|
||||
* @swagger
|
||||
* /admin_api/account/list:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const db = require('../dbProxy.js');
|
||||
const logProxy = require('../logProxy.js');
|
||||
const deviceManager = require('../schedule/deviceManager.js');
|
||||
|
||||
/**
|
||||
@@ -192,7 +193,7 @@ class MqttDispatcher {
|
||||
heartbeatData = JSON.parse(message);
|
||||
}
|
||||
|
||||
const { sn_code, timestamp, status, memory, platform_status, platform_login_status } = heartbeatData;
|
||||
const { sn_code, timestamp, status, memory, platform_status, platform_login_status,clientId } = heartbeatData;
|
||||
|
||||
if (!sn_code) {
|
||||
console.warn('[MQTT心跳] 心跳消息中未找到设备SN码');
|
||||
@@ -231,6 +232,7 @@ class MqttDispatcher {
|
||||
loggedInPlatform = platform_login_status.platform || null;
|
||||
loggedInUsername = platform_login_status.username || '';
|
||||
loggedInUserId = platform_login_status.user_id || null;
|
||||
|
||||
|
||||
if (platform_login_status.timestamp) {
|
||||
loginTime = new Date(platform_login_status.timestamp);
|
||||
@@ -266,19 +268,11 @@ class MqttDispatcher {
|
||||
if (device) {
|
||||
await device_status.update(updateData, { where: { sn_code } });
|
||||
console.log(`[MQTT心跳] 设备 ${sn_code} 状态已更新 - 在线: true, 登录: ${updateData.isLoggedIn}`);
|
||||
} else {
|
||||
// 创建新设备记录
|
||||
await device_status.create({
|
||||
sn_code,
|
||||
deviceName: `设备_${sn_code}`,
|
||||
deviceType: 'node_mqtt_client',
|
||||
...updateData,
|
||||
isRunning: false,
|
||||
taskStatus: 'idle',
|
||||
healthStatus: 'unknown',
|
||||
healthScore: 0
|
||||
});
|
||||
console.log(`[MQTT心跳] 设备 ${sn_code} 记录已创建 - 在线: true, 登录: ${updateData.isLoggedIn}`);
|
||||
}
|
||||
else
|
||||
{
|
||||
logProxy.error('[MQTT心跳] 设备 ${sn_code} 不存在', { sn_code });
|
||||
return;
|
||||
}
|
||||
|
||||
// 记录心跳到设备管理器
|
||||
|
||||
@@ -37,6 +37,46 @@ class PlaAccountService {
|
||||
return accountData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备SN码获取账号信息
|
||||
* @param {string} sn_code - 设备SN码
|
||||
* @returns {Promise<Object>} 账号信息
|
||||
*/
|
||||
async getAccountBySnCode(sn_code) {
|
||||
const pla_account = db.getModel('pla_account');
|
||||
const device_status = db.getModel('device_status');
|
||||
|
||||
if (!sn_code) {
|
||||
throw new Error('设备SN码不能为空');
|
||||
}
|
||||
|
||||
// 根据 sn_code 查询账号,排除已删除的账号
|
||||
const account = await pla_account.findOne({
|
||||
where: {
|
||||
sn_code: sn_code,
|
||||
is_delete: 0
|
||||
}
|
||||
});
|
||||
|
||||
if (!account) {
|
||||
throw new Error('账号不存在');
|
||||
}
|
||||
|
||||
const accountData = account.get({ plain: true });
|
||||
|
||||
// 从 device_status 查询在线状态和登录状态
|
||||
const deviceStatus = await device_status.findByPk(sn_code);
|
||||
if (deviceStatus) {
|
||||
accountData.is_online = deviceStatus.isOnline || false;
|
||||
accountData.is_logged_in = deviceStatus.isLoggedIn || false;
|
||||
} else {
|
||||
accountData.is_online = false;
|
||||
accountData.is_logged_in = false;
|
||||
}
|
||||
|
||||
return accountData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账号列表
|
||||
* @param {Object} params - 查询参数
|
||||
|
||||
Reference in New Issue
Block a user