终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

99 рядки
4.0 KiB

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