using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using HBLConsole.Service; using HBLConsole.Model; using System.Collections.ObjectModel; using HBLConsole.GVL; namespace HBLConsole.Business { public class AlarmHelper { private volatile static AlarmHelper _Instance; public static AlarmHelper GetInstance => _Instance ?? (_Instance = new AlarmHelper()); private AlarmHelper() { } public ObservableCollection Alarms { get; set; } = new ObservableCollection(); public ObservableCollection HistoryAlarms { get; set; } = new ObservableCollection(); ConcurrentDictionary flagbit = new ConcurrentDictionary(); public Action AddAction { get; set; } public Action RemoveAction { get; set; } public Action ChangeAction { get; set; } /// /// 沿报警检测 /// /// 触发变量 /// 报警信息 /// 触发类型,上升沿 或 下降沿 public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising) { if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false); if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger) { if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text]) { AddAlarm(Trigger, text, alarmLevel); flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false; } } else { RemoveAlarm(text); } if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger; } /// /// 添加报警信息 /// /// 报警信息 private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel) { Alarm tempAlarm = new Alarm() { NumId = Alarms.Count, Date = DateTime.Now.ToString("yyyy/MM/dd"), Grade = alarmLevel.ToString(), Info = AlarmInfo, Value = value.ToString(), Time = DateTime.Now.ToString("HH:mm:ss"), }; //tempAlarm.Id = IotReport.GetInstance.HttpAddAlarm(new BPA.Message.API请求.AlarmTable //{ // AlarmTime=DateTime.Now, // AlarmType= tempAlarm.Grade, // AlarmMessage= tempAlarm.Info, // AlarmVla= tempAlarm.Value, // ClientId= InternetInfo.ClientId.ToString() //}); Sqlite.GetInstance.Base.Add(tempAlarm); if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null) { Alarms.Add(tempAlarm); if (AddAction != null) AddAction(AlarmInfo);//添加报警通知 if (ChangeAction != null) ChangeAction();//更改报警通知 } } /// /// 移除报警信息 /// /// 报警信息 private void RemoveAlarm(string AlarmInfo) { var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo); if (result != null) { Alarms.Remove(result); //IotReport.GetInstance.HttpDeleteAlarm(result.Id); for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; } if (RemoveAction != null) RemoveAction(AlarmInfo); if (ChangeAction != null) ChangeAction(); } } } }