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.
 
 

92 lines
3.3 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(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(string AlarmInfo, AlarmLevel alarmLevel)
  52. {
  53. Alarm tempAlarm = new Alarm()
  54. {
  55. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  56. Grade = alarmLevel.ToString(),
  57. Info = AlarmInfo,
  58. Time = DateTime.Now.ToString("HH:mm:ss"),
  59. };
  60. Sqlite<Alarm>.GetInstance.Add(tempAlarm);
  61. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  62. {
  63. Alarms.Add(tempAlarm);
  64. if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
  65. if (ChangeAction != null) ChangeAction();//更改报警通知
  66. }
  67. }
  68. /// <summary>
  69. /// 移除报警信息
  70. /// </summary>
  71. /// <param name="AlarmInfo">报警信息</param>
  72. private void RemoveAlarm(string AlarmInfo)
  73. {
  74. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  75. if (result != null)
  76. {
  77. Alarms.Remove(result);
  78. if (RemoveAction != null) RemoveAction(AlarmInfo);
  79. if (ChangeAction != null) ChangeAction();
  80. }
  81. }
  82. }
  83. }