MainWindowViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. [ObservableProperty]
  19. private string _ipAddress = string.Empty;
  20. [ObservableProperty]
  21. private string _port = string.Empty;
  22. [ObservableProperty]
  23. private string _hubName = string.Empty;
  24. [ObservableProperty]
  25. [NotifyPropertyChangedFor(nameof(IsNotConnected))]
  26. private bool _isConnected;
  27. [ObservableProperty]
  28. private string _selectedRecipeDict=string.Empty;
  29. [ObservableProperty]
  30. private string _selectedConfigDict = string.Empty;
  31. [ObservableProperty]
  32. private string[] _deviceModels = Enum.GetNames(typeof(DeviceModel));
  33. [ObservableProperty]
  34. private string _deviceModel = string.Empty;
  35. [ObservableProperty]
  36. private int _deviceModelIndex = -1;
  37. [ObservableProperty]
  38. private string _deviceSubModel = string.Empty;
  39. [ObservableProperty]
  40. private string _deviceName = string.Empty;
  41. [ObservableProperty]
  42. private string _position = string.Empty;
  43. [ObservableProperty]
  44. private string _softwareVersion = string.Empty;
  45. [ObservableProperty]
  46. private string _dBConnectionString = string.Empty;
  47. [ObservableProperty]
  48. private string _guidStr = string.Empty;
  49. public MainWindowViewModel(IClientService clientService)
  50. {
  51. _clientService = clientService;
  52. Initialize();
  53. }
  54. public bool IsNotConnected => !IsConnected;
  55. [RelayCommand]
  56. private void SelectRecipe()
  57. {
  58. var dialog = new OpenFolderDialog()
  59. {
  60. Title = "Select Folder"
  61. };
  62. if (dialog.ShowDialog() == true)
  63. {
  64. SelectedRecipeDict = dialog.FolderName;
  65. var service = (IClientBaseProvider)_clientService;
  66. service.RecipePath = SelectedRecipeDict;
  67. }
  68. }
  69. [RelayCommand]
  70. private void SelectConfig()
  71. {
  72. var dialog = new OpenFolderDialog()
  73. {
  74. Title = "Select Folder"
  75. };
  76. if (dialog.ShowDialog() == true)
  77. {
  78. SelectedConfigDict = dialog.FolderName;
  79. var service = (IClientBaseProvider)_clientService;
  80. service.ConfigPath = SelectedConfigDict;
  81. }
  82. }
  83. [RelayCommand]
  84. private void Connect()
  85. {
  86. if (string.IsNullOrWhiteSpace(IpAddress) || string.IsNullOrWhiteSpace(Port) || string.IsNullOrWhiteSpace(HubName))
  87. {
  88. MessageBox.Show("存在未被填写的项目!");
  89. //log
  90. return;
  91. }
  92. if (!_clientService.Initialize(IpAddress, int.Parse(Port), HubName))
  93. {
  94. MessageBox.Show("初始化失败!");
  95. //log
  96. return;
  97. }
  98. IsConnected = true;
  99. var addressInfo = new AddressInfo()
  100. {
  101. Ip= IpAddress,
  102. Port=int.Parse(Port),
  103. HubName=HubName,
  104. };
  105. string jsonString = JsonSerializer.Serialize(addressInfo);
  106. File.WriteAllText(Path.Combine(_settingsFolder, _ipAddressFileName), jsonString);
  107. }
  108. [RelayCommand]
  109. private void Register()
  110. {
  111. if (string.IsNullOrWhiteSpace(DeviceModel) ||
  112. string.IsNullOrWhiteSpace(DeviceSubModel) ||
  113. string.IsNullOrWhiteSpace(DeviceName) ||
  114. string.IsNullOrWhiteSpace(Position) ||
  115. string.IsNullOrWhiteSpace(SoftwareVersion) ||
  116. string.IsNullOrWhiteSpace(DBConnectionString))
  117. {
  118. MessageBox.Show("存在未被填写的项目!");
  119. return;
  120. }
  121. try
  122. {
  123. _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
  124. var rst=Guid.TryParse(GuidStr, out var guidRst);
  125. var deviceInfo = new Models.DeviceInfo
  126. {
  127. DeviceModel = deviceModel,
  128. DeviceSubModel = DeviceSubModel,
  129. DeviceName = DeviceName,
  130. Position = Position,
  131. Guid = rst ? guidRst : 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. }
  185. }
  186. }
  187. }