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.
 
 

99 lines
3.8 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. namespace HBLConsole.Business
  11. {
  12. public class AlarmHelper
  13. {
  14. private volatile static AlarmHelper _Instance;
  15. public static AlarmHelper GetInstance => _Instance ?? (_Instance = new AlarmHelper());
  16. private AlarmHelper()
  17. {
  18. Json<AlarmHzParameter>.Read();
  19. colorValue.TryAdd(AlarmLevel.一般报警.ToString(), Color.FromArgb(1, 255, 216, 0));
  20. colorValue.TryAdd(AlarmLevel.严重报警.ToString(), Color.FromArgb(1, 255, 0, 0));
  21. }
  22. private List<AlarmInfo> Alarms { get; set; } = new List<AlarmInfo>();
  23. ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  24. public ConcurrentDictionary<string, Color> colorValue = new ConcurrentDictionary<string, Color>();
  25. public Action<string> AddAction { get; set; }
  26. public Action<string> RemoveAction { get; set; }
  27. public Action ChangeAction { get; set; }
  28. /// <summary>
  29. /// 沿报警检测
  30. /// </summary>
  31. /// <param name="Trigger">触发变量</param>
  32. /// <param name="text">报警信息</param>
  33. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  34. public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  35. {
  36. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  37. if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
  38. {
  39. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  40. {
  41. AddAlarm(text, alarmLevel);
  42. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  43. }
  44. }
  45. else
  46. {
  47. RemoveAlarm(text);
  48. }
  49. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  50. }
  51. /// <summary>
  52. /// 获取当前报警信息集合
  53. /// </summary>
  54. public List<AlarmInfo> GetAlarmInfo => Alarms;
  55. /// <summary>
  56. /// 添加报警信息
  57. /// </summary>
  58. /// <param name="AlarmInfo">报警信息</param>
  59. private void AddAlarm(string AlarmInfo, AlarmLevel alarmLevel)
  60. {
  61. if (Alarms.FirstOrDefault(p => p.AlarmInfomation == AlarmInfo) == null)
  62. {
  63. Alarms.Add(new AlarmInfo() { AlarmInfomation = AlarmInfo, date = DateTime.Now.ToString("HH:mm:ss"), AlarmLevel = alarmLevel.ToString() });
  64. //报警频率计算
  65. if (Json<AlarmHzParameter>.Data.AlarmHz.ContainsKey(AlarmInfo))
  66. Json<AlarmHzParameter>.Data.AlarmHz[AlarmInfo]++;
  67. else
  68. Json<AlarmHzParameter>.Data.AlarmHz.TryAdd(AlarmInfo, 1);
  69. if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
  70. if (ChangeAction != null) ChangeAction();//更改报警通知
  71. }
  72. }
  73. /// <summary>
  74. /// 移除报警信息
  75. /// </summary>
  76. /// <param name="AlarmInfo">报警信息</param>
  77. private void RemoveAlarm(string AlarmInfo)
  78. {
  79. var result = Alarms.FirstOrDefault(p => p.AlarmInfomation == AlarmInfo);
  80. if (result != null)
  81. {
  82. Alarms.Remove(result);
  83. if (RemoveAction != null) RemoveAction(AlarmInfo);
  84. if (ChangeAction != null) ChangeAction();
  85. }
  86. }
  87. }
  88. }