FatigueTestConfigService.cs
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Rcs.Application.Services;
using Rcs.Domain.Entities;
using StackExchange.Redis;
namespace Rcs.Infrastructure.Services;
/// <summary>
/// 疲劳测试配置服务实现(基于Redis存储)
/// @author zzy
/// </summary>
public class FatigueTestConfigService : IFatigueTestConfigService
{
private readonly IConnectionMultiplexer _redis;
private readonly ILogger<FatigueTestConfigService> _logger;
private const string ConfigKey = "hahrcs:fatigue_test:config";
private const string RunningStatusKey = "hahrcs:fatigue_test:running";
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public FatigueTestConfigService(
IConnectionMultiplexer redis,
ILogger<FatigueTestConfigService> logger)
{
_redis = redis;
_logger = logger;
}
/// <summary>
/// 获取疲劳测试配置
/// </summary>
public async Task<FatigueTestConfig?> GetConfigAsync(CancellationToken cancellationToken = default)
{
try
{
var db = _redis.GetDatabase();
var json = await db.StringGetAsync(ConfigKey);
if (json.IsNullOrEmpty)
{
return null;
}
return JsonSerializer.Deserialize<FatigueTestConfig>(json!, JsonOptions);
}
catch (Exception ex)
{
_logger.LogError(ex, "[FatigueTestConfig] 获取配置失败");
return null;
}
}
/// <summary>
/// 保存疲劳测试配置
/// </summary>
public async Task<bool> SaveConfigAsync(FatigueTestConfig config, CancellationToken cancellationToken = default)
{
try
{
var db = _redis.GetDatabase();
config.UpdatedAt = DateTime.Now;
var json = JsonSerializer.Serialize(config, JsonOptions);
await db.StringSetAsync(ConfigKey, json);
_logger.LogInformation("[FatigueTestConfig] 配置已保存到Redis");
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "[FatigueTestConfig] 保存配置失败");
return false;
}
}
/// <summary>
/// 获取运行状态
/// </summary>
public async Task<bool> GetRunningStatusAsync(CancellationToken cancellationToken = default)
{
try
{
var db = _redis.GetDatabase();
var value = await db.StringGetAsync(RunningStatusKey);
if (value.IsNullOrEmpty)
{
return false;
}
return bool.TryParse(value, out var result) && result;
}
catch (Exception ex)
{
_logger.LogError(ex, "[FatigueTestConfig] 获取运行状态失败");
return false;
}
}
/// <summary>
/// 设置运行状态
/// </summary>
public async Task<bool> SetRunningStatusAsync(bool isRunning, CancellationToken cancellationToken = default)
{
try
{
var db = _redis.GetDatabase();
await db.StringSetAsync(RunningStatusKey, isRunning.ToString());
_logger.LogInformation("[FatigueTestConfig] 运行状态已设置为: {IsRunning}", isRunning);
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "[FatigueTestConfig] 设置运行状态失败");
return false;
}
}
/// <summary>
/// 启动疲劳测试
/// </summary>
public async Task<bool> StartAsync(CancellationToken cancellationToken = default)
{
var config = await GetConfigAsync(cancellationToken);
if (config == null)
{
_logger.LogWarning("[FatigueTestConfig] 启动失败:配置不存在");
return false;
}
config.IsRunning = true;
config.UpdatedAt = DateTime.Now;
await SaveConfigAsync(config, cancellationToken);
await SetRunningStatusAsync(true, cancellationToken);
_logger.LogInformation("[FatigueTestConfig] 疲劳测试已启动");
return true;
}
/// <summary>
/// 停止疲劳测试
/// </summary>
public async Task<bool> StopAsync(CancellationToken cancellationToken = default)
{
var config = await GetConfigAsync(cancellationToken);
if (config != null)
{
config.IsRunning = false;
config.UpdatedAt = DateTime.Now;
await SaveConfigAsync(config, cancellationToken);
}
await SetRunningStatusAsync(false, cancellationToken);
_logger.LogInformation("[FatigueTestConfig] 疲劳测试已停止");
return true;
}
}