MainWindowViewModel.cs 5.5 KB

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