终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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