yanjinhui
7 天以前 b9c76ce85e533250cd36de670146530f970859e7
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
using HslCommunication.WebSocket;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_Core;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_TelescopicService;
using WIDESEAWCS_ISystemServices;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class AlarmResetJob : JobBase, IJob
    {
 
        private readonly IAlarmResetHsyServer _alarmResetHsyServer;
        private readonly ISys_UserService _UserService;
        WebSocketServer _webSocketServer;
 
 
 
        public AlarmResetJob(WebSocketServer webSocketServer, IAlarmResetHsyServer alarmResetHsyServer, ISys_UserService UserService)
        {
            _webSocketServer = webSocketServer;
            _alarmResetHsyServer = alarmResetHsyServer;
            _UserService = UserService;
        }
 
        // 增添的字典跟踪报警为每个装置 Quartz 默认每次调度任务时会重新创建
        private static readonly Dictionary<string, bool> _leftAlarmStates = new Dictionary<string, bool>() //加锁
        {
            ["M109"] = false,//伺服报警
            ["M111"] = false,//其他报警
            ["M110"] = false,//急停报警
            ["M120"] = false,//障碍报警
        };
        private static readonly Dictionary<string, bool> _rightAlarmStates = new Dictionary<string, bool>()
        {
            ["M109"] = false,//伺服报警
            ["M111"] = false,//其他报警
            ["M110"] = false,//急停报警
            ["M120"] = false,//障碍报警
        };
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
 
                //获取设备号
                // 获取全部设备配置
                //OtherDevice serialPortDevice = (OtherDevice)context.JobDetail.JobDataMap.Get("JobParams");
 
                //if (serialPortDevice != null)
                //{
                //    HandleAlarm(serialPortDevice);
                //}
 
                //只读
                var alarmInfo = _alarmResetHsyServer.GetWebSocketInfo();
                _webSocketServer.PublishAllClientPayload(JsonConvert.SerializeObject(alarmInfo));
 
            }
            catch (Exception ex)
            {
 
                Console.WriteLine("错误信息:" + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return Task.CompletedTask;
        }
        private void HandleAlarm(OtherDevice device)
        {
            try
            {
                var stuck = device.Communicator.Read<bool>("M109"); //伸缩杆伺服报警//默认是false
                var other = device.Communicator.Read<bool>("M111"); //其他报警 //默认是false
                var scram = device.Communicator.Read<bool>("M110");//急停报警//默认是false
                var stop = device.Communicator.Read<bool>("M120");//遇到障碍停止报警
 
                Console.WriteLine($"{device.DeviceName}:卡住报警的默认值为{stuck},其他报警的默认值为{other}");
 
                // 根据设备代码获取位置和部门ID
                var (location, deptId) = device.DeviceCode switch
                {
                    "SSG001" => ("检8道左侧警惕机构", 1),
                    "SSG002" => ("检8道右侧警惕机构", 1),
                    "SSG003" => ("轨道二-左", 2),
                    "SSG004" => ("轨道二-右", 2),
                    "SSG005" => ("轨道三-左", 3),
                    "SSG006" => ("轨道三-右", 3),
                    "SSG007" => ("轨道四-左", 4),
                    "SSG008" => ("轨道四-右", 4),
                    "SSG009" => ("轨道五-左", 5),
                    "SSG0010" => ("轨道五-右",5),
                    _ => ($"未知设备({device.DeviceCode})", 0) // 未知设备默认部门ID为0
                };
 
                // 如果部门ID为0(未知设备),可以选择记录日志或处理错误
                if (deptId == 0)
                {
                    Console.WriteLine($"未知设备代码: {device.DeviceCode}");
                    return;
                }
 
                if (stuck && !_leftAlarmStates["M109"])
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:卡住报警", stuck);
                    Console.WriteLine($"{location}:卡住报警", stuck);
                }
               
                if (other)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:其他报警", other);
                    Console.WriteLine($"{location}:其他报警", other);
                }
              
                 if (scram)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:急停报警", scram);
                }
                if (stop)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, "{location}:遇到障碍报警", stop);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设备 {device.DeviceCode} 处理异常:" + ex.Message);
            }
        }
    }
}