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("M109"); //伸缩杆报警//默认是false var other = device.Communicator.Read("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); } } } }