终端一体化运控平台
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.
 
 
 

156 lignes
6.3 KiB

  1. using BPASmartClient.CustomResource.Pages.ViewModel;
  2. using BPA.Helper;
  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.Threading.Tasks;
  12. using System.Windows;
  13. namespace BPASmartClient.CustomResource.Pages.Model
  14. {
  15. public class AlarmHelper<AlarmT> where AlarmT : class, new()
  16. {
  17. private volatile static ConcurrentDictionary<string, AlarmT> _Instance;
  18. private static readonly object Obj_Lock = new object();
  19. public static AlarmT GetInstance(string name = "")
  20. {
  21. lock (Obj_Lock)
  22. {
  23. if (string.IsNullOrEmpty(name)) name = typeof(AlarmT).Name;
  24. if (_Instance == null) _Instance = new ConcurrentDictionary<string, AlarmT>();
  25. if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new AlarmT());
  26. return _Instance[name];
  27. }
  28. }
  29. private AlarmHelper() { }
  30. public static ObservableCollection<BPASmartClient.Model.Alarm> Alarms { get; set; } = new ObservableCollection<BPASmartClient.Model.Alarm>();
  31. public static List<BPASmartClient.Model.Alarm> HistoryAlarms { get; set; } = new List<BPASmartClient.Model.Alarm>();
  32. static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  33. static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
  34. public static Action<string> AddAction { get; set; }
  35. public static Action<string> RemoveAction { get; set; }
  36. public static Action ChangeAction { get; set; }
  37. //public static AlarmT Alarm { get; set; } = new AlarmT();
  38. public static void Init()
  39. {
  40. AlarmViewModel.AlarmInfos = Alarms;
  41. TaskManage.GetInstance.StartLong(new Action(() =>
  42. {
  43. if (_Instance != null)
  44. {
  45. foreach (var temp in _Instance)
  46. {
  47. foreach (var item in temp.Value.GetType().GetProperties())
  48. {
  49. if (item.CustomAttributes.Count() > 0)
  50. {
  51. var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
  52. if (AlarmModel != null)
  53. {
  54. bool value = Convert.ToBoolean(_Instance[temp.Key].GetType().GetProperty(item.Name)?.GetValue(_Instance[temp.Key]));
  55. string text = typeof(AlarmT).Name == temp.Key ? AlarmModel.AlarmInfo : $"{temp.Key}:{AlarmModel.AlarmInfo}";
  56. EdgeAlarm(value, text, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. Thread.Sleep(100);
  63. }), $"{typeof(AlarmT).Name},报警通用模块监听");
  64. }
  65. /// <summary>
  66. /// 沿报警检测
  67. /// </summary>
  68. /// <param name="Trigger">触发变量</param>
  69. /// <param name="text">报警信息</param>
  70. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  71. private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  72. {
  73. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  74. if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
  75. if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
  76. {
  77. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  78. {
  79. AddAlarm(Trigger, text, alarmLevel);
  80. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  81. }
  82. }
  83. else RemoveAlarm(text);
  84. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  85. }
  86. /// <summary>
  87. /// 添加报警信息
  88. /// </summary>
  89. /// <param name="AlarmInfo">报警信息</param>
  90. private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  91. {
  92. BPASmartClient.Model.Alarm tempAlarm = new BPASmartClient.Model.Alarm()
  93. {
  94. NumId = Alarms.Count + 1,
  95. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  96. Grade = alarmLevel.ToString(),
  97. Info = AlarmInfo,
  98. Value = value.ToString(),
  99. Time = DateTime.Now.ToString("HH:mm:ss"),
  100. };
  101. try
  102. {
  103. var res = Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Base.Add(tempAlarm);
  104. Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Save();
  105. }
  106. catch (Exception)
  107. {
  108. }
  109. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  110. {
  111. Application.Current.Dispatcher.Invoke(new Action(() =>
  112. {
  113. Alarms.Insert(0, tempAlarm);
  114. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  115. }));
  116. AddAction?.Invoke(AlarmInfo);//添加报警通知
  117. ChangeAction?.Invoke();//更改报警通知
  118. }
  119. }
  120. /// <summary>
  121. /// 移除报警信息
  122. /// </summary>
  123. /// <param name="AlarmInfo">报警信息</param>
  124. private static void RemoveAlarm(string AlarmInfo)
  125. {
  126. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  127. if (result != null)
  128. {
  129. Application.Current.Dispatcher.Invoke(new Action(() =>
  130. {
  131. Alarms.Remove(result);
  132. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  133. }));
  134. if (RemoveAction != null) RemoveAction(AlarmInfo);
  135. if (ChangeAction != null) ChangeAction();
  136. }
  137. }
  138. }
  139. }