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 _leftAlarmStates = new Dictionary() //加锁 { ["M109"] = false,//伺服报警 ["M111"] = false,//其他报警 ["M110"] = false,//急停报警 ["M120"] = false,//障碍报警 }; private static readonly Dictionary _rightAlarmStates = new Dictionary() { ["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("M109"); //伸缩杆伺服报警//默认是false var other = device.Communicator.Read("M111"); //其他报警 //默认是false var scram = device.Communicator.Read("M110");//急停报警//默认是false var stop = device.Communicator.Read("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); } } } }