1
This commit is contained in:
@@ -78,8 +78,10 @@ class DeviceWorkStatusNotifier {
|
||||
};
|
||||
|
||||
// 如果有当前执行的指令,优先显示指令状态
|
||||
if (options.currentCommand) {
|
||||
const cmd = options.currentCommand;
|
||||
// 优先从 options 中获取,如果没有则从 taskStatusSummary 中获取
|
||||
const currentCommand = options.currentCommand || taskStatusSummary.currentCommand;
|
||||
if (currentCommand) {
|
||||
const cmd = currentCommand;
|
||||
workStatus.currentActivity = {
|
||||
type: 'command',
|
||||
id: cmd.command_id || cmd.id,
|
||||
@@ -87,8 +89,8 @@ class DeviceWorkStatusNotifier {
|
||||
description: this._formatCommandDescription(cmd),
|
||||
status: 'running',
|
||||
progress: cmd.progress || 0,
|
||||
currentStep: cmd.currentStep || '',
|
||||
startTime: cmd.startTime || new Date().toISOString()
|
||||
currentStep: cmd.currentStep || cmd.current_step || '',
|
||||
startTime: cmd.startTime || cmd.start_time || new Date().toISOString()
|
||||
};
|
||||
}
|
||||
// 如果有当前执行的任务,显示任务状态
|
||||
@@ -146,8 +148,16 @@ class DeviceWorkStatusNotifier {
|
||||
|
||||
await mqttClient.publish(topic, message);
|
||||
|
||||
// 输出日志
|
||||
console.log(`[设备工作状态] 已推送到 ${sn_code}: ${workStatus.displayText}`);
|
||||
// 输出详细日志
|
||||
if (workStatus.currentActivity) {
|
||||
const activity = workStatus.currentActivity;
|
||||
const activityInfo = activity.type === 'command'
|
||||
? `指令[${activity.name}]`
|
||||
: `任务[${activity.name}]`;
|
||||
console.log(`[设备工作状态] 已推送到 ${sn_code}: ${activityInfo} - ${workStatus.displayText}`);
|
||||
} else {
|
||||
console.log(`[设备工作状态] 已推送到 ${sn_code}: ${workStatus.displayText}`);
|
||||
}
|
||||
} catch (error) {
|
||||
// 通知失败不影响任务执行,只记录日志
|
||||
console.warn(`[设备工作状态] 推送失败:`, error.message);
|
||||
@@ -232,7 +242,15 @@ class DeviceWorkStatusNotifier {
|
||||
const statusText = activity.status === 'running' ? '执行中' :
|
||||
activity.status === 'completed' ? '已完成' :
|
||||
activity.status === 'failed' ? '失败' : '未知';
|
||||
parts.push(`${typeText}: ${activity.description || activity.name} (${statusText}, 进度: ${activity.progress}%)`);
|
||||
|
||||
// 构建详细描述:包含指令/任务名称和描述
|
||||
let activityDesc = activity.description || activity.name;
|
||||
if (activity.type === 'command' && activity.currentStep) {
|
||||
// 对于指令,如果有当前步骤信息,追加到描述中
|
||||
activityDesc = `${activityDesc} - ${activity.currentStep}`;
|
||||
}
|
||||
|
||||
parts.push(`${typeText}: ${activityDesc} (${statusText}${activity.progress > 0 ? `, 进度: ${activity.progress}%` : ''})`);
|
||||
} else {
|
||||
parts.push('当前活动: 无');
|
||||
}
|
||||
|
||||
@@ -335,7 +335,9 @@ class ScheduledJobs {
|
||||
try {
|
||||
const deviceWorkStatusNotifier = require('./deviceWorkStatusNotifier');
|
||||
const summary = await this.taskQueue.getTaskStatusSummary(sn_code);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(sn_code, summary);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(sn_code, summary, {
|
||||
currentCommand: summary.currentCommand || null
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[设备工作状态同步] 设备 ${sn_code} 同步失败:`, error.message);
|
||||
}
|
||||
|
||||
@@ -585,7 +585,9 @@ class TaskQueue {
|
||||
|
||||
// 推送设备工作状态(任务开始执行)
|
||||
const summary = await this.getTaskStatusSummary(task.sn_code);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(task.sn_code, summary);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(task.sn_code, summary, {
|
||||
currentCommand: summary.currentCommand || null
|
||||
});
|
||||
|
||||
// 使用注册的任务处理器执行任务
|
||||
const handler = this.taskHandlers.get(task.taskType);
|
||||
@@ -660,7 +662,9 @@ class TaskQueue {
|
||||
|
||||
// 推送设备工作状态(任务失败)
|
||||
const summary = await this.getTaskStatusSummary(task.sn_code);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(task.sn_code, summary);
|
||||
await deviceWorkStatusNotifier.sendDeviceWorkStatus(task.sn_code, summary, {
|
||||
currentCommand: summary.currentCommand || null
|
||||
});
|
||||
} catch (dbError) {
|
||||
console.error(`[任务队列] 更新任务失败状态到数据库失败:`, dbError);
|
||||
}
|
||||
@@ -1165,6 +1169,37 @@ class TaskQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前正在执行的指令(从数据库查询状态为 running 的指令)
|
||||
let currentCommand = null;
|
||||
try {
|
||||
const taskCommandsModel = db.getModel('task_commands');
|
||||
// 查找该设备下当前正在执行的指令(通过关联的 task_id 查找)
|
||||
if (currentTask && currentTask.taskId) {
|
||||
const runningCommand = await taskCommandsModel.findOne({
|
||||
where: {
|
||||
task_id: currentTask.taskId,
|
||||
status: 'running'
|
||||
},
|
||||
order: [['id', 'DESC']]
|
||||
});
|
||||
|
||||
if (runningCommand) {
|
||||
const cmdData = runningCommand.toJSON();
|
||||
currentCommand = {
|
||||
command_id: cmdData.id,
|
||||
command_name: cmdData.command_name || '执行指令',
|
||||
command_type: cmdData.command_type || '',
|
||||
command_params: cmdData.command_params || cmdData.params || {},
|
||||
progress: cmdData.progress || 0,
|
||||
currentStep: cmdData.current_step || '',
|
||||
startTime: cmdData.start_time || cmdData.created_time || new Date().toISOString()
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[任务队列] 查询当前执行指令失败:`, error);
|
||||
}
|
||||
|
||||
// 计算总待执行数:队列中的任务数 + 当前任务中剩余的步骤数
|
||||
let totalPendingCount = queue ? queue.size() : 0;
|
||||
|
||||
@@ -1190,6 +1225,7 @@ class TaskQueue {
|
||||
return {
|
||||
sn_code,
|
||||
currentTask,
|
||||
currentCommand, // 当前正在执行的指令
|
||||
pendingTasks,
|
||||
nextTaskTime,
|
||||
pendingCount: queue ? queue.size() : 0, // 队列中的任务数
|
||||
@@ -1202,6 +1238,7 @@ class TaskQueue {
|
||||
return {
|
||||
sn_code,
|
||||
currentTask: null,
|
||||
currentCommand: null,
|
||||
pendingTasks: [],
|
||||
nextTaskTime: null,
|
||||
pendingCount: 0,
|
||||
|
||||
Reference in New Issue
Block a user