| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using Device;
- using EEMSUIClientCore;
- using Mapster;
- using ServiceBase;
- using System;
- using System.Windows.Controls;
- namespace EEMSMain.ViewModels;
- public partial class MainWindowViewModel : ObservableObject
- {
- public MainWindowViewModel(ICommonContainer commonContainer, IUIProvider uIProvider, IUICaller uICaller, ContainerManager containerManager, DeviceCollection deviceCollection)
- {
- this._uiProvider = uIProvider;
- this._uiCaller = uICaller;
- this._commonContainer = commonContainer;
- this.ContainerManager = containerManager;
- this._deviceCollection = deviceCollection;
- containerManager.Containers.CollectionChanged += Containers_CollectionChanged;
- //this.FakeData();
- this.ConnectServer();
- }
- private void Containers_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- if (sender is not ObservableDictionary<int, ContainerInfo> t)
- return;
- ContainerInfo? containerInfo = t.Values.FirstOrDefault(x => x.IsDefault);
- if (containerInfo is null)
- return;
- this.Open(containerInfo);
- }
- private readonly IUIProvider _uiProvider;
- private readonly IUICaller _uiCaller;
- private readonly ICommonContainer _commonContainer;
- public ContainerManager ContainerManager { get; }
- private readonly DeviceCollection _deviceCollection;
- void FakeData()
- {
- Random r = new();
- for (int i = 1; i <= 6; i++)
- {
- Guid guid = Guid.NewGuid();
- DeviceInfo_VM device = new()
- {
- DeviceModel = DeviceModel.JetKepler,
- DeviceSubModel = KeplerSubModel.JetKepler_2200A.ToString(),
- DeviceName = $"Device {i}",
- Position = $"position-{i}",
- SoftwareVersion = "1.0.0.0",
- IP = $"192.168.250.{i}",
- Port = 50002,
- UpdateTime = DateTime.Now,
- Guid = guid,
- IsConnected = Convert.ToBoolean(r.Next(0, 2))
- };
- RecipeInfo_VM recipe = new()
- {
- DeviceId = guid,
- CurrentStepName = "Step 1",
- NextStepName = "Step 2",
- CurrentStepTotalTime = r.Next(50, 100),
- CurrentStepRemainTime = r.Next(0, 50),
- TotalTime = r.Next(200, 400),
- TotalRemainTime = r.Next(100, 200),
- };
- //recipe.RecipeInfo["PJobID"] = "TestPJob";
- //recipe.RecipeInfo["CJboID"] = "TestCJob";
- //recipe.RecipeInfo["RecipeType"] = (RecipeType)r.Next(0, 8);
- DeviceData_VM deviceData = new()
- {
- DeviceId = guid,
- DeviceModel = DeviceModel.JetKepler,
- DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
- RecipeInfo = recipe,
- Alarms = []
- };
- deviceData.OtherInfo.Add("子型号", KeplerSubModel.JetKepler_2200A);
- //deviceData.OtherInfo["PMC Mode"] = PMCMode.Running.ToString();
- if (r.Next(1, 4) > 1)
- {
- for (int j = 0; j < r.Next(1, 5); j++)
- {
- Alarm_VM alarm = new()
- {
- DeviceId = guid,
- AlarmCode = r.Next(10000, 90000).ToString(),
- AlarmTime = DateTime.Now,
- AlarmName = $"Test Alarm {j}"
- };
- deviceData.Alarms.Add(alarm);
- }
- }
- _deviceCollection.DeviceDataList.Add(guid, deviceData);
- _deviceCollection.DeviceList.Add(guid, device);
- }
- for (int i = 1; i <= 3; i++)
- {
- Guid guid = Guid.NewGuid();
- DeviceInfo_VM device = new()
- {
- DeviceModel = DeviceModel.Proxima,
- DeviceSubModel = ProximaSubModel.Proxima_ELK.ToString(),
- DeviceName = $"Device {i}",
- Position = $"position-{i}",
- SoftwareVersion = "1.0.0.0",
- IP = $"192.168.250.{i}",
- Port = 50002,
- UpdateTime = DateTime.Now,
- Guid = guid,
- IsConnected = Convert.ToBoolean(r.Next(0, 2))
- };
- RecipeInfo_VM recipe = new()
- {
- DeviceId = guid,
- CurrentStepName = "Step 1",
- NextStepName = "Step 2",
- CurrentStepTotalTime = r.Next(50, 100),
- CurrentStepRemainTime = r.Next(0, 50),
- TotalTime = r.Next(200, 400),
- TotalRemainTime = r.Next(100, 200),
- };
- recipe.RecipeInfo.Add("PJobID", "TestPJob");
- recipe.RecipeInfo.Add("CJboID", "TestCJob");
- recipe.RecipeInfo.Add("RecipeType", (RecipeType)r.Next(0, 8));
- DeviceData_VM deviceData = new()
- {
- DeviceId = guid,
- DeviceModel = DeviceModel.Proxima,
- DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
- //PMCMode = PMCMode.Running,
- RecipeInfo = recipe,
- Alarms = []
- };
- deviceData.OtherInfo.Add("子型号", ProximaSubModel.Proxima_ELK);
- deviceData.OtherInfo.Add(nameof(PMCMode), PMCMode.Running);
- _deviceCollection.DeviceDataList.Add(guid, deviceData);
- _deviceCollection.DeviceList.Add(guid, device);
- }
- }
- void ConnectServer()
- {
- HubBase hubBase = new();
- hubBase.Initialize(this._uiProvider);
- Task.Factory.StartNew(() =>
- {
- while (!hubBase.Open("127.0.0.1", 50054, "UIHub"))
- Thread.Sleep(1000);
- IEnumerable<DeviceInfo> devices = this._uiCaller.RequestDeviceLists().Result;
- if (devices is null || !devices.Any())
- return;
- App.Current.Dispatcher?.Invoke(() =>
- {
- this._deviceCollection.DeviceList.Clear();
- foreach (DeviceInfo device in devices)
- {
- if (device.Guid is null)
- continue;
- this._deviceCollection.DeviceList[device.Guid.Value] = device.Adapt<DeviceInfo_VM>();
- DeviceData_VM deviceData = new()
- {
- DeviceId = device.Guid.Value,
- DeviceModel = device.DeviceModel,
- DeviceStatus = (DeviceStatus.Undefinde).ToString(),
- Alarms = []
- };
- this._deviceCollection.DeviceDataList[device.Guid.Value] = deviceData;
- }
- });
- MessageBox.Show($"Server Connected");
- });
- }
- [ObservableProperty]
- private string? _CurrentModule;
- [RelayCommand]
- private void Open(ContainerInfo para)
- {
- try
- {
- _commonContainer.RequestNavigation(para.ModuleName);
- this.CurrentModule = para.Name;
- }
- catch
- {
- }
- }
- }
|