终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 regels
4.0 KiB

  1. using BPA.Helper;
  2. using BPASmartClient.Model;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.Device
  10. {
  11. public class AlarmHelper
  12. {
  13. public List<BPASmartClient.Model.Alarm> Alarms { get; set; } = new();
  14. public List<BPASmartClient.Model.Alarm> HistoryAlarms { get; set; } = new();
  15. ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  16. ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
  17. public Action<string> AddAction { get; set; }
  18. public Action<string> RemoveAction { get; set; }
  19. public Action ChangeAction { get; set; }
  20. /// <summary>
  21. /// 沿报警检测
  22. /// </summary>
  23. /// <param name="Trigger">触发变量</param>
  24. /// <param name="text">报警信息</param>
  25. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  26. public void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  27. {
  28. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  29. if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
  30. //if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
  31. if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
  32. {
  33. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  34. {
  35. //AddAlarm(Trigger, text, alarmLevel);
  36. AddAlarm(Trigger, text.Substring(0, text.LastIndexOf("-")), alarmLevel);
  37. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  38. }
  39. }
  40. else
  41. {
  42. //RemoveAlarm(text);
  43. RemoveAlarm(text.Substring(0, text.LastIndexOf("-")));
  44. }
  45. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  46. }
  47. /// <summary>
  48. /// 添加报警信息
  49. /// </summary>
  50. /// <param name="AlarmInfo">报警信息</param>
  51. private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  52. {
  53. BPASmartClient.Model.Alarm tempAlarm = new BPASmartClient.Model.Alarm()
  54. {
  55. NumId = Alarms.Count + 1,
  56. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  57. Grade = alarmLevel.ToString(),
  58. Info = AlarmInfo,
  59. Value = value.ToString(),
  60. Time = DateTime.Now.ToString("HH:mm:ss"),
  61. };
  62. var res = Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Base.Add(tempAlarm);
  63. Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Save();
  64. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  65. {
  66. Alarms.Insert(0, tempAlarm);
  67. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  68. AddAction?.Invoke(AlarmInfo);//添加报警通知
  69. ChangeAction?.Invoke();//更改报警通知
  70. MessageLog.GetInstance.AddDeviceAlarmLogShow(tempAlarm.Info, Guid.NewGuid().ToString());
  71. }
  72. }
  73. /// <summary>
  74. /// 移除报警信息
  75. /// </summary>
  76. /// <param name="AlarmInfo">报警信息</param>
  77. private void RemoveAlarm(string AlarmInfo)
  78. {
  79. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  80. if (result != null)
  81. {
  82. Alarms.Remove(result);
  83. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  84. if (RemoveAction != null) RemoveAction(AlarmInfo);
  85. if (ChangeAction != null) ChangeAction();
  86. }
  87. }
  88. }
  89. }