关联实际触发器对象的代码清理

This commit is contained in:
辉鸭蛋
2025-10-02 16:00:35 +08:00
parent d6bfa5f736
commit ef66e2ff75
6 changed files with 31 additions and 78 deletions

View File

@@ -1,14 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.Model.Gear.Tasks;
namespace BetterGenshinImpact.Model.Gear.Triggers;
/// <summary>
/// 直接顺序执行的触发器
/// </summary>
public class SequentialGearTrigger : GearBaseTrigger
{
}

View File

@@ -8,6 +8,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Service.GearTask.Model;
namespace BetterGenshinImpact.Service;
@@ -23,7 +24,7 @@ public class GearTaskStorageService
public GearTaskStorageService(ILogger<GearTaskStorageService> logger)
{
_logger = logger;
_taskStoragePath = Path.Combine(Global.Absolute("User"), "task_v2", "list");
_taskStoragePath = GearTaskPaths.TaskListPath;
// 确保目录存在
Directory.CreateDirectory(_taskStoragePath);

View File

@@ -0,0 +1,17 @@
using System.IO;
using BetterGenshinImpact.Core.Config;
namespace BetterGenshinImpact.Service.GearTask.Model;
/// <summary>
/// 存储定义任务的路径信息
/// </summary>
public class GearTaskPaths
{
private static readonly string TaskV2Path = Path.Combine(Global.Absolute("User"), "task_v2");
public static readonly string TaskListPath = Path.Combine(TaskV2Path, "list");
public static readonly string TaskTriggerPath = Path.Combine(TaskV2Path, "trigger");
}

View File

@@ -14,7 +14,6 @@ namespace BetterGenshinImpact.Service;
/// </summary>
public class QuartzSchedulerService(ILogger<QuartzSchedulerService> logger, ISchedulerFactory schedulerFactory) : IHostedService
{
private ILogger<QuartzSchedulerService> _logger = logger;
public async Task StartAsync(CancellationToken cancellationToken)
{
@@ -23,7 +22,7 @@ public class QuartzSchedulerService(ILogger<QuartzSchedulerService> logger, ISch
Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>> jobsDictionary = new();
foreach (var data in allData)
{
if (string.IsNullOrEmpty(data.CronExpression))
if (string.IsNullOrEmpty(data.CronExpression) || !data.IsEnabled)
{
continue;
}
@@ -44,7 +43,8 @@ public class QuartzSchedulerService(ILogger<QuartzSchedulerService> logger, ISch
await scheduler.ScheduleJobs(jobsDictionary, replace: true, cancellationToken);
}
public async Task StopAsync(CancellationToken cancellationToken)
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

View File

@@ -15,9 +15,6 @@ public partial class GearTriggerViewModel : ObservableObject
[ObservableProperty]
private string _name = string.Empty;
[ObservableProperty]
private string _description = string.Empty;
[ObservableProperty]
private bool _isEnabled = true;
@@ -37,15 +34,13 @@ public partial class GearTriggerViewModel : ObservableObject
/// 触发器类型
/// </summary>
[ObservableProperty]
private TriggerType _triggerType = TriggerType.Sequential;
private TriggerType _triggerType = TriggerType.Timed;
/// <summary>
/// 任务引用列表
/// 任务引用名称
/// </summary>
[ObservableProperty]
private string _taskDefinitionName = string.Empty;
// 顺序触发器属性(无额外属性)
// 热键触发器属性
@@ -60,29 +55,13 @@ public partial class GearTriggerViewModel : ObservableObject
{
Name = name;
TriggerType = triggerType;
Description = GetDefaultDescription(triggerType);
}
/// <summary>
/// 获取触发器类型的默认描述
/// </summary>
private string GetDefaultDescription(TriggerType triggerType)
{
return triggerType switch
{
TriggerType.Sequential => "按顺序执行任务",
TriggerType.Timed => "定时执行任务",
TriggerType.Hotkey => "热键触发执行任务",
_ => "未知触发器类型"
};
}
/// <summary>
/// 获取触发器类型显示名称
/// </summary>
public string TriggerTypeDisplayName => TriggerType switch
{
TriggerType.Sequential => "顺序触发",
TriggerType.Timed => "定时触发",
TriggerType.Hotkey => "热键触发",
_ => "未知类型"
@@ -92,24 +71,7 @@ public partial class GearTriggerViewModel : ObservableObject
/// 获取热键显示文本
/// </summary>
public string HotkeyDisplayText => Hotkey?.ToString() ?? "未设置";
// /// <summary>
// /// 获取定时器配置显示文本
// /// </summary>
// public string TimerDisplayText
// {
// get
// {
// if (TriggerType != TriggerType.Timed) return "";
//
// var text = $"间隔: {IntervalMs}ms";
// if (DelayMs > 0) text += $", 延迟: {DelayMs}ms";
// if (MaxExecutions > 0) text += $", 最大次数: {MaxExecutions}";
// if (!IsRepeating) text += ", 单次执行";
//
// return text;
// }
// }
/// <summary>
/// 转换为对应的触发器实例
@@ -118,24 +80,20 @@ public partial class GearTriggerViewModel : ObservableObject
{
GearBaseTrigger trigger = TriggerType switch
{
TriggerType.Sequential => new SequentialGearTrigger(),
TriggerType.Timed => new QuartzCronGearTrigger
{
// IntervalMs = IntervalMs,
// IsRepeating = IsRepeating,
// DelayMs = DelayMs,
// MaxExecutions = MaxExecutions
CronExpression = CronExpression
},
TriggerType.Hotkey => new HotkeyGearTrigger
{
Hotkey = Hotkey,
IsEnabled = IsEnabled
Hotkey = Hotkey
},
_ => new SequentialGearTrigger()
_ => throw new ArgumentOutOfRangeException()
};
trigger.Name = Name;
trigger.TaskDefinitionName = _taskDefinitionName;
trigger.IsEnabled = IsEnabled;
trigger.TaskDefinitionName = TaskDefinitionName;
return trigger;
}
@@ -146,11 +104,6 @@ public partial class GearTriggerViewModel : ObservableObject
/// </summary>
public enum TriggerType
{
/// <summary>
/// 顺序触发
/// </summary>
Sequential,
/// <summary>
/// 定时触发
/// </summary>

View File

@@ -42,14 +42,12 @@ public partial class GearTriggerPageViewModel : ViewModel
// 添加定时触发器示例数据
TimedTriggers.Add(new GearTriggerViewModel("每日签到", TriggerType.Timed)
{
Description = "每天早上8点自动签到",
IsEnabled = true,
TaskDefinitionName = "自动签到任务"
});
TimedTriggers.Add(new GearTriggerViewModel("周常清理", TriggerType.Timed)
{
Description = "每周一清理背包",
IsEnabled = false,
TaskDefinitionName = "背包清理任务"
});
@@ -57,7 +55,6 @@ public partial class GearTriggerPageViewModel : ViewModel
// 添加快捷键触发器示例数据
HotkeyTriggers.Add(new GearTriggerViewModel("快速战斗", TriggerType.Hotkey)
{
Description = "按F1快速开始战斗",
IsEnabled = true,
TaskDefinitionName = "自动战斗任务",
Hotkey = new HotKey { Key = System.Windows.Input.Key.F1 }
@@ -65,7 +62,6 @@ public partial class GearTriggerPageViewModel : ViewModel
HotkeyTriggers.Add(new GearTriggerViewModel("快速采集", TriggerType.Hotkey)
{
Description = "按F2快速采集物品",
IsEnabled = true,
TaskDefinitionName = "自动采集任务",
Hotkey = new HotKey { Key = System.Windows.Input.Key.F2 }