MainWindowViewModel.cs 5.8 KB

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