终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

159 regels
5.6 KiB

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