HuBingJie
9 天以前 21078521d79f0eb5535cc34be7917a3fd1123b8f
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using Autofac.Core;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm.Http;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_DTO.Enum;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_TaskInfoService
{
    public class RgvoperainformService : ServiceBase<Dt_Task_hty, ITask_HtyRepository>, IRgvoperainformService
    {
        private readonly IAgvStationService _gvStationService;
        private readonly IServiceProvider _serviceProvider;
        private static bool _isMonitoring = false;
        //private static CancellationTokenSource _cancellationTokenSource;
        
        // 在类级别添加独立的监控状态
        private bool _isOutboundMonitoring = false;
        private bool _isInboundMonitoring = false;
        private bool _isSafetyDoorMonitoring = false;
 
        private CancellationTokenSource _outboundCancellationTokenSource;
        private CancellationTokenSource _inboundCancellationTokenSource;
        private CancellationTokenSource _safetyDoorCancellationTokenSource;
        public RgvoperainformService(ITask_HtyRepository BaseDal, IAgvStationService agvStationService, IServiceProvider serviceProvider) : base(BaseDal)
        {
            _gvStationService = agvStationService;
            _serviceProvider = serviceProvider;
        }
 
 
        //=================================================读取设备信息==========================================================
 
 
 
 
 
        /// <summary>
        /// 获取设备状态(支持出库、入库和安全门独立监控)
        /// </summary>
        /// <param name="off">0:停止 1:启动 2:仅查询</param>
        /// <param name="monitorType">监控类型:outbound-出库 inbound-入库 safetydoor-安全门 all-全部</param>
        /// <returns></returns>
        public WebResponseContent GetDeviceStatusDto(int off, string monitorType = "outbound")
        {
            bool isOutbound = monitorType == "outbound" || monitorType == "all";//出库
            bool isInbound = monitorType == "inbound" || monitorType == "all";//入库
            bool isSafetyDoor = monitorType == "safetydoor" || monitorType == "all";//安全门
 
            if (off == 1)
            {
                // 启动监控
                if (isOutbound && !_isOutboundMonitoring)
                {
                    _isOutboundMonitoring = true;
                    _outboundCancellationTokenSource = new CancellationTokenSource();
                    _ = Task.Run(async () => await StartDeviceMonitoring(_outboundCancellationTokenSource.Token, "outbound"));
                }
 
                if (isInbound && !_isInboundMonitoring)
                {
                    _isInboundMonitoring = true;
                    _inboundCancellationTokenSource = new CancellationTokenSource();
                    _ = Task.Run(async () => await StartDeviceMonitoring(_inboundCancellationTokenSource.Token, "inbound"));
                }
 
                if (isSafetyDoor && !_isSafetyDoorMonitoring)
                {
                    _isSafetyDoorMonitoring = true;
                    _safetyDoorCancellationTokenSource = new CancellationTokenSource();
                    _ = Task.Run(async () => await StartDeviceMonitoring(_safetyDoorCancellationTokenSource.Token, "safetydoor"));
                }
 
                var immediateData = GetImmediateDeviceData(monitorType);
                string message = GetMonitorStatusMessage(monitorType);
                return WebResponseContent.Instance.OK($"设备状态监控已启动 - {message}", immediateData);
            }
            else if (off == 0)
            {
                // 停止监控
                if (isOutbound && _isOutboundMonitoring)
                {
                    _isOutboundMonitoring = false;
                    _outboundCancellationTokenSource?.Cancel();
                }
 
                if (isInbound && _isInboundMonitoring)
                {
                    _isInboundMonitoring = false;
                    _inboundCancellationTokenSource?.Cancel();
                }
 
                if (isSafetyDoor && _isSafetyDoorMonitoring)
                {
                    _isSafetyDoorMonitoring = false;
                    _safetyDoorCancellationTokenSource?.Cancel();
                }
 
                var finalData = GetImmediateDeviceData(monitorType);
                string message = GetMonitorStatusMessage(monitorType);
                return WebResponseContent.Instance.OK($"设备状态监控已停止 - {message}", finalData);
            }
            else
            {
                // 返回当前数据
                var currentData = GetImmediateDeviceData(monitorType);
                string message = GetMonitorStatusMessage(monitorType);
                return WebResponseContent.Instance.OK($"设备状态监控当前状态 - {message}", currentData);
            }
        }
 
        /// <summary>
        /// 启动设备监控
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <param name="monitorType">监控类型</param>
        /// <returns></returns>
        private async Task StartDeviceMonitoring(CancellationToken cancellationToken, string monitorType)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var hubContext = scope.ServiceProvider.GetRequiredService<IHubContext<DeviceStatusHub>>();
 
                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        // 获取指定类型的设备数据
                        var deviceData = GetImmediateDeviceData(monitorType);
 
                        // 推送数据给所有连接的客户端,带上监控类型标识
                        await hubContext.Clients.All.SendAsync("ReceiveDeviceStatusUpdate",
                            new { MonitorType = monitorType, Data = deviceData }, cancellationToken);
 
                        Console.WriteLine($"✅ {GetMonitorTypeDisplayName(monitorType)}设备数据推送完成,时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
 
                        // 等待30秒
                        await Task.Delay(30000, cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        // 监控被取消,正常退出
                        Console.WriteLine($"{GetMonitorTypeDisplayName(monitorType)}设备监控已停止");
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"❌ {GetMonitorTypeDisplayName(monitorType)}设备监控异常: {ex.Message}");
                        await Task.Delay(5000, cancellationToken);
                    }
                }
            }
 
            // 更新监控状态
            switch (monitorType)
            {
                case "outbound":
                    _isOutboundMonitoring = false;
                    break;
                case "inbound":
                    _isInboundMonitoring = false;
                    break;
                case "safetydoor":
                    _isSafetyDoorMonitoring = false;
                    break;
            }
        }
 
        /// <summary>
        /// 获取设备数据
        /// </summary>
        /// <param name="monitorType">监控类型</param>
        /// <returns></returns>
        private Dictionary<string, Dictionary<string, int>> GetImmediateDeviceData(string monitorType = "all")
        {
            // 定义设备分组
            var deviceGroups = new Dictionary<string, List<string>>
            {
                // 出库设备
                ["MotherCar"] = new List<string> { "RGV112", "RGV110", "RGV114", "RGV115" },
                ["TransferCar"] = new List<string> { "RGV116", "RGV111" },
                ["MaterialOut"] = new List<string> { "RGV118" },
 
                // 入库设备
                ["InboundMotherCar"] = new List<string> { "RGV103", "RGV105", "RGV108", "RGV109" },
                ["InboundTransferCar"] = new List<string> { "RGV104", "RGV107" },
                ["MaterialIn"] = new List<string> { "RGV101" },
 
                // 安全门设备
                ["SafetyDoor"] = new List<string> { "HCJ2000" }, // 安全门
            };
 
            // 设备类型对应的参数配置
            var paramConfigs = new Dictionary<string, List<(string DeviceProParamName, string DeviceProParamType, string DeviceProRemark)>>
            {
                // 出库设备参数配置
                ["TransferCar"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGV_Risingsignalplace", "ReadDeviceCommand", "上升信号到位"),
            ("RGV_Descentsignal", "ReadDeviceCommand", "下降信号到位"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
                ["MotherCar"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
                ["MaterialOut"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGV_Risingsignalplace", "ReadDeviceCommand", "上升信号到位"),
            ("RGV_Descentsignal", "ReadDeviceCommand", "下降信号到位"),
            ("RGV_Telescopicfork", "ReadDeviceCommand", "伸缩叉伸出信号"),
            ("RGV_Retractionsignal", "ReadDeviceCommand", "伸缩叉缩回信号"),
            ("RGV_Telescopicfork", "DeviceCommand", "伸出操作"),
            ("RGVForkCurrentPosition", "RGVForkCurrentPosition", "当前位置(货叉)"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
 
                // 入库设备参数配置
                ["MaterialIn"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGV_Risingsignalplace", "ReadDeviceCommand", "上升信号到位"),
            ("RGV_Descentsignal", "ReadDeviceCommand", "下降信号到位"),
            ("RGV_Telescopicfork", "ReadDeviceCommand", "伸缩叉伸出信号"),
            ("RGV_Retractionsignal", "ReadDeviceCommand", "伸缩叉缩回信号"),
            ("RGV_Telescopicfork", "DeviceCommand", "伸出操作"),
            ("RGVForkCurrentPosition", "RGVForkCurrentPosition", "当前位置(货叉)"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
                ["InboundMotherCar"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
                ["InboundTransferCar"] = new List<(string, string, string)>
        {
            ("RgvCraneAutoStatus", "RgvCraneAutoStatus", "工作模式"),
            ("RGVCurrentlocation", "RGVCurrentlocation", "当前位置"),
            ("RgvEquipmentStatus", "RgvEquipmentStatus", "有货状态"),
            ("RGV_Targetaddress", "ReadDeviceCommand", "目标地址"),
            ("RGV_Rgvtaskstutas", "RGV_Rgvtaskstutas", "任务状态"),
            ("RGV_Rgvtaskid", "ReadDeviceCommand", "RGV任务编号"),
            ("RGV_Faultcode", "ReadDeviceCommand", "故障代码"),
            ("RGV_Risingsignalplace", "ReadDeviceCommand", "上升信号到位"),
            ("RGV_Descentsignal", "ReadDeviceCommand", "下降信号到位"),
            ("RGVInitialize", "RGVInitialize", "初始化未完成标志位")
        },
 
                // 安全门设备参数配置
                ["SafetyDoor"] = new List<(string, string, string)>
        {
            ("IndicatorStatus", "ReadDeviceCommand", "安全门指示灯状态"),
            ("DoorRequest", "ReadDeviceCommand", "安全门请求开门"),
            ("PowerOffStatus", "ReadDeviceCommand", "安全门断电状态"),
            ("EmergencyStopStatus", "ReadDeviceCommand", "安全门急停状态"),
            ("SafetyLockStatus", "ReadDeviceCommand", "安全门锁状态"),
            ("ResetStatus", "ReadDeviceCommand", "安全门复位状态"),
            ("AlarmSummary", "DeviceCommand", "报警信息"),
            ("OpenDoor", "DeviceCommand", "开门信息")
        }
            };
 
            // 创建最终结果字典
            var finalResult = new Dictionary<string, Dictionary<string, int>>();
 
            // 根据监控类型筛选设备组
            var filteredDeviceGroups = deviceGroups.Where(group =>
            {
                return monitorType switch
                {
                    "outbound" => group.Key == "MotherCar" || group.Key == "TransferCar" || group.Key == "MaterialOut",
                    "inbound" => group.Key == "MaterialIn" || group.Key == "InboundMotherCar" || group.Key == "InboundTransferCar",
                    "safetydoor" => group.Key == "SafetyDoor", // 安全门监控类型
                    "all" => true,
                    _ => group.Key == "MotherCar" || group.Key == "TransferCar" || group.Key == "MaterialOut"
                };
            }).ToDictionary(x => x.Key, x => x.Value);
 
            int i = 0;
            // 遍历筛选后的设备组
            foreach (var deviceGroup in filteredDeviceGroups)
            {
                string deviceType = deviceGroup.Key;
                List<string> deviceCodes = deviceGroup.Value;
 
                // 获取该设备类型的参数配置
                if (!paramConfigs.ContainsKey(deviceType))
                    continue;
 
                var deviceParams = paramConfigs[deviceType];
 
                // 遍历该类型下的所有设备
                foreach (string deviceChildCode in deviceCodes)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == deviceChildCode);
 
                    // 处理不同类型的设备
                    if (device is SpeStackerCrane commonStacker)
                    {
                        // 为当前设备创建一个字典,用于保存 DeviceProRemark 和对应的值
                        var deviceData = new Dictionary<string, int>();
 
                        // 遍历该设备的所有参数
                        foreach (var param in deviceParams)
                        {
                            try
                            {
                                DeviceProDTO? rgvResetOperation = RgvOperationService.GetRGVDeviceProDTO(
                                    commonStacker, deviceChildCode, param.DeviceProParamName, param.DeviceProParamType);
 
                                if (rgvResetOperation != null)
                                {
                                    int res = RgvOperationService.GetLine(commonStacker, rgvResetOperation.DeviceProAddress);
 
                                    // 确保每个 DeviceProRemark 都对应正确的查询结果
                                    deviceData[param.DeviceProRemark] = res;
                                }
                                else
                                {
                                    deviceData[param.DeviceProRemark] = -1; // 使用 -1 表示查询失败
                                }
                            }
                            catch (Exception)
                            {
                                deviceData[param.DeviceProRemark] = -1; // 使用 -1 表示查询异常
                            }
                        }
 
                        // 将当前设备的数据添加到最终结果中
                        finalResult[deviceChildCode] = deviceData;
                    }
                    else if (device is CommonConveyorLine conveyorLine)
                    {
                        // 处理安全门设备(CommonConveyorLine 类型)
                        // 创建三个安全门设备的数据
                        List<string> safetyDoorList = new List<string> { "AQM001", "AQM002", "AQM003" };
 
                        // 为每个安全门创建数据
                        foreach (string safetyDoorCode in safetyDoorList)
                        {
                            var deviceData = new Dictionary<string, int>();
 
                            // 遍历该设备的所有参数
                            foreach (var param in deviceParams)
                            {
                                try
                                {
                                    // 使用安全门名称查询对应的参数
                                    DeviceProDTO? devicePro = GetSafetyDoorDeviceProDTO(
                                        conveyorLine, safetyDoorCode, param.DeviceProParamName, param.DeviceProParamType);
 
                                    if (devicePro != null)
                                    {
                                        // 对于 CommonConveyorLine 类型,使用通用的数据读取方法
                                        int res = ReadCommonConveyorLineData(conveyorLine, devicePro.DeviceProAddress);
                                        deviceData[param.DeviceProRemark] = res;
                                    }
                                    else
                                    {
                                        deviceData[param.DeviceProRemark] = -1; // 使用 -1 表示查询失败
                                    }
                                }
                                catch (Exception)
                                {
                                    deviceData[param.DeviceProRemark] = -1; // 使用 -1 表示查询异常
                                }
                            }
 
                            // 将当前安全门设备的数据添加到最终结果中,使用安全门名称作为key
                            finalResult[safetyDoorCode] = deviceData;
                        }
                    }
                    else
                    {
                        // 即使设备不存在或不是支持的类型,也创建一个空字典
                        finalResult[deviceChildCode] = new Dictionary<string, int>();
                        if (device != null)
                        {
                            Console.WriteLine($"警告:设备 {deviceChildCode} 类型 {device.GetType().Name} 不支持,无法读取数据");
                        }
                        else
                        {
                            Console.WriteLine($"警告:设备 {deviceChildCode} 不存在");
                        }
                    }
                }
            }
 
            return finalResult;
        }
 
        /// <summary>
        /// 获取监控状态消息
        /// </summary>
        /// <param name="monitorType"></param>
        /// <returns></returns>
        private string GetMonitorStatusMessage(string monitorType)
        {
            return monitorType switch
            {
                "outbound" => $"出库监控: {(_isOutboundMonitoring ? "运行中" : "已停止")}",
                "inbound" => $"入库监控: {(_isInboundMonitoring ? "运行中" : "已停止")}",
                "safetydoor" => $"安全门监控: {(_isSafetyDoorMonitoring ? "运行中" : "已停止")}",
                "all" => $"出库监控: {(_isOutboundMonitoring ? "运行中" : "已停止")}, 入库监控: {(_isInboundMonitoring ? "运行中" : "已停止")}, 安全门监控: {(_isSafetyDoorMonitoring ? "运行中" : "已停止")}",
                _ => $"出库监控: {(_isOutboundMonitoring ? "运行中" : "已停止")}"
            };
        }
 
        /// <summary>
        /// 获取监控类型显示名称
        /// </summary>
        /// <param name="monitorType"></param>
        /// <returns></returns>
        private string GetMonitorTypeDisplayName(string monitorType)
        {
            return monitorType switch
            {
                "outbound" => "出库",
                "inbound" => "入库",
                "safetydoor" => "安全门",
                "all" => "全部",
                _ => "出库"
            };
        }
 
 
        /// <summary>
        /// 读取设备信息
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="DeviceProDataBlock"></param>
        /// <returns></returns>
        public static int ReadCommonConveyorLineData(CommonConveyorLine Commonstacker, string DeviceProDataBlock)
        {
            return Commonstacker.Communicator.Read<short>(DeviceProDataBlock);
        }
 
 
        /// <summary>
        /// 查询安全门设备
        /// </summary>
        /// <param name="Commonstacker"></param>
        /// <param name="SCAddress"></param>
        /// <param name="Interactivet"></param>
        /// <returns></returns>
        public static DeviceProDTO? GetSafetyDoorDeviceProDTO(CommonConveyorLine Commonstacker, string SCAddress, string DeviceProParamName, string DeviceProParamType)
        {
            return Commonstacker.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SCAddress && x.DeviceProParamName == DeviceProParamName && x.DeviceProParamType == DeviceProParamType);
        }
 
        //=================================================读取设备信息==========================================================
 
 
        #region 设备信息
        /***
        /// <summary>
        /// 查询RGV设备
        /// </summary>
        /// <param name="Commonstacker"></param>
        /// <param name="SCAddress"></param>
        /// <param name="Interactivet"></param>
        /// <returns></returns>
        public static DeviceProDTO? GetRGVDeviceProDTO(SpeStackerCrane Commonstacker, string SCAddress, string DeviceProParamName, string DeviceProParamType)
        {
            return Commonstacker.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SCAddress && x.DeviceProParamName == DeviceProParamName && x.DeviceProParamType == DeviceProParamType);
        }
        /// <summary>
        /// 读取设备信息
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="DeviceProDataBlock"></param>
        /// <returns></returns>
        public static int GetLine(SpeStackerCrane Commonstacker, string DeviceProDataBlock)
        {
            return Commonstacker.Communicator.Read<short>(DeviceProDataBlock);
        }
 
        /// <summary>
        /// 写入设备信息
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="DeviceProDataBlock"></param>
        /// <returns></returns>
        public static bool RgvSetLine(SpeStackerCrane Commonstacker, string DeviceProDataBlock, short rgvvalues)
        {
            return Commonstacker.Communicator.Write<short>(DeviceProDataBlock, rgvvalues);
        }
        */
        #endregion
 
 
        /// <summary>
        /// 设备操作
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="operationType"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent DeviceOperation(string deviceName, string operationType, int parameter)
        {
            WebResponseContent webResponse = new WebResponseContent();
            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == deviceName);
            if (device == null) return webResponse.Error("未找到此设备");
            SpeStackerCrane Commonstacker = (SpeStackerCrane)device;
            string DeviceProParamName = null;
            string DeviceProParamType = null;
            int zhi = 1002011;
            try
            {
 
            switch (operationType)
            {
                case "cs":          //初始化
                                    //获取实例
                    DeviceProDTO? RGV_RGVtasktypetcs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_RGVtasktypet", "DeviceCommand");        //任务类型                                                                                                                                                   //第一步写入任务
                    RgvOperationService.RgvSetLine(Commonstacker, RGV_RGVtasktypetcs.DeviceProAddress, (short)3);
                        //写入自动
                    DeviceProDTO? RGV_DWorkingmodecs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_DWorkingmode", "DeviceCommand");        //任务id
                    RgvOperationService.RgvSetLine(Commonstacker, RGV_DWorkingmodecs.DeviceProAddress, (short)1);
                        //写入清除任务
                    RgvOperationService.RgvSetLine(Commonstacker, RGV_RGVtasktypetcs.DeviceProAddress, (short)4);
 
                        //反馈确认任务
                    DeviceProDTO? RGV_taskcompletecs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_taskcomplete", "RGV_taskcomplete");        //任务id
                    RgvOperationService.RgvSetLine(Commonstacker, RGV_taskcompletecs.DeviceProAddress, (short)1);
                    DeviceProParamName = "RGV_Resetoperation";
                    DeviceProParamType = "DeviceCommand";
                    zhi = 1;
                        break;
                case "sd":             //手动
                    DeviceProParamName = "RGV_DWorkingmode";
                    DeviceProParamType = "DeviceCommand";
                    zhi = 0;
                    break;
                case "zd":              //自动
                    DeviceProParamName = "RGV_DWorkingmode";
                    DeviceProParamType = "DeviceCommand";
                    zhi = 1;
                    break;
                case "ss":              //上升
                    DeviceProParamName = "RGV_Risingsignalplace";
                    DeviceProParamType = "DeviceCommand";
                    zhi = parameter;
                    break;
                case "xj":          //下降
                    DeviceProParamName = "RGV_Descentsignal";
                    DeviceProParamType = "DeviceCommand";
                    zhi = parameter;
                    break;
                case "dz":          //地址
                    DeviceProParamName = "RGV_RGVTasklocationt";
                    DeviceProParamType = "DeviceCommand";
                    zhi = parameter;
                        //获取实例
                        DeviceProDTO? RGV_RGVtasktypet = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_RGVtasktypet", "DeviceCommand");        //任务类型
                                                                                                                                                                        //第一步写入任务
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_RGVtasktypet.DeviceProAddress, (short)3);
 
                        //写入自动
                        DeviceProDTO? RGV_DWorkingmode = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_Rgvtaskidt", "DeviceCommand");        //任务id
                                                                                                                                                                      //任务id
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_DWorkingmode.DeviceProAddress, (short)30001);
 
                        //任务id
                        DeviceProDTO? RGV_taskcomplete = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, "RGV_Lanjiantaskidt", "DeviceCommand");        //任务id
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_taskcomplete.DeviceProAddress, (short)30001);
                        break;
                case "sc":      //伸出
                        //DeviceProParamName = "RGV_DWorkingmode";
                        //DeviceProParamType = "DeviceCommand";
                        //zhi = 1;
                        break;
                case "sh":      //缩回
                    //DeviceProParamName = "RGV_Retractionsignal";
                    //DeviceProParamType = "DeviceCommand";
                    //zhi = 1;
                    break;
                case "kld":      //开雷达
                    DeviceProParamName = "RGV_Shieldradar";
                    DeviceProParamType = "DeviceCommand";
                    zhi = 1;
                    break;
                case "gld":      //关雷达
                    DeviceProParamName = "RGV_Shieldradar";
                    DeviceProParamType = "DeviceCommand";
                    zhi = 0;
                    break;
                case "fw":
                        DeviceProParamName = "RGV_Resetoperation";
                        DeviceProParamType = "DeviceCommand";
                        zhi = 1;
                        break;
                }
 
                //获取需要复位的设备
                DeviceProDTO? RGV_Resetoperation = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, deviceName, DeviceProParamName, DeviceProParamType);        //复位报警信息
            RgvOperationService.RgvSetLine(Commonstacker, RGV_Resetoperation.DeviceProAddress,(short)zhi);
            return webResponse.OK("设备操作成功");
            }catch(Exception)
            {
                return webResponse.Error("设备操作失败");
            }
 
        }
 
 
        /// <summary>
        /// 一键操作
        /// </summary>
        /// <param name="monitorType"></param>
        /// <param name="operationType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent OneClickOperation(string monitorType, string operationType)
        {
            WebResponseContent webResponse = new WebResponseContent();
            List<string> Devices = new List<string>();
            if (monitorType == "Inbound")
            {
                //入库设备
                Devices = new List<string> { "RGV103", "RGV105", "RGV108", "RGV109", "RGV104", "RGV107", "RGV101" };
            }
            else if (monitorType == "Outbound")
            {
                //出库设备
                Devices = new List<string> { "RGV112", "RGV110", "RGV114", "RGV115", "RGV116", "RGV111", "RGV118" };
            }
            try
            {
            List<IDevice?> devices = Storage.Devices.Where(x => Devices.Contains(x.DeviceCode)).Cast<IDevice?>().ToList();
            if (devices == null || !devices.Any())
            {
                return webResponse.Error("未找到相关设备");
            }
            string DeviceProParamName = null;
            string DeviceProParamType = null;
            int zhi = 1002011;
            switch (operationType)
            {
                case "init":    //初始化
                        DeviceProParamName = "RGV_Resetoperation";
                        DeviceProParamType = "DeviceCommand";
                        zhi = 1;
                        break;
                case "reset":
                        DeviceProParamName = "RGV_Resetoperation";
                        DeviceProParamType = "DeviceCommand";
                        zhi = 1;
                        break;
                case "start":
                        DeviceProParamName = "RGV_DWorkingmode";
                        DeviceProParamType = "DeviceCommand";
                        zhi = 1;
                        break;
                case "stop":
                        DeviceProParamName = "RGV_DWorkingmode";
                        DeviceProParamType = "DeviceCommand";
                        zhi = 0;
                        break;
            }
            foreach(var device in devices)
            {
                    SpeStackerCrane Commonstacker = (SpeStackerCrane)device;
 
                    if (operationType == "init")
                    {
                        //获取实例
                        DeviceProDTO? RGV_RGVtasktypetcs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, device.DeviceCode, "RGV_RGVtasktypet", "DeviceCommand");        //任务类型                                                                                                                                                   //第一步写入任务
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_RGVtasktypetcs.DeviceProAddress, (short)3);
                        //写入自动
                        DeviceProDTO? RGV_DWorkingmodecs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, device.DeviceCode, "RGV_DWorkingmode", "DeviceCommand");        //任务id
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_DWorkingmodecs.DeviceProAddress, (short)1);
                        //写入清除任务
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_RGVtasktypetcs.DeviceProAddress, (short)4);
 
                        //反馈确认任务
                        DeviceProDTO? RGV_taskcompletecs = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, device.DeviceCode, "RGV_taskcomplete", "RGV_taskcomplete");        //任务id
                        RgvOperationService.RgvSetLine(Commonstacker, RGV_taskcompletecs.DeviceProAddress, (short)1);
                    }
                DeviceProDTO? RGV_Resetoperation = RgvOperationService.GetRGVDeviceProDTO(Commonstacker, device.DeviceCode, DeviceProParamName, DeviceProParamType);        //复位报警信息
                RgvOperationService.RgvSetLine(Commonstacker, RGV_Resetoperation.DeviceProAddress, (short)zhi);
            }
            return webResponse.OK("设备操作成功");
        }catch(Exception)
            {
                return webResponse.Error("设备操作失败");
            }
}
    }
}