MainWindowViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. namespace EEMSUIClient.ViewModels;
  2. public partial class MainWindowViewModel
  3. (ClientService clientService,
  4. RunningData running,
  5. AddressInfo address,
  6. ConfigService configService,
  7. LocalFilePathInfo localFilePathInfo,
  8. IClientBaseProvider clientBaseProvider) : ObservableObject
  9. {
  10. private Dictionary<string, object> _realtimeData = [];
  11. [ObservableProperty]
  12. private TempTestData _TempTest = new();
  13. [ObservableProperty]
  14. private LocalFilePathInfo _localFilePathInfo = localFilePathInfo;
  15. [ObservableProperty]
  16. private RunningData _Running = running;
  17. [ObservableProperty]
  18. private AddressInfo _Address = address;
  19. [ObservableProperty]
  20. [NotifyPropertyChangedFor(nameof(IsNotConnected))]
  21. private bool _isConnected;
  22. public bool IsNotConnected => !IsConnected;
  23. [RelayCommand]
  24. private void SelectRecipe()
  25. {
  26. OpenFolderDialog dialog = new();
  27. if (dialog.ShowDialog() != true)
  28. return;
  29. this.LocalFilePathInfo.RecipePath = dialog.FolderName;
  30. clientBaseProvider.RecipePath = dialog.FolderName;
  31. if (!configService.SaveDirectoryInfo())
  32. MessageBox.Show("路径保存失败!");
  33. }
  34. [RelayCommand]
  35. private void SelectConfig()
  36. {
  37. OpenFolderDialog dialog = new();
  38. if (dialog.ShowDialog() != true)
  39. return;
  40. this.LocalFilePathInfo.ConfigPath = dialog.FolderName;
  41. clientBaseProvider.ConfigPath = dialog.FolderName;
  42. if (!configService.SaveDirectoryInfo())
  43. MessageBox.Show("路径保存失败!");
  44. }
  45. [RelayCommand]
  46. private void Connect()
  47. {
  48. if (!clientService.Initialize())
  49. {
  50. MessageBox.Show("初始化失败!");
  51. return;
  52. }
  53. IsConnected = true;
  54. if (!configService.SaveConnectionInfo())
  55. MessageBox.Show("设备保存失败!");
  56. }
  57. [RelayCommand]
  58. private void Register()
  59. {
  60. if (string.IsNullOrWhiteSpace(Running.DeviceInfo.DeviceSubModel) ||
  61. string.IsNullOrWhiteSpace(Running.DeviceInfo.DeviceName) ||
  62. string.IsNullOrWhiteSpace(Running.DeviceInfo.Position) ||
  63. string.IsNullOrWhiteSpace(Running.DeviceInfo.SoftwareVersion) ||
  64. string.IsNullOrWhiteSpace(Running.DeviceInfo.DBConnectionString))
  65. {
  66. MessageBox.Show("存在未被填写的项目!");
  67. return;
  68. }
  69. Guid guid = clientService.RegisterDevice(Running.DeviceInfo.Adapt<DeviceInfo>());
  70. if (guid == Guid.Empty)
  71. {
  72. MessageBox.Show("未能获取Guid,设备注册失败!");
  73. return;
  74. }
  75. Running.DeviceInfo.Guid = guid;
  76. MessageBox.Show("设备注册成功。");
  77. if (!configService.SaveDeviceInfo())
  78. MessageBox.Show("设备保存失败!");
  79. }
  80. [RelayCommand]
  81. private void Trigger()
  82. {
  83. _realtimeData.Clear();
  84. _realtimeData.Add("PJobID", TempTest.PJobID);
  85. _realtimeData.Add("CJboID", TempTest.CJobID);
  86. _realtimeData.Add("RecipeType", TempTest.RecipeType.ToString());
  87. _realtimeData.Add("Status", TempTest.DeviceStatus.ToString());
  88. try
  89. {
  90. clientService.UpdateRealTimeData(_realtimeData);
  91. }
  92. catch (Exception ex)
  93. {
  94. MessageBox.Show(ex.Message);
  95. }
  96. }
  97. [RelayCommand]
  98. private void StartDataService()
  99. {
  100. clientService.StartDataCollectionService();
  101. }
  102. }
  103. public partial class TempTestData : ObservableObject
  104. {
  105. [ObservableProperty]
  106. private DeviceStatus _DeviceStatus;
  107. [ObservableProperty]
  108. private string _PJobID = string.Empty;
  109. [ObservableProperty]
  110. private string _CJobID = string.Empty;
  111. [ObservableProperty]
  112. private RecipeType _RecipeType;
  113. }