终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

92 řádky
3.4 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. public Action<string> AddAction { get; set; }
  18. public Action<string> RemoveAction { get; set; }
  19. public Action ChangeAction { get; set; }
  20. /// <summary>
  21. /// 沿报警检测
  22. /// </summary>
  23. /// <param name="Trigger">触发变量</param>
  24. /// <param name="text">报警信息</param>
  25. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  26. public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  27. {
  28. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  29. if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
  30. {
  31. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  32. {
  33. AddAlarm(Trigger, text, alarmLevel);
  34. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  35. }
  36. }
  37. else
  38. {
  39. RemoveAlarm(text);
  40. }
  41. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  42. }
  43. /// <summary>
  44. /// 添加报警信息
  45. /// </summary>
  46. /// <param name="AlarmInfo">报警信息</param>
  47. private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  48. {
  49. Alarm tempAlarm = new Alarm()
  50. {
  51. NumId = Alarms.Count + 1,
  52. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  53. Grade = alarmLevel.ToString(),
  54. Info = AlarmInfo,
  55. Value = value.ToString(),
  56. Time = DateTime.Now.ToString("HH:mm:ss"),
  57. };
  58. Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  59. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  60. {
  61. Alarms.Insert(0, tempAlarm);
  62. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  63. AddAction?.Invoke(AlarmInfo);//添加报警通知
  64. ChangeAction?.Invoke();//更改报警通知
  65. MessageLog.GetInstance.AddDeviceAlarmLogShow(tempAlarm.Info, Guid.NewGuid().ToString());
  66. }
  67. }
  68. /// <summary>
  69. /// 移除报警信息
  70. /// </summary>
  71. /// <param name="AlarmInfo">报警信息</param>
  72. private void RemoveAlarm(string AlarmInfo)
  73. {
  74. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  75. if (result != null)
  76. {
  77. Alarms.Remove(result);
  78. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  79. if (RemoveAction != null) RemoveAction(AlarmInfo);
  80. if (ChangeAction != null) ChangeAction();
  81. }
  82. }
  83. }
  84. }