MainWindowViewModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. private bool _isInitialized = false;
  14. [ObservableProperty]
  15. private string _ipAddress = string.Empty;
  16. [ObservableProperty]
  17. private string _port = string.Empty;
  18. [ObservableProperty]
  19. private string _hubName = string.Empty;
  20. [ObservableProperty]
  21. [NotifyPropertyChangedFor(nameof(IsNotConnected))]
  22. private bool _isConnected;
  23. [ObservableProperty]
  24. private string[] _deviceModels = Enum.GetNames(typeof(DeviceModel));
  25. [ObservableProperty]
  26. private string _deviceModel = string.Empty;
  27. [ObservableProperty]
  28. private int _deviceModelIndex = -1;
  29. [ObservableProperty]
  30. private string _deviceSubModel = string.Empty;
  31. [ObservableProperty]
  32. private string _deviceName = string.Empty;
  33. [ObservableProperty]
  34. private string _position = string.Empty;
  35. [ObservableProperty]
  36. private string _softwareVersion = string.Empty;
  37. [ObservableProperty]
  38. private string _dBConnectionString = string.Empty;
  39. [ObservableProperty]
  40. private string _guidStr = string.Empty;
  41. public MainWindowViewModel(IClientService clientService)
  42. {
  43. _clientService = clientService;
  44. _clientService.RequestFileReceived += OnRequestFileReceived;
  45. Initialize();
  46. }
  47. public bool IsNotConnected => !IsConnected;
  48. [RelayCommand]
  49. private void Connect()
  50. {
  51. if (string.IsNullOrWhiteSpace(IpAddress) || string.IsNullOrWhiteSpace(Port) || string.IsNullOrWhiteSpace(HubName))
  52. {
  53. MessageBox.Show("存在未被填写的项目!");
  54. //log
  55. return;
  56. }
  57. if (!_clientService.Initialize(IpAddress, int.Parse(Port), HubName))
  58. {
  59. MessageBox.Show("初始化失败!");
  60. //log
  61. return;
  62. }
  63. IsConnected = true;
  64. }
  65. [RelayCommand]
  66. private void Register()
  67. {
  68. if (string.IsNullOrWhiteSpace(DeviceModel) ||
  69. string.IsNullOrWhiteSpace(DeviceSubModel) ||
  70. string.IsNullOrWhiteSpace(DeviceName) ||
  71. string.IsNullOrWhiteSpace(Position) ||
  72. string.IsNullOrWhiteSpace(SoftwareVersion) ||
  73. string.IsNullOrWhiteSpace(DBConnectionString))
  74. {
  75. MessageBox.Show("存在未被填写的项目!");
  76. return;
  77. }
  78. try
  79. {
  80. _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
  81. var deviceInfo = new Models.DeviceInfo
  82. {
  83. DeviceModel = deviceModel,
  84. DeviceSubModel = DeviceSubModel,
  85. DeviceName = DeviceName,
  86. Position = Position,
  87. Guid = _isInitialized ? Guid.Parse(GuidStr) : null,
  88. SoftwareVersion = SoftwareVersion,
  89. DBConnectionString = DBConnectionString
  90. };
  91. var guid = _clientService.RegisterDevice(deviceInfo);
  92. if (guid == Guid.Empty)
  93. {
  94. MessageBox.Show("未能获取Guid,设备注册失败!");
  95. return;
  96. }
  97. GuidStr = guid.ToString();
  98. MessageBox.Show("设备注册成功。");
  99. string jsonString = JsonSerializer.Serialize(deviceInfo);
  100. File.WriteAllText(jsonFilePath, jsonString);
  101. }
  102. catch (Exception ex)
  103. {
  104. MessageBox.Show($"遭遇异常:{ex.Message}");
  105. }
  106. }
  107. private void Initialize()
  108. {
  109. if (!Path.Exists(jsonFilePath))
  110. {
  111. return;
  112. }
  113. string jsonFromFile = File.ReadAllText(jsonFilePath);
  114. var deserializedDeviceInfo = JsonSerializer.Deserialize<Models.DeviceInfo>(jsonFromFile);
  115. if (deserializedDeviceInfo is null)
  116. {
  117. return;
  118. }
  119. DeviceModelIndex = (int)deserializedDeviceInfo.DeviceModel!;
  120. DeviceModel = deserializedDeviceInfo.DeviceModel.ToString() ?? string.Empty;
  121. DeviceSubModel = deserializedDeviceInfo.DeviceSubModel ?? string.Empty;
  122. DeviceName = deserializedDeviceInfo.DeviceName ?? string.Empty;
  123. Position = deserializedDeviceInfo.Position ?? string.Empty;
  124. SoftwareVersion = deserializedDeviceInfo.SoftwareVersion ?? string.Empty;
  125. GuidStr = deserializedDeviceInfo.Guid.ToString() ?? string.Empty;
  126. DBConnectionString = deserializedDeviceInfo.DBConnectionString ?? string.Empty;
  127. _isInitialized = true;
  128. }
  129. private void OnRequestFileReceived(object? sender, (Guid guid, ServiceBase.FileType fileType) e)
  130. {
  131. //log
  132. }
  133. }