终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

146 righe
5.8 KiB

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