This commit is contained in:
张成
2025-12-18 18:32:11 +08:00
parent 699c1d4f55
commit 891dfc5777
4 changed files with 37 additions and 50 deletions

View File

@@ -102,10 +102,9 @@ module.exports = {
'POST /device/list': async (ctx) => {
const models = Framework.getModels();
const { pla_account, op } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
const body = ctx.getBody();
const { isOnline, healthStatus, platform, searchText} = ctx.getBody();
// 获取分页参数
const { limit, offset } = ctx.getPageSize();
@@ -121,6 +120,11 @@ module.exports = {
];
}
// 在线状态筛选(直接查询数据库)
if (isOnline !== undefined) {
where.is_online = isOnline ? 1 : 0;
}
const result = await pla_account.findAndCountAll({
where,
limit,
@@ -128,35 +132,26 @@ module.exports = {
order: [['id', 'DESC']]
});
// 获取设备在线状态
const deviceStatus = deviceManager.getAllDevicesStatus();
// 组合数据并过滤在线状态
let list = result.rows.map(account => {
// 组合数据(直接从数据库读取)
const list = result.rows.map(account => {
const accountData = account.toJSON();
const status = deviceStatus[accountData.sn_code] || { isOnline: false };
return {
sn_code: accountData.sn_code,
device_id: accountData.device_id,
deviceName: accountData.name || accountData.sn_code,
platform: accountData.platform_type,
isOnline: status.isOnline || false,
isOnline: accountData.is_online === 1,
isRunning: false,
lastHeartbeatTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null,
lastHeartbeatTime: null,
accountName: accountData.name,
platform_type: accountData.platform_type,
is_enabled: accountData.is_enabled
};
});
// 如果指定了在线状态筛选
if (isOnline !== undefined) {
list = list.filter(item => item.isOnline === isOnline);
}
return ctx.success({
total: list.length,
list: list.slice(0, limit)
total: result.count,
list: list
});
},
@@ -174,30 +169,28 @@ module.exports = {
'GET /device/overview': async (ctx) => {
const models = Framework.getModels();
const { pla_account } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
// 从 pla_account 获取账号总数
// 从 pla_account 获取账号统计(直接查询数据库)
const totalDevices = await pla_account.count({ where: { is_delete: 0 } });
// 从 deviceManager 获取在线设备统计
const deviceStatus = deviceManager.getAllDevicesStatus();
const onlineDevices = Object.values(deviceStatus).filter(d => d.isOnline).length;
const onlineDevices = await pla_account.count({ where: { is_delete: 0, is_online: 1 } });
const runningDevices = 0; // 不再维护运行状态
const healthyDevices = onlineDevices; // 简化处理,在线即健康
const warningDevices = 0;
const errorDevices = 0;
// 获取最近离线的设备(从内存状态中获取)
const offlineDevicesList = Object.entries(deviceStatus)
.filter(([sn_code, status]) => !status.isOnline)
.map(([sn_code, status]) => ({
sn_code,
deviceName: sn_code,
lastOfflineTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null,
lastError: ''
}))
.sort((a, b) => (b.lastOfflineTime?.getTime() || 0) - (a.lastOfflineTime?.getTime() || 0))
.slice(0, 5);
// 获取最近离线的设备(从数据库查询)
const offlineAccounts = await pla_account.findAll({
where: { is_delete: 0, is_online: 0 },
limit: 5,
order: [['id', 'DESC']]
});
const offlineDevicesList = offlineAccounts.map(account => ({
sn_code: account.sn_code,
deviceName: account.name || account.sn_code,
lastOfflineTime: null,
lastError: ''
}));
return ctx.success({
totalDevices,