MainWindowViewModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Device;
  2. using EEMSUIClientCore;
  3. using Mapster;
  4. using ServiceBase;
  5. using System;
  6. using System.Windows.Controls;
  7. namespace EEMSMain.ViewModels;
  8. public partial class MainWindowViewModel : ObservableObject
  9. {
  10. public MainWindowViewModel(ICommonContainer commonContainer, IUIProvider uIProvider, IUICaller uICaller, ContainerManager containerManager, DeviceCollection deviceCollection)
  11. {
  12. this._uiProvider = uIProvider;
  13. this._uiCaller = uICaller;
  14. this._commonContainer = commonContainer;
  15. this.ContainerManager = containerManager;
  16. this._deviceCollection = deviceCollection;
  17. containerManager.Containers.CollectionChanged += Containers_CollectionChanged;
  18. //this.FakeData();
  19. this.ConnectServer();
  20. }
  21. private void Containers_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  22. {
  23. if (sender is not ObservableDictionary<int, ContainerInfo> t)
  24. return;
  25. ContainerInfo? containerInfo = t.Values.FirstOrDefault(x => x.IsDefault);
  26. if (containerInfo is null)
  27. return;
  28. this.Open(containerInfo);
  29. }
  30. private readonly IUIProvider _uiProvider;
  31. private readonly IUICaller _uiCaller;
  32. private readonly ICommonContainer _commonContainer;
  33. public ContainerManager ContainerManager { get; }
  34. private readonly DeviceCollection _deviceCollection;
  35. void FakeData()
  36. {
  37. Random r = new();
  38. for (int i = 1; i <= 6; i++)
  39. {
  40. Guid guid = Guid.NewGuid();
  41. DeviceInfo_VM device = new()
  42. {
  43. DeviceModel = DeviceModel.JetKepler,
  44. DeviceSubModel = KeplerSubModel.JetKepler_2200A.ToString(),
  45. DeviceName = $"Device {i}",
  46. Position = $"position-{i}",
  47. SoftwareVersion = "1.0.0.0",
  48. IP = $"192.168.250.{i}",
  49. Port = 50002,
  50. UpdateTime = DateTime.Now,
  51. Guid = guid,
  52. IsConnected = Convert.ToBoolean(r.Next(0, 2))
  53. };
  54. RecipeInfo_VM recipe = new()
  55. {
  56. DeviceId = guid,
  57. CurrentStepName = "Step 1",
  58. NextStepName = "Step 2",
  59. CurrentStepTotalTime = r.Next(50, 100),
  60. CurrentStepRemainTime = r.Next(0, 50),
  61. TotalTime = r.Next(200, 400),
  62. TotalRemainTime = r.Next(100, 200),
  63. };
  64. //recipe.RecipeInfo["PJobID"] = "TestPJob";
  65. //recipe.RecipeInfo["CJboID"] = "TestCJob";
  66. //recipe.RecipeInfo["RecipeType"] = (RecipeType)r.Next(0, 8);
  67. DeviceData_VM deviceData = new()
  68. {
  69. DeviceId = guid,
  70. DeviceModel = DeviceModel.JetKepler,
  71. DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
  72. RecipeInfo = recipe,
  73. Alarms = []
  74. };
  75. deviceData.OtherInfo.Add("子型号", KeplerSubModel.JetKepler_2200A);
  76. //deviceData.OtherInfo["PMC Mode"] = PMCMode.Running.ToString();
  77. if (r.Next(1, 4) > 1)
  78. {
  79. for (int j = 0; j < r.Next(1, 5); j++)
  80. {
  81. Alarm_VM alarm = new()
  82. {
  83. DeviceId = guid,
  84. AlarmCode = r.Next(10000, 90000).ToString(),
  85. AlarmTime = DateTime.Now,
  86. AlarmName = $"Test Alarm {j}"
  87. };
  88. deviceData.Alarms.Add(alarm);
  89. }
  90. }
  91. _deviceCollection.DeviceDataList.Add(guid, deviceData);
  92. _deviceCollection.DeviceList.Add(guid, device);
  93. }
  94. for (int i = 1; i <= 3; i++)
  95. {
  96. Guid guid = Guid.NewGuid();
  97. DeviceInfo_VM device = new()
  98. {
  99. DeviceModel = DeviceModel.Proxima,
  100. DeviceSubModel = ProximaSubModel.Proxima_ELK.ToString(),
  101. DeviceName = $"Device {i}",
  102. Position = $"position-{i}",
  103. SoftwareVersion = "1.0.0.0",
  104. IP = $"192.168.250.{i}",
  105. Port = 50002,
  106. UpdateTime = DateTime.Now,
  107. Guid = guid,
  108. IsConnected = Convert.ToBoolean(r.Next(0, 2))
  109. };
  110. RecipeInfo_VM recipe = new()
  111. {
  112. DeviceId = guid,
  113. CurrentStepName = "Step 1",
  114. NextStepName = "Step 2",
  115. CurrentStepTotalTime = r.Next(50, 100),
  116. CurrentStepRemainTime = r.Next(0, 50),
  117. TotalTime = r.Next(200, 400),
  118. TotalRemainTime = r.Next(100, 200),
  119. };
  120. recipe.RecipeInfo.Add("PJobID", "TestPJob");
  121. recipe.RecipeInfo.Add("CJboID", "TestCJob");
  122. recipe.RecipeInfo.Add("RecipeType", (RecipeType)r.Next(0, 8));
  123. DeviceData_VM deviceData = new()
  124. {
  125. DeviceId = guid,
  126. DeviceModel = DeviceModel.Proxima,
  127. DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
  128. //PMCMode = PMCMode.Running,
  129. RecipeInfo = recipe,
  130. Alarms = []
  131. };
  132. deviceData.OtherInfo.Add("子型号", ProximaSubModel.Proxima_ELK);
  133. deviceData.OtherInfo.Add(nameof(PMCMode), PMCMode.Running);
  134. _deviceCollection.DeviceDataList.Add(guid, deviceData);
  135. _deviceCollection.DeviceList.Add(guid, device);
  136. }
  137. }
  138. void ConnectServer()
  139. {
  140. HubBase hubBase = new();
  141. hubBase.Initialize(this._uiProvider);
  142. Task.Factory.StartNew(() =>
  143. {
  144. while (!hubBase.Open("127.0.0.1", 50054, "UIHub"))
  145. Thread.Sleep(1000);
  146. IEnumerable<DeviceInfo> devices = this._uiCaller.RequestDeviceLists().Result;
  147. if (devices is null || !devices.Any())
  148. return;
  149. App.Current.Dispatcher?.Invoke(() =>
  150. {
  151. this._deviceCollection.DeviceList.Clear();
  152. foreach (DeviceInfo device in devices)
  153. {
  154. if (device.Guid is null)
  155. continue;
  156. this._deviceCollection.DeviceList[device.Guid.Value] = device.Adapt<DeviceInfo_VM>();
  157. DeviceData_VM deviceData = new()
  158. {
  159. DeviceId = device.Guid.Value,
  160. DeviceModel = device.DeviceModel,
  161. DeviceStatus = (DeviceStatus.Undefinde).ToString(),
  162. Alarms = []
  163. };
  164. this._deviceCollection.DeviceDataList[device.Guid.Value] = deviceData;
  165. }
  166. });
  167. MessageBox.Show($"Server Connected");
  168. });
  169. }
  170. [ObservableProperty]
  171. private string? _CurrentModule;
  172. [RelayCommand]
  173. private void Open(ContainerInfo para)
  174. {
  175. try
  176. {
  177. _commonContainer.RequestNavigation(para.ModuleName);
  178. this.CurrentModule = para.Name;
  179. }
  180. catch
  181. {
  182. }
  183. }
  184. }