终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

93 wiersze
3.5 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. var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  59. Sqlite<Alarm>.GetInstance.Save();
  60. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  61. {
  62. Alarms.Insert(0, tempAlarm);
  63. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  64. AddAction?.Invoke(AlarmInfo);//添加报警通知
  65. ChangeAction?.Invoke();//更改报警通知
  66. MessageLog.GetInstance.AddDeviceAlarmLogShow(tempAlarm.Info, Guid.NewGuid().ToString());
  67. }
  68. }
  69. /// <summary>
  70. /// 移除报警信息
  71. /// </summary>
  72. /// <param name="AlarmInfo">报警信息</param>
  73. private void RemoveAlarm(string AlarmInfo)
  74. {
  75. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  76. if (result != null)
  77. {
  78. Alarms.Remove(result);
  79. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  80. if (RemoveAction != null) RemoveAction(AlarmInfo);
  81. if (ChangeAction != null) ChangeAction();
  82. }
  83. }
  84. }
  85. }