终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

124 lignes
4.9 KiB

  1. using BPASmartClient.Helper;
  2. //using BPASmartClient.Message;
  3. using BPASmartClient.Model;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Threading;
  11. using System.Windows;
  12. namespace BPASmartClient.CustomResource.Pages.Model
  13. {
  14. public class AlarmHelper<AlarmT> where AlarmT : class, new()
  15. {
  16. public static ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
  17. public static List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
  18. static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  19. static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
  20. public static Action<string> AddAction { get; set; }
  21. public static Action<string> RemoveAction { get; set; }
  22. public static Action ChangeAction { get; set; }
  23. public static AlarmT Alarm { get; set; } = new AlarmT();
  24. public static void Init()
  25. {
  26. ThreadManage.GetInstance().StartLong(new Action(() =>
  27. {
  28. foreach (var item in Alarm.GetType().GetProperties())
  29. {
  30. if (item.CustomAttributes.Count() > 0)
  31. {
  32. var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
  33. if (AlarmModel != null)
  34. {
  35. bool value = Convert.ToBoolean(Alarm.GetType().GetProperty(item.Name)?.GetValue(Alarm));
  36. EdgeAlarm(value, AlarmModel.AlarmInfo, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
  37. }
  38. }
  39. }
  40. Thread.Sleep(100);
  41. }), "报警通用模块监听");
  42. }
  43. /// <summary>
  44. /// 沿报警检测
  45. /// </summary>
  46. /// <param name="Trigger">触发变量</param>
  47. /// <param name="text">报警信息</param>
  48. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  49. private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  50. {
  51. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  52. if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
  53. if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
  54. {
  55. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] :flagbit[text])
  56. {
  57. AddAlarm(Trigger, text, alarmLevel);
  58. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  59. }
  60. }
  61. else RemoveAlarm(text);
  62. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  63. }
  64. /// <summary>
  65. /// 添加报警信息
  66. /// </summary>
  67. /// <param name="AlarmInfo">报警信息</param>
  68. private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  69. {
  70. Alarm tempAlarm = new Alarm()
  71. {
  72. NumId = Alarms.Count + 1,
  73. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  74. Grade = alarmLevel.ToString(),
  75. Info = AlarmInfo,
  76. Value = value.ToString(),
  77. Time = DateTime.Now.ToString("HH:mm:ss"),
  78. };
  79. var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  80. Sqlite<Alarm>.GetInstance.Save();
  81. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  82. {
  83. Application.Current.Dispatcher.Invoke(new Action(() =>
  84. {
  85. Alarms.Insert(0, tempAlarm);
  86. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  87. }));
  88. AddAction?.Invoke(AlarmInfo);//添加报警通知
  89. ChangeAction?.Invoke();//更改报警通知
  90. }
  91. }
  92. /// <summary>
  93. /// 移除报警信息
  94. /// </summary>
  95. /// <param name="AlarmInfo">报警信息</param>
  96. private static void RemoveAlarm(string AlarmInfo)
  97. {
  98. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  99. if (result != null)
  100. {
  101. Application.Current.Dispatcher.Invoke(new Action(() =>
  102. {
  103. Alarms.Remove(result);
  104. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  105. }));
  106. if (RemoveAction != null) RemoveAction(AlarmInfo);
  107. if (ChangeAction != null) ChangeAction();
  108. }
  109. }
  110. }
  111. }