BackUpViewModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using Caliburn.Micro.Core;
  8. using Caliburn.Micro;
  9. using FurnaceUI.Models;
  10. using FurnaceUI.Views.Maintenances;
  11. using Aitex.Core.RT.IOCore;
  12. using MECF.Framework.Common.OperationCenter;
  13. using Aitex.Core.Util;
  14. using OpenSEMI.ClientBase;
  15. using MECF.Framework.Common.DataCenter;
  16. using System.Globalization;
  17. using MECF.Framework.UI.Client.ClientBase;
  18. using System.Collections.ObjectModel;
  19. using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
  20. using FurnaceUI.DataModule;
  21. using System.Windows.Data;
  22. using System.Threading;
  23. using System.IO;
  24. using System.Diagnostics;
  25. using FurnaceUI.Views.Editors;
  26. using System.Windows.Input;
  27. using FurnaceUI.Controls.Common;
  28. namespace FurnaceUI.Views.Parameter
  29. {
  30. public class BackUpViewModel : FurnaceUIViewModelBase
  31. {
  32. [Subscription("Rt.Status")]
  33. public string RtStatus { get; set; }
  34. [Subscription("PM1.Status")]
  35. public string PM1EntityStatus { get; set; }
  36. private Visibility _gridHistoryVisibility = Visibility.Visible;
  37. public Visibility GridHistoryVisibility
  38. {
  39. get { return _gridHistoryVisibility; }
  40. set { _gridHistoryVisibility = value; this.NotifyOfPropertyChange(nameof(GridHistoryVisibility)); }
  41. }
  42. [Subscription("System.BackUpFileData")]
  43. public Dictionary<string, List<string>> AllFilesDatas { get; set; }
  44. private ObservableCollection<PageDataView> _historyTableDatas = new ObservableCollection<PageDataView>();
  45. public ObservableCollection<PageDataView> HistoryTableDatas
  46. {
  47. get { return _historyTableDatas; }
  48. set { _historyTableDatas = value; this.NotifyOfPropertyChange(nameof(HistoryTableDatas)); }
  49. }
  50. public PageDataView SelectedItemHistory { get; set; }
  51. private bool _busyIndicatorVisibility = false;
  52. public bool BusyIndicatorVisibility
  53. {
  54. get { return _busyIndicatorVisibility; }
  55. set { _busyIndicatorVisibility = value; this.NotifyOfPropertyChange(nameof(BusyIndicatorVisibility)); }
  56. }
  57. private bool _isAllEnable = true;
  58. public bool IsAllEnable
  59. {
  60. get { return _isAllEnable; }
  61. set { _isAllEnable = value; this.NotifyOfPropertyChange(nameof(IsAllEnable)); }
  62. }
  63. RelayCommand _compareCommand;
  64. public ICommand CompareCommand
  65. {
  66. get
  67. {
  68. if (_compareCommand == null)
  69. {
  70. _compareCommand = new RelayCommand(param => this.CompareClick(), param => this.CanCompareClick());
  71. }
  72. return _compareCommand;
  73. }
  74. }
  75. RelayCommand _rollBackCommand;
  76. public ICommand RollBackCommand
  77. {
  78. get
  79. {
  80. if (_rollBackCommand == null)
  81. {
  82. _rollBackCommand = new RelayCommand(param => this.RollBackClick(), param => this.CanRollBackClick());
  83. }
  84. return _rollBackCommand;
  85. }
  86. }
  87. protected override void OnActivate()
  88. {
  89. base.OnActivate();
  90. DelayData();
  91. }
  92. protected override void OnViewLoaded(object view)
  93. {
  94. base.OnViewLoaded(view);
  95. }
  96. protected override void OnDeactivate(bool close)
  97. {
  98. base.OnDeactivate(close);
  99. }
  100. public void CreateZIP()
  101. {
  102. var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.CONFIRM,
  103. $"Are you sure you want to CreateZIP?");
  104. if (selection == DialogButton.No)
  105. return;
  106. BusyIndicatorVisibility = true;
  107. InvokeClient.Instance.Service.DoOperation($"System.CreateZIP");
  108. DelayData();
  109. }
  110. public void BackUpClick()
  111. {
  112. var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.CONFIRM,
  113. $"Are you sure you want to BackUp?");
  114. if (selection == DialogButton.No)
  115. return;
  116. BusyIndicatorVisibility = true;
  117. InvokeClient.Instance.Service.DoOperation($"System.BackUpFileData");
  118. DelayData(1500);
  119. }
  120. async void DelayData(int delayTime = 1000)
  121. {
  122. await WaitForResultsAsync(delayTime);
  123. GetDataOfConfigItems();
  124. BusyIndicatorVisibility = false;
  125. }
  126. private async Task WaitForResultsAsync(int delayTime)
  127. {
  128. // Simulate waiting for results using a delay
  129. // In a real-world scenario, you might wait for an event or a specific condition
  130. await Task.Delay(delayTime);
  131. // Here you can add logic to check if the results are ready
  132. // For example, polling or using a completion source
  133. }
  134. private void RollBackClick()
  135. {
  136. var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.WARNING,
  137. $"If you want to RollBack, you must restart after RollBack!");
  138. if (selection == DialogButton.No)
  139. return;
  140. var selectItem = HistoryTableDatas.Where(a => a.IsSelect == true).Select(a => $"{a.ValueStr}@{a.Name}").FirstOrDefault();
  141. if (selectItem == null)
  142. {
  143. DialogBox.ShowWarning("Please select at least one");
  144. return;
  145. }
  146. InvokeClient.Instance.Service.DoOperation($"System.RollBackFileData", selectItem);
  147. DelayData();
  148. }
  149. private void CompareClick()
  150. {
  151. var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.CONFIRM,
  152. $"Are you sure you want to Compare?");
  153. if (selection == DialogButton.No)
  154. return;
  155. var selectItems = HistoryTableDatas.Where(a => a.IsSelect == true).Select(a => $"{a.ValueStr}@{a.Name}").ToList();
  156. InvokeClient.Instance.Service.DoOperation($"System.CompareFileData", string.Join(",", selectItems.ToArray()));
  157. var windowManager = IoC.Get<IWindowManager>();
  158. BackUpCompareViewModel backUpCompareViewModel = new BackUpCompareViewModel();
  159. backUpCompareViewModel.SelectItemNames = HistoryTableDatas.Where(a => a.IsSelect == true).Select(a => $"{a.ValueStr}").ToList();
  160. var rtn = (windowManager as WindowManager)?.ShowDialogWithTitle(backUpCompareViewModel, null, $"Compare View");
  161. }
  162. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  163. {
  164. base.InvokeAfterUpdateProperty(data);
  165. //System位于AutoIdle/Idle/Init/ PM位于
  166. IsAllEnable = (RtStatus == "Idle" || RtStatus == "AutoIdle" || RtStatus == "Init" || RtStatus == "Error") && (PM1EntityStatus == "Idle");
  167. }
  168. private bool CanRollBackClick()
  169. {
  170. bool isEnable = false;
  171. if (HistoryTableDatas != null)
  172. {
  173. int count = HistoryTableDatas.Count(p => p.IsSelect == true);
  174. if (count == 1)
  175. {
  176. isEnable = true;
  177. }
  178. }
  179. return isEnable;
  180. }
  181. private bool CanCompareClick()
  182. {
  183. bool isEnable = false;
  184. if (HistoryTableDatas != null)
  185. {
  186. int count = HistoryTableDatas.Count(p => p.IsSelect == true);
  187. if (0 < count && count <= 2)
  188. {
  189. isEnable = true;
  190. }
  191. }
  192. return isEnable;
  193. }
  194. private void GetDataOfConfigItems()
  195. {
  196. HistoryTableDatas.Clear();
  197. if (AllFilesDatas == null || AllFilesDatas.Count == 0)
  198. {
  199. return;
  200. }
  201. foreach (var item in AllFilesDatas["SC"])
  202. {
  203. var nameStr = item.Split('@');
  204. var time = nameStr[0];
  205. var name = nameStr[1];
  206. HistoryTableDatas.Add(new PageDataView() { Name = name, ValueStr = time });
  207. }
  208. }
  209. }
  210. }