wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System.Diagnostics;
using Aging.Infrastructure.Contracts;
using log4net;
 
namespace Aging.Infrastructure.Services
{
    /// <summary>
    /// 设备服务实现。
    /// 协调 ResourceManager、DeviceManager 和 SlotMapping,
    /// 提供面向 DeviceId 的命令执行、查询和安全关机功能。
    /// 所有操作均需通过资源预约验证和租约管理,确保硬件资源的安全访问。
    /// </summary>
    public sealed class DeviceService : IDeviceService
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DeviceService));
 
        /// <summary>
        /// 资源管理器
        /// </summary>
        private readonly IResourceManager _resourceManager;
 
        /// <summary>
        /// 设备管理器
        /// </summary>
        private readonly IDeviceManager _deviceManager;
 
        /// <summary>
        /// Slot-设备-通道映射解析器
        /// </summary>
        private readonly SlotDeviceMappingResolver _slotMappingResolver;
 
        /// <summary>
        /// 初始化设备服务实例
        /// </summary>
        /// <param name="resourceManager">资源管理器</param>
        /// <param name="deviceManager">设备管理器</param>
        /// <param name="slotMappingResolver">Slot 映射解析器</param>
        public DeviceService(
            IResourceManager resourceManager,
            IDeviceManager deviceManager,
            SlotDeviceMappingResolver slotMappingResolver)
        {
            _resourceManager = resourceManager ?? throw new ArgumentNullException(nameof(resourceManager));
            _deviceManager = deviceManager ?? throw new ArgumentNullException(nameof(deviceManager));
            _slotMappingResolver = slotMappingResolver ?? throw new ArgumentNullException(nameof(slotMappingResolver));
        }
 
        /// <inheritdoc/>
        public async Task<DeviceCommandResult> ExecuteCommandAsync(
            string deviceId, string command, ResourceOwnerToken owner, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(deviceId))
                throw new ArgumentException("设备ID不能为空", nameof(deviceId));
            if (string.IsNullOrWhiteSpace(command))
                throw new ArgumentException("命令不能为空", nameof(command));
            if (owner == null)
                throw new ArgumentNullException(nameof(owner));
 
            var stopwatch = Stopwatch.StartNew();
            var resourceKey = new ResourceKey(deviceId, "Generic");
 
            try
            {
                Log.Info($"[DeviceService] 执行命令: Device={deviceId}, Command={command}, Owner={owner.OwnerId}");
 
                // 1. 获取设备驱动
                var driver = _deviceManager.GetDriver(deviceId);
                if (driver == null)
                {
                    stopwatch.Stop();
                    Log.Warn($"[DeviceService] 设备 {deviceId} 驱动不存在");
                    return new DeviceCommandResult
                    {
                        Success = false,
                        ErrorCode = "DRIVER_NOT_FOUND",
                        ErrorMessage = $"设备 {deviceId} 的驱动未注册",
                        LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                    };
                }
 
                // 2. 验证预约
                if (!_resourceManager.ValidateOwner(owner, resourceKey))
                {
                    stopwatch.Stop();
                    Log.Warn($"[DeviceService] 权限验证失败: Device={deviceId}, Owner={owner.OwnerId}");
                    return new DeviceCommandResult
                    {
                        Success = false,
                        ErrorCode = "PERMISSION_DENIED",
                        ErrorMessage = $"所有者 {owner.OwnerId} 未预约设备 {deviceId} 或令牌已过期",
                        LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                    };
                }
 
                // 3. 获取租约并执行命令
                using var lease = await _resourceManager.AcquireOperationAsync(owner, resourceKey, ct).ConfigureAwait(false);
 
                var response = await driver.SendCommandAsync(command, ct).ConfigureAwait(false);
 
                stopwatch.Stop();
                Log.Info($"[DeviceService] 命令执行成功: Device={deviceId}, Command={command}, 延迟={stopwatch.Elapsed.TotalMilliseconds:F2}ms");
 
                return new DeviceCommandResult
                {
                    Success = true,
                    RawResponse = response,
                    Readback = response,
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (ResourceConflictException ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 资源冲突: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "RESOURCE_CONFLICT",
                    ErrorMessage = ex.Message,
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (OperationCanceledException)
            {
                stopwatch.Stop();
                Log.Warn($"[DeviceService] 命令执行被取消: Device={deviceId}");
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "OPERATION_CANCELLED",
                    ErrorMessage = "操作已被取消",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (TimeoutException ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 命令执行超时: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "TIMEOUT",
                    ErrorMessage = $"设备 {deviceId} 命令执行超时: {ex.Message}",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (Exception ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 命令执行异常: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "UNKNOWN_ERROR",
                    ErrorMessage = $"设备 {deviceId} 命令执行失败: {ex.Message}",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
        }
 
        /// <inheritdoc/>
        public async Task<DeviceCommandResult> ExecuteQueryAsync(
            string deviceId, string query, ResourceOwnerToken owner, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(deviceId))
                throw new ArgumentException("设备ID不能为空", nameof(deviceId));
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentException("查询命令不能为空", nameof(query));
            if (owner == null)
                throw new ArgumentNullException(nameof(owner));
 
            var stopwatch = Stopwatch.StartNew();
            var resourceKey = new ResourceKey(deviceId, "Generic");
 
            try
            {
                Log.Info($"[DeviceService] 执行查询: Device={deviceId}, Query={query}, Owner={owner.OwnerId}");
 
                // 1. 获取设备驱动
                var driver = _deviceManager.GetDriver(deviceId);
                if (driver == null)
                {
                    stopwatch.Stop();
                    Log.Warn($"[DeviceService] 设备 {deviceId} 驱动不存在");
                    return new DeviceCommandResult
                    {
                        Success = false,
                        ErrorCode = "DRIVER_NOT_FOUND",
                        ErrorMessage = $"设备 {deviceId} 的驱动未注册",
                        LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                    };
                }
 
                // 2. 验证预约
                if (!_resourceManager.ValidateOwner(owner, resourceKey))
                {
                    stopwatch.Stop();
                    Log.Warn($"[DeviceService] 权限验证失败: Device={deviceId}, Owner={owner.OwnerId}");
                    return new DeviceCommandResult
                    {
                        Success = false,
                        ErrorCode = "PERMISSION_DENIED",
                        ErrorMessage = $"所有者 {owner.OwnerId} 未预约设备 {deviceId} 或令牌已过期",
                        LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                    };
                }
 
                // 3. 获取租约并执行查询
                using var lease = await _resourceManager.AcquireOperationAsync(owner, resourceKey, ct).ConfigureAwait(false);
 
                var response = await driver.QueryAsync(query, ct).ConfigureAwait(false);
 
                stopwatch.Stop();
                Log.Info($"[DeviceService] 查询执行成功: Device={deviceId}, Query={query}, 延迟={stopwatch.Elapsed.TotalMilliseconds:F2}ms");
 
                return new DeviceCommandResult
                {
                    Success = true,
                    RawResponse = response,
                    Readback = response,
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (ResourceConflictException ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 资源冲突: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "RESOURCE_CONFLICT",
                    ErrorMessage = ex.Message,
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (OperationCanceledException)
            {
                stopwatch.Stop();
                Log.Warn($"[DeviceService] 查询执行被取消: Device={deviceId}");
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "OPERATION_CANCELLED",
                    ErrorMessage = "操作已被取消",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (TimeoutException ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 查询执行超时: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "TIMEOUT",
                    ErrorMessage = $"设备 {deviceId} 查询执行超时: {ex.Message}",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
            catch (Exception ex)
            {
                stopwatch.Stop();
                Log.Error($"[DeviceService] 查询执行异常: Device={deviceId}, 错误: {ex.Message}", ex);
                return new DeviceCommandResult
                {
                    Success = false,
                    ErrorCode = "UNKNOWN_ERROR",
                    ErrorMessage = $"设备 {deviceId} 查询执行失败: {ex.Message}",
                    LatencyMs = stopwatch.Elapsed.TotalMilliseconds
                };
            }
        }
 
        /// <inheritdoc/>
        public async Task SafeShutdownAsync(string deviceId, ResourceOwnerToken owner, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(deviceId))
                throw new ArgumentException("设备ID不能为空", nameof(deviceId));
            if (owner == null)
                throw new ArgumentNullException(nameof(owner));
 
            var resourceKey = new ResourceKey(deviceId, "Generic");
 
            try
            {
                Log.Info($"[DeviceService] 开始安全关机: Device={deviceId}, Owner={owner.OwnerId}");
 
                // 1. 获取驱动
                var driver = _deviceManager.GetDriver(deviceId);
                if (driver == null)
                {
                    Log.Warn($"[DeviceService] 安全关机失败: 设备 {deviceId} 驱动不存在");
                    return;
                }
 
                // 2. 验证预约
                if (!_resourceManager.ValidateOwner(owner, resourceKey))
                {
                    Log.Warn($"[DeviceService] 安全关机权限验证失败: Device={deviceId}, Owner={owner.OwnerId}");
                    return;
                }
 
                // 3. 获取租约
                using var lease = await _resourceManager.AcquireOperationAsync(owner, resourceKey, ct).ConfigureAwait(false);
 
                // 4. 发送安全关闭命令
                Log.Info($"[DeviceService] 发送安全关闭命令: Device={deviceId}");
                await driver.SendCommandAsync("OUTP OFF", ct).ConfigureAwait(false);
 
                // 5. 等待确认(短暂延迟确保命令执行完成)
                await Task.Delay(500, ct).ConfigureAwait(false);
 
                // 6. 验证关闭状态
                var statusResponse = await driver.QueryAsync("OUTP?", ct).ConfigureAwait(false);
                Log.Info($"[DeviceService] 设备 {deviceId} 关闭状态确认: {statusResponse}");
 
                Log.Info($"[DeviceService] 安全关机完成: Device={deviceId}");
            }
            catch (OperationCanceledException)
            {
                Log.Warn($"[DeviceService] 安全关机操作被取消: Device={deviceId}");
                throw;
            }
            catch (Exception ex)
            {
                Log.Error($"[DeviceService] 安全关机异常: Device={deviceId}, 错误: {ex.Message}", ex);
                throw;
            }
        }
    }
}