终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

100 linhas
4.0 KiB

  1. using BPA.Helper;
  2. using BPA.Helper.Interface;
  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<BPASmartClient.Model.Alarm> Alarms { get; set; } = new();
  15. public List<BPASmartClient.Model.Alarm> HistoryAlarms { get; set; } = new();
  16. ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  17. ConcurrentDictionary<string, IDelay> delays = new ConcurrentDictionary<string, IDelay>();
  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. BPASmartClient.Model.Alarm tempAlarm = new BPASmartClient.Model.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<BPASmartClient.Model.Alarm>.GetInstance.Base.Add(tempAlarm);
  64. Sqlite<BPASmartClient.Model.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.ShowWarn(tempAlarm.Info);
  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. }