终端一体化运控平台
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.
 
 
 

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