|
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.Device
- {
- public class AlarmHelper
- {
- public List<Alarm> Alarms { get; set; } = new List<Alarm>();
- public List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
- ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
- public Action<string> AddAction { get; set; }
- public Action<string> RemoveAction { get; set; }
- public Action ChangeAction { get; set; }
-
- /// <summary>
- /// 沿报警检测
- /// </summary>
- /// <param name="Trigger">触发变量</param>
- /// <param name="text">报警信息</param>
- /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
- 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;
- }
-
- /// <summary>
- /// 添加报警信息
- /// </summary>
- /// <param name="AlarmInfo">报警信息</param>
- private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
- {
- Alarm tempAlarm = new Alarm()
- {
- NumId = Alarms.Count + 1,
- Date = DateTime.Now.ToString("yyyy/MM/dd"),
- Grade = alarmLevel.ToString(),
- Info = AlarmInfo,
- Value = value.ToString(),
- Time = DateTime.Now.ToString("HH:mm:ss"),
- };
-
- var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
- Sqlite<Alarm>.GetInstance.Save();
-
- if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
- {
- Alarms.Insert(0, tempAlarm);
- for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
- AddAction?.Invoke(AlarmInfo);//添加报警通知
- ChangeAction?.Invoke();//更改报警通知
- MessageLog.GetInstance.AddDeviceAlarmLogShow(tempAlarm.Info, Guid.NewGuid().ToString());
- }
- }
-
- /// <summary>
- /// 移除报警信息
- /// </summary>
- /// <param name="AlarmInfo">报警信息</param>
- private void RemoveAlarm(string AlarmInfo)
- {
- var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
- if (result != null)
- {
- Alarms.Remove(result);
- for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
- if (RemoveAction != null) RemoveAction(AlarmInfo);
- if (ChangeAction != null) ChangeAction();
- }
- }
-
- }
- }
|