MainWindowViewModel.cs 7.4 KB

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