DBInfoAlarmViewModel.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. namespace ProximaAnalizer.ViewModels;
  2. internal partial class DBInfoAlarmViewModel : ObservableObject
  3. {
  4. public DBInfoAlarmViewModel(DBDataHelper dBDataHelper,
  5. TraceData traceData,
  6. IRegionManager regionManager,
  7. IEventAggregator eventAggregator,
  8. IDialogService dialogService)
  9. {
  10. this._traceData = traceData;
  11. this._DBDataHelper = dBDataHelper;
  12. this._dialogService = dialogService;
  13. this._regionManager = regionManager;
  14. this._eventAggregator = eventAggregator;
  15. CurrentChangedQueue = new(CurrentChangedQueueHandler);
  16. eventAggregator.GetEvent<RefreshAlarmData>().Subscribe(InitDisplayData);
  17. eventAggregator.GetEvent<UpdateSelectData>().Subscribe(SelectedData);
  18. }
  19. private readonly IRegionManager _regionManager;
  20. private readonly IEventAggregator _eventAggregator;
  21. private readonly IDialogService _dialogService;
  22. private readonly DBDataHelper _DBDataHelper;
  23. private readonly TraceData _traceData;
  24. private readonly EventQueue<long> CurrentChangedQueue;
  25. private Timer? _AutoPlayTimer;
  26. private Pages? _FromPage;
  27. private void SelectedData()
  28. {
  29. this.Selected = _eventAggregator.GetEvent<UpdateSelectData>().Selected;
  30. }
  31. private void InitDisplayData()
  32. {
  33. this.RecipeSteps.Clear();
  34. this._regionManager.RequestNavigate("AlarmRegion", "AlarmData");
  35. RefreshAlarmData alarmData = _eventAggregator.GetEvent<RefreshAlarmData>();
  36. this._FromPage = alarmData.FromPage;
  37. if (_traceData.ProcessData is null)
  38. return;
  39. this.RecipeName = _traceData.ProcessData.Recipe_Name;
  40. if (!this._DBDataHelper.GetRecipeSteps(_traceData.ProcessData.Guid, out string reason, out List<RecipeStepData>? recipeSteps) || recipeSteps is null)
  41. {
  42. MessageBox.Show(reason, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  43. return;
  44. }
  45. recipeSteps.Foreach(t => this.RecipeSteps.TryAdd(TimeRounder.IngoreMillionSeconds(t.Step_Begin_Time.Ticks), t));
  46. this.Start = recipeSteps.First().Step_Begin_Time;
  47. this.End = recipeSteps.Last().Step_End_Time;
  48. this._DBDataHelper.SetTimeRange(this.Start, this.End);
  49. if (this._DBDataHelper.GetAlarmData(out List<EventData>? alarms) && alarms is not null)
  50. {
  51. Dictionary<DateTime, StringBuilder> alarmCache = [];
  52. foreach (EventData? alarm in alarms)
  53. {
  54. if (alarm is null || string.IsNullOrEmpty(alarm.Level))
  55. continue;
  56. DateTime occorTime = TimeRounder.IngoreMillionSeconds(alarm.Occur_Time.Ticks);
  57. if (!alarmCache.TryGetValue(occorTime, out StringBuilder? events) || events is null)
  58. {
  59. events = new();
  60. alarmCache[occorTime] = events;
  61. events.Append(alarm.Description);
  62. continue;
  63. }
  64. events.Append(Environment.NewLine);
  65. events.Append(alarm.Description);
  66. }
  67. this.Alarms = alarmCache;
  68. }
  69. if (alarmData.Selected is not null)
  70. {
  71. DateTime time = TimeRounder.IngoreMillionSeconds(alarmData.Selected.Occur_Time.Ticks);
  72. this.Current = time;
  73. this.CurrentLong = time.Ticks;
  74. return;
  75. }
  76. this.Current = this.Start;
  77. this.CurrentLong = this.Start.Ticks;
  78. }
  79. [ObservableProperty]
  80. private RecipeStepData? _CurrentRecipeStep;
  81. [ObservableProperty]
  82. private ObservableDictionary<DateTime, RecipeStepData> _RecipeSteps = [];
  83. [ObservableProperty]
  84. private Dictionary<DateTime, StringBuilder> _Alarms = [];
  85. [ObservableProperty]
  86. private KeyValuePair<DateTime, StringBuilder>? _SelectedAlarm;
  87. partial void OnSelectedAlarmChanged(KeyValuePair<DateTime, StringBuilder>? value)
  88. {
  89. if (value is null)
  90. return;
  91. this.CurrentLong = value.Value.Key.Ticks;
  92. }
  93. [ObservableProperty]
  94. private string? _RecipeName;
  95. [ObservableProperty]
  96. private DateTime _Start;
  97. [ObservableProperty]
  98. private DateTime _End;
  99. [ObservableProperty]
  100. private DateTime _Current;
  101. [ObservableProperty]
  102. private long _CurrentLong;
  103. partial void OnCurrentLongChanged(long value) => this.CurrentChangedQueue.Enqueue(value);
  104. private void CurrentChangedQueueHandler(long value)
  105. {
  106. if (CurrentChangedQueue.Count != 0)
  107. return;
  108. DateTime time = TimeRounder.IngoreMillionSeconds(value);
  109. RecipeStepData? step = this.RecipeSteps.Where(t => t.Key <= this.Current).LastOrDefault().Value;
  110. App.Current.Dispatcher?.Invoke(() =>
  111. {
  112. if (this.Alarms.ContainsKey(time))
  113. this.SelectedAlarm = this.Alarms.Where(t => t.Key == time).FirstOrDefault();
  114. else
  115. this.SelectedAlarm = null;
  116. this.Current = time;
  117. this.CurrentRecipeStep = step;
  118. });
  119. DateTime startTime = time.AddMilliseconds(-500);
  120. DateTime endTime = time.AddMilliseconds(500);
  121. this._DBDataHelper.GetSystemData(startTime, endTime, out List<dynamic>? systemData);
  122. this._DBDataHelper.GetPMData(startTime, endTime, out List<dynamic>? pmData);
  123. this._eventAggregator.GetEvent<UpdateAlarmData>().PmData = pmData;
  124. this._eventAggregator.GetEvent<UpdateAlarmData>().SystemData = systemData;
  125. this._eventAggregator.GetEvent<UpdateAlarmData>().Publish();
  126. }
  127. [ObservableProperty]
  128. private Visibility _PlayVis = Visibility.Visible;
  129. [ObservableProperty]
  130. private Visibility _StopVis = Visibility.Collapsed;
  131. [RelayCommand]
  132. private void Play(string para)
  133. {
  134. switch (para)
  135. {
  136. case "play":
  137. this._AutoPlayTimer ??= new(TimerCallback, null, 0, 1000);
  138. this.PlayVis = Visibility.Collapsed;
  139. this.StopVis = Visibility.Visible;
  140. break;
  141. case "stop":
  142. this._AutoPlayTimer?.Dispose();
  143. this._AutoPlayTimer = null;
  144. this.PlayVis = Visibility.Visible;
  145. this.StopVis = Visibility.Collapsed;
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. private void TimerCallback(object? state)
  152. {
  153. if (this.CurrentLong >= this.End.Ticks)
  154. {
  155. this.Play("stop");
  156. return;
  157. }
  158. App.Current.Dispatcher?.Invoke(() => this.CurrentLong += 10000000);
  159. }
  160. [RelayCommand]
  161. private void TimeOpera(string para)
  162. {
  163. this.Play("stop");
  164. switch (para)
  165. {
  166. case "+":
  167. if (this.Current >= this.End)
  168. return;
  169. this.CurrentLong += 10000000;
  170. break;
  171. case "-":
  172. if (this.Current <= Start)
  173. return;
  174. this.CurrentLong -= 10000000;
  175. break;
  176. case "++":
  177. if (this.Current >= this.End)
  178. return;
  179. this.CurrentLong += 600000000;
  180. break;
  181. case "--":
  182. if (this.Current <= Start)
  183. return;
  184. this.CurrentLong -= 600000000;
  185. break;
  186. case "---":
  187. {
  188. DateTime alarm = this.Alarms.Where(t => t.Key < this.Current).LastOrDefault().Key;
  189. if (alarm == DateTime.MinValue)
  190. break;
  191. this.CurrentLong = alarm.Ticks;
  192. }
  193. break;
  194. case "+++":
  195. {
  196. DateTime alarm = this.Alarms.Where(t => t.Key > this.Current).FirstOrDefault().Key;
  197. if (alarm == DateTime.MinValue)
  198. break;
  199. this.CurrentLong = alarm.Ticks;
  200. }
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. [RelayCommand]
  207. private void SingleTrace()
  208. {
  209. if (this.Selected is null)
  210. return;
  211. if (_eventAggregator.GetEvent<UpdateSelectData>().KeyNames is not object keys || keys is null)
  212. return;
  213. this.Play("stop");
  214. IDialogParameters para = new DialogParameters
  215. {
  216. { "point", this.Selected },
  217. { "keys", keys },
  218. { "time", this.Current}
  219. };
  220. this._dialogService.ShowDialog("SingleTrace", para);
  221. }
  222. [RelayCommand]
  223. private void Navi(string para)
  224. {
  225. this.Play("stop");
  226. this.CurrentChangedQueue.Enqueue(this.CurrentLong);
  227. this._regionManager.RequestNavigate("AlarmRegion", para);
  228. }
  229. [RelayCommand]
  230. private void Return()
  231. {
  232. this.Play("stop");
  233. _eventAggregator.GetEvent<WindowSwitch>().Page = _FromPage is null ? Pages.RecipeStepNavi : _FromPage.Value;
  234. _eventAggregator.GetEvent<WindowSwitch>().Publish();
  235. }
  236. [ObservableProperty]
  237. private object? _Selected;
  238. partial void OnSelectedChanged(object? value)
  239. {
  240. }
  241. }