终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

158 Zeilen
5.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.ObjectModel;
  7. using System.Windows;
  8. using System.Threading;
  9. using BPA.Helper;
  10. using BPASmartClient.Device;
  11. using BPASmartClient.Business;
  12. using BPASmartClient.Model;
  13. namespace BPASmartClient.ViewModel
  14. {
  15. public class AlarmViewModel : NotifyBase
  16. {
  17. public AlarmViewModel()
  18. {
  19. ControlCommand = new BPARelayCommand(() =>
  20. {
  21. if (ControlButText == "报警复位")
  22. {
  23. return;
  24. }
  25. if (ControlButText == "开始查询")
  26. {
  27. var lists = Sqlite<BPASmartClient.Model.Alarm>.GetInstance.GetData();
  28. var res = lists.Where(p => Convert.ToDateTime(p.Date) >= StartDateTime && Convert.ToDateTime(p.Date) <= EndDateTime).ToList();
  29. if (res != null)
  30. {
  31. HistoryAlarm.Clear();
  32. foreach (var item in res)
  33. {
  34. HistoryAlarm.Add(item);
  35. }
  36. }
  37. }
  38. });
  39. SwitchCommand = new BPARelayCommand(() =>
  40. {
  41. if (ButContent == "历史报警")
  42. {
  43. GetHistoryAlarm();
  44. CurrentDataVis = Visibility.Hidden;
  45. HistoryDataVis = Visibility.Visible;
  46. IsVisibility = Visibility.Visible;
  47. ControlButText = "开始查询";
  48. ButContent = "实时报警";
  49. return;
  50. }
  51. if (ButContent == "实时报警")
  52. {
  53. HistoryDataVis = Visibility.Hidden;
  54. CurrentDataVis = Visibility.Visible;
  55. IsVisibility = Visibility.Hidden;
  56. ControlButText = "报警复位";
  57. ButContent = "历史报警";
  58. return;
  59. }
  60. });
  61. AlarmMonitoring();
  62. }
  63. public void AlarmMonitoring()
  64. {
  65. AlarmInfos.Clear();
  66. Plugin.GetInstance?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  67. {
  68. device.alarmHelper.Alarms.ForEach((item) => { AlarmInfos.Add(item); });
  69. device.alarmHelper.AddAction += new Action<string>((s) =>
  70. {
  71. var res = device.alarmHelper.Alarms.FirstOrDefault(p => p.Info == s);
  72. if (res != null) Application.Current.Dispatcher.Invoke(new Action(() => { AlarmInfos.Insert(0, res); }));
  73. });
  74. device.alarmHelper.RemoveAction += new Action<string>((s) =>
  75. {
  76. var res = AlarmInfos.FirstOrDefault(p => p.Info == s);
  77. if (res != null) Application.Current.Dispatcher.Invoke(new Action(() => { AlarmInfos.Remove(res); }));
  78. });
  79. });
  80. }
  81. private void GetHistoryAlarm()
  82. {
  83. var data = Sqlite<BPASmartClient.Model.Alarm>.GetInstance.GetData();
  84. if (data != null)
  85. {
  86. HistoryAlarm.Clear();
  87. foreach (var item in data)
  88. {
  89. int day = DateTime.Now.Subtract(Convert.ToDateTime(item.Date)).Days;
  90. if (day == 0)
  91. {
  92. HistoryAlarm.Add(item);
  93. }
  94. }
  95. }
  96. }
  97. public BPARelayCommand SwitchCommand { get; set; }
  98. public BPARelayCommand ControlCommand { get; set; }
  99. public Visibility CurrentDataVis { get { return _mCurrentDataVis; } set { _mCurrentDataVis = value; OnPropertyChanged(); } }
  100. private Visibility _mCurrentDataVis = Visibility.Visible;
  101. public Visibility HistoryDataVis { get { return _mHistoryDataVis; } set { _mHistoryDataVis = value; OnPropertyChanged(); } }
  102. private Visibility _mHistoryDataVis = Visibility.Hidden;
  103. /// <summary>
  104. /// 是否显示
  105. /// </summary>
  106. public Visibility IsVisibility { get { return _mIsVisibility; } set { _mIsVisibility = value; OnPropertyChanged(); } }
  107. private Visibility _mIsVisibility = Visibility.Hidden;
  108. /// <summary>
  109. /// 文字显示
  110. /// </summary>
  111. public string ButContent { get { return _mButContent; } set { _mButContent = value; OnPropertyChanged(); } }
  112. private string _mButContent = "历史报警";
  113. /// <summary>
  114. /// 控制按钮文本显示
  115. /// </summary>
  116. public string ControlButText { get { return _mControlButText; } set { _mControlButText = value; OnPropertyChanged(); } }
  117. private string _mControlButText = "报警复位";
  118. /// <summary>
  119. /// 开始时间
  120. /// </summary>
  121. public DateTime StartDateTime { get { return _mStartDateTime; } set { _mStartDateTime = value; OnPropertyChanged(); } }
  122. private DateTime _mStartDateTime = DateTime.Now;
  123. /// <summary>
  124. /// 结束时间
  125. /// </summary>
  126. public DateTime EndDateTime { get { return _mEndDateTime; } set { _mEndDateTime = value; OnPropertyChanged(); } }
  127. private DateTime _mEndDateTime = DateTime.Now;
  128. public ObservableCollection<BPASmartClient.Model.Alarm> AlarmInfos { get; set; } = new();
  129. public ObservableCollection<BPASmartClient.Model.Alarm> HistoryAlarm { get; set; } = new();
  130. }
  131. }