No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

110 líneas
4.0 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. using HBLConsole.GVL;
  12. namespace HBLConsole.Business
  13. {
  14. public class AlarmHelper
  15. {
  16. private volatile static AlarmHelper _Instance;
  17. public static AlarmHelper GetInstance => _Instance ?? (_Instance = new AlarmHelper());
  18. private AlarmHelper() { }
  19. public ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
  20. public ObservableCollection<Alarm> HistoryAlarms { get; set; } = new ObservableCollection<Alarm>();
  21. ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  22. public Action<string> AddAction { get; set; }
  23. public Action<string> RemoveAction { get; set; }
  24. public Action ChangeAction { get; set; }
  25. /// <summary>
  26. /// 沿报警检测
  27. /// </summary>
  28. /// <param name="Trigger">触发变量</param>
  29. /// <param name="text">报警信息</param>
  30. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  31. public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  32. {
  33. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  34. if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
  35. {
  36. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  37. {
  38. AddAlarm(Trigger, text, alarmLevel);
  39. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  40. }
  41. }
  42. else
  43. {
  44. RemoveAlarm(text);
  45. }
  46. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  47. }
  48. /// <summary>
  49. /// 添加报警信息
  50. /// </summary>
  51. /// <param name="AlarmInfo">报警信息</param>
  52. private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  53. {
  54. Alarm tempAlarm = new Alarm()
  55. {
  56. NumId = Alarms.Count,
  57. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  58. Grade = alarmLevel.ToString(),
  59. Info = AlarmInfo,
  60. Value = value.ToString(),
  61. Time = DateTime.Now.ToString("HH:mm:ss"),
  62. };
  63. //tempAlarm.Id = IotReport.GetInstance.HttpAddAlarm(new BPA.Message.API请求.AlarmTable
  64. //{
  65. // AlarmTime=DateTime.Now,
  66. // AlarmType= tempAlarm.Grade,
  67. // AlarmMessage= tempAlarm.Info,
  68. // AlarmVla= tempAlarm.Value,
  69. // ClientId= InternetInfo.ClientId.ToString()
  70. //});
  71. Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  72. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  73. {
  74. Alarms.Add(tempAlarm);
  75. if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
  76. if (ChangeAction != null) ChangeAction();//更改报警通知
  77. }
  78. }
  79. /// <summary>
  80. /// 移除报警信息
  81. /// </summary>
  82. /// <param name="AlarmInfo">报警信息</param>
  83. private void RemoveAlarm(string AlarmInfo)
  84. {
  85. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  86. if (result != null)
  87. {
  88. Alarms.Remove(result);
  89. //IotReport.GetInstance.HttpDeleteAlarm(result.Id);
  90. for (int i = 0; i < Alarms.Count; i++)
  91. {
  92. Alarms.ElementAt(i).NumId = i + 1;
  93. }
  94. if (RemoveAction != null) RemoveAction(AlarmInfo);
  95. if (ChangeAction != null) ChangeAction();
  96. }
  97. }
  98. }
  99. }