MainWindowViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using EEMSClientCore;
  4. using EEMSUIClient.Models;
  5. using EEMSUIClient.Services;
  6. using GeneralData;
  7. using Microsoft.Win32;
  8. using System.IO;
  9. using System.Text.Json;
  10. using System.Windows;
  11. namespace EEMSUIClient.ViewModels;
  12. public partial class MainWindowViewModel : ObservableObject
  13. {
  14. private readonly IClientService _clientService;
  15. private readonly string _settingsFolder = $"{Environment.CurrentDirectory}/Settings";
  16. private readonly string _ipAddressFileName = "IpAddressInformation.json";
  17. private readonly string _deviceInfoFileName = "DeviceInformation.json";
  18. private bool _isInitialized = false;
  19. [ObservableProperty]
  20. private string _ipAddress = string.Empty;
  21. [ObservableProperty]
  22. private string _port = string.Empty;
  23. [ObservableProperty]
  24. private string _hubName = string.Empty;
  25. [ObservableProperty]
  26. [NotifyPropertyChangedFor(nameof(IsNotConnected))]
  27. private bool _isConnected;
  28. [ObservableProperty]
  29. private string _selectedRecipeDict=string.Empty;
  30. [ObservableProperty]
  31. private string _selectedConfigDict = string.Empty;
  32. [ObservableProperty]
  33. private string[] _deviceModels = Enum.GetNames(typeof(DeviceModel));
  34. [ObservableProperty]
  35. private string _deviceModel = string.Empty;
  36. [ObservableProperty]
  37. private int _deviceModelIndex = -1;
  38. [ObservableProperty]
  39. private string _deviceSubModel = string.Empty;
  40. [ObservableProperty]
  41. private string _deviceName = string.Empty;
  42. [ObservableProperty]
  43. private string _position = string.Empty;
  44. [ObservableProperty]
  45. private string _softwareVersion = string.Empty;
  46. [ObservableProperty]
  47. private string _dBConnectionString = string.Empty;
  48. [ObservableProperty]
  49. private string _guidStr = string.Empty;
  50. public MainWindowViewModel(IClientService clientService)
  51. {
  52. _clientService = clientService;
  53. Initialize();
  54. }
  55. public bool IsNotConnected => !IsConnected;
  56. [RelayCommand]
  57. private void SelectRecipe()
  58. {
  59. var dialog = new OpenFolderDialog()
  60. {
  61. Title = "Select Folder"
  62. };
  63. if (dialog.ShowDialog() == true)
  64. {
  65. SelectedRecipeDict = dialog.FolderName;
  66. var service = (IClientBaseProvider)_clientService;
  67. service.RecipePath = SelectedRecipeDict;
  68. }
  69. }
  70. [RelayCommand]
  71. private void SelectConfig()
  72. {
  73. var dialog = new OpenFolderDialog()
  74. {
  75. Title = "Select Folder"
  76. };
  77. if (dialog.ShowDialog() == true)
  78. {
  79. SelectedConfigDict = dialog.FolderName;
  80. var service = (IClientBaseProvider)_clientService;
  81. service.ConfigPath = SelectedConfigDict;
  82. }
  83. }
  84. [RelayCommand]
  85. private void Connect()
  86. {
  87. if (string.IsNullOrWhiteSpace(IpAddress) || string.IsNullOrWhiteSpace(Port) || string.IsNullOrWhiteSpace(HubName))
  88. {
  89. MessageBox.Show("存在未被填写的项目!");
  90. //log
  91. return;
  92. }
  93. if (!_clientService.Initialize(IpAddress, int.Parse(Port), HubName))
  94. {
  95. MessageBox.Show("初始化失败!");
  96. //log
  97. return;
  98. }
  99. IsConnected = true;
  100. var addressInfo = new AddressInfo()
  101. {
  102. Ip= IpAddress,
  103. Port=int.Parse(Port),
  104. HubName=HubName,
  105. };
  106. string jsonString = JsonSerializer.Serialize(addressInfo);
  107. File.WriteAllText(Path.Combine(_settingsFolder, _ipAddressFileName), jsonString);
  108. }
  109. [RelayCommand]
  110. private void Register()
  111. {
  112. if (string.IsNullOrWhiteSpace(DeviceModel) ||
  113. string.IsNullOrWhiteSpace(DeviceSubModel) ||
  114. string.IsNullOrWhiteSpace(DeviceName) ||
  115. string.IsNullOrWhiteSpace(Position) ||
  116. string.IsNullOrWhiteSpace(SoftwareVersion) ||
  117. string.IsNullOrWhiteSpace(DBConnectionString))
  118. {
  119. MessageBox.Show("存在未被填写的项目!");
  120. return;
  121. }
  122. try
  123. {
  124. _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
  125. var deviceInfo = new Models.DeviceInfo
  126. {
  127. DeviceModel = deviceModel,
  128. DeviceSubModel = DeviceSubModel,
  129. DeviceName = DeviceName,
  130. Position = Position,
  131. Guid = _isInitialized ? Guid.Parse(GuidStr) : null,
  132. SoftwareVersion = SoftwareVersion,
  133. DBConnectionString = DBConnectionString
  134. };
  135. var guid = _clientService.RegisterDevice(deviceInfo);
  136. if (guid == Guid.Empty)
  137. {
  138. MessageBox.Show("未能获取Guid,设备注册失败!");
  139. return;
  140. }
  141. GuidStr = guid.ToString();
  142. MessageBox.Show("设备注册成功。");
  143. var jsonString = JsonSerializer.Serialize(deviceInfo);
  144. File.WriteAllText(Path.Combine(_settingsFolder, _deviceInfoFileName), jsonString);
  145. }
  146. catch (Exception ex)
  147. {
  148. MessageBox.Show($"遭遇异常:{ex.Message}");
  149. }
  150. }
  151. private void Initialize()
  152. {
  153. if (!Directory.Exists(_settingsFolder))
  154. {
  155. Directory.CreateDirectory(_settingsFolder);
  156. }
  157. var ipAddressFilePath = Path.Combine(_settingsFolder, _ipAddressFileName);
  158. if (Path.Exists(ipAddressFilePath))
  159. {
  160. string jsonFromFile = File.ReadAllText(ipAddressFilePath);
  161. var deserializedAddressInfo = JsonSerializer.Deserialize<AddressInfo>(jsonFromFile);
  162. if (deserializedAddressInfo is not null)
  163. {
  164. IpAddress = deserializedAddressInfo.Ip;
  165. Port = deserializedAddressInfo.Port.ToString();
  166. HubName = deserializedAddressInfo.HubName;
  167. }
  168. }
  169. var deviceInfoFilePath = Path.Combine(_settingsFolder, _deviceInfoFileName);
  170. if (Path.Exists(deviceInfoFilePath))
  171. {
  172. string jsonFromFile = File.ReadAllText(deviceInfoFilePath);
  173. var deserializedDeviceInfo = JsonSerializer.Deserialize<DeviceInfo>(jsonFromFile);
  174. if (deserializedDeviceInfo is not null)
  175. {
  176. DeviceModelIndex = (int)deserializedDeviceInfo.DeviceModel!;
  177. DeviceModel = deserializedDeviceInfo.DeviceModel.ToString() ?? string.Empty;
  178. DeviceSubModel = deserializedDeviceInfo.DeviceSubModel ?? string.Empty;
  179. DeviceName = deserializedDeviceInfo.DeviceName ?? string.Empty;
  180. Position = deserializedDeviceInfo.Position ?? string.Empty;
  181. SoftwareVersion = deserializedDeviceInfo.SoftwareVersion ?? string.Empty;
  182. GuidStr = deserializedDeviceInfo.Guid.ToString() ?? string.Empty;
  183. DBConnectionString = deserializedDeviceInfo.DBConnectionString ?? string.Empty;
  184. _isInitialized = true;
  185. }
  186. }
  187. }
  188. }