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 lines
3.6 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing;
  8. using HBLConsole.Service;
  9. using HBLConsole.Model;
  10. using System.Collections.ObjectModel;
  11. namespace HBLConsole.Business
  12. {
  13. public class AlarmHelper
  14. {
  15. private volatile static AlarmHelper _Instance;
  16. public static AlarmHelper GetInstance => _Instance ?? (_Instance = new AlarmHelper());
  17. private AlarmHelper() { }
  18. public ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
  19. public ObservableCollection<Alarm> HistoryAlarms { get; set; } = new ObservableCollection<Alarm>();
  20. ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  21. public Action<string> AddAction { get; set; }
  22. public Action<string> RemoveAction { get; set; }
  23. public Action ChangeAction { get; set; }
  24. /// <summary>
  25. /// 沿报警检测
  26. /// </summary>
  27. /// <param name="Trigger">触发变量</param>
  28. /// <param name="text">报警信息</param>
  29. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  30. public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  31. {
  32. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  33. if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
  34. {
  35. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  36. {
  37. AddAlarm(Trigger, text, alarmLevel);
  38. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  39. }
  40. }
  41. else
  42. {
  43. RemoveAlarm(text);
  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. Alarm tempAlarm = new Alarm()
  54. {
  55. NumId = Alarms.Count,
  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. Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  63. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  64. {
  65. Alarms.Add(tempAlarm);
  66. if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
  67. if (ChangeAction != null) ChangeAction();//更改报警通知
  68. }
  69. }
  70. /// <summary>
  71. /// 移除报警信息
  72. /// </summary>
  73. /// <param name="AlarmInfo">报警信息</param>
  74. private void RemoveAlarm(string AlarmInfo)
  75. {
  76. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  77. if (result != null)
  78. {
  79. Alarms.Remove(result);
  80. for (int i = 0; i < Alarms.Count; i++)
  81. {
  82. Alarms.ElementAt(i).NumId = i + 1;
  83. }
  84. if (RemoveAction != null) RemoveAction(AlarmInfo);
  85. if (ChangeAction != null) ChangeAction();
  86. }
  87. }
  88. }
  89. }