yanjinhui
2025-06-02 4b4cc0dd66d0bfb8220709f6a5edd5fc3b8a65c2
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
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;
        }
 
        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>("M110"); //其他报警 //默认是false
 
                Console.WriteLine($"{device.DeviceName}:卡住报警的默认值为{stuck},其他报警的默认值为{other}");
 
                // 根据设备代码获取位置和部门ID
                var (location, deptId) = device.DeviceCode switch
                {
                    "SSG001" => ("轨道一-左", 1),
                    "SSG002" => ("轨道一-右", 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 && !other)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:伸缩杆卡住报警", stuck);
                    Console.WriteLine($"{location}:伸缩杆卡住报警", stuck);
                }
                else if (!stuck && other)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:其他报警", other);
                    Console.WriteLine($"{location}:其他报警", other);
                }
                else if (stuck && other)
                {
                    var alarm = _alarmResetHsyServer.AddAlarmHsy(deptId, $"{location}:伸缩杆卡住报警和其他报警", other);
                    Console.WriteLine($"{location}:伸缩杆卡住报警和其他报警", other);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设备 {device.DeviceCode} 处理异常:" + ex.Message);
            }
        }
    }
}