11
This commit is contained in:
88
api/middleware/schedule/handlers/activeHandler.js
Normal file
88
api/middleware/schedule/handlers/activeHandler.js
Normal file
@@ -0,0 +1,88 @@
|
||||
const BaseHandler = require('./baseHandler');
|
||||
const ConfigManager = require('../services/configManager');
|
||||
const command = require('../command');
|
||||
const config = require('../config');
|
||||
|
||||
/**
|
||||
* 自动活跃处理器
|
||||
* 负责保持账户活跃度
|
||||
*/
|
||||
class ActiveHandler extends BaseHandler {
|
||||
/**
|
||||
* 处理自动活跃任务
|
||||
* @param {object} task - 任务对象
|
||||
* @returns {Promise<object>} 执行结果
|
||||
*/
|
||||
async handle(task) {
|
||||
return await this.execute(task, async () => {
|
||||
return await this.doActive(task);
|
||||
}, {
|
||||
checkAuth: true,
|
||||
checkOnline: true,
|
||||
recordDeviceMetrics: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行活跃逻辑
|
||||
*/
|
||||
async doActive(task) {
|
||||
const { sn_code, taskParams } = task;
|
||||
const { platform = 'boss' } = taskParams;
|
||||
|
||||
console.log(`[自动活跃] 开始 - 设备: ${sn_code}`);
|
||||
|
||||
// 1. 获取账户配置
|
||||
const accountConfig = await this.getAccountConfig(sn_code, ['platform_type', 'active_strategy']);
|
||||
|
||||
if (!accountConfig) {
|
||||
return {
|
||||
activeCount: 0,
|
||||
message: '未找到账户配置'
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 解析活跃策略配置
|
||||
const activeStrategy = ConfigManager.parseActiveStrategy(accountConfig.active_strategy);
|
||||
|
||||
// 3. 检查活跃时间范围
|
||||
const timeRange = ConfigManager.getTimeRange(activeStrategy);
|
||||
if (timeRange) {
|
||||
const timeRangeValidator = require('../services/timeRangeValidator');
|
||||
const timeCheck = timeRangeValidator.checkTimeRange(timeRange);
|
||||
|
||||
if (!timeCheck.allowed) {
|
||||
return {
|
||||
activeCount: 0,
|
||||
message: timeCheck.reason
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 创建活跃指令
|
||||
const actions = activeStrategy.actions || ['view_jobs'];
|
||||
const activeCommands = actions.map(action => ({
|
||||
command_type: `active_${action}`,
|
||||
command_name: `自动活跃 - ${action}`,
|
||||
command_params: JSON.stringify({
|
||||
sn_code,
|
||||
platform: platform || accountConfig.platform_type || 'boss',
|
||||
action
|
||||
}),
|
||||
priority: config.getTaskPriority('auto_active') || 5
|
||||
}));
|
||||
|
||||
// 5. 执行活跃指令
|
||||
const result = await command.executeCommands(task.id, activeCommands, this.mqttClient);
|
||||
|
||||
console.log(`[自动活跃] 完成 - 设备: ${sn_code}, 执行动作: ${actions.join(', ')}`);
|
||||
|
||||
return {
|
||||
activeCount: actions.length,
|
||||
actions,
|
||||
message: '活跃完成'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ActiveHandler;
|
||||
Reference in New Issue
Block a user