Forráskód Böngészése

EEMSUIClient:
Fix bug: 1. The device information was not saved automatically. 2. The consistency of the guid was not maintained.

SenGao 10 órája%!(EXTRA string=óta)
szülő
commit
07819ebbfe

+ 13 - 32
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -12,7 +12,8 @@ public partial class MainWindowViewModel : ObservableObject
 {
     private readonly IClientService _clientService;
 
-    private readonly string jsonFilePath= $"{Environment.CurrentDirectory}/DeviceInformation.json";
+    private readonly string jsonFilePath = $"{Environment.CurrentDirectory}/DeviceInformation.json";
+    private bool _isInitialized = false;
 
     [ObservableProperty]
     private string _ipAddress = string.Empty;
@@ -96,18 +97,22 @@ public partial class MainWindowViewModel : ObservableObject
             MessageBox.Show("存在未被填写的项目!");
             return;
         }
+
         try
         {
             _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
-            var guid = _clientService.RegisterDevice(new Models.DeviceInfo
+            var deviceInfo = new Models.DeviceInfo
             {
                 DeviceModel = deviceModel,
                 DeviceSubModel = DeviceSubModel,
                 DeviceName = DeviceName,
                 Position = Position,
+                Guid = _isInitialized ? Guid.Parse(GuidStr) : null,
                 SoftwareVersion = SoftwareVersion,
                 DBConnectionString = DBConnectionString
-            });
+            };
+
+            var guid = _clientService.RegisterDevice(deviceInfo);
             if (guid == Guid.Empty)
             {
                 MessageBox.Show("未能获取Guid,设备注册失败!");
@@ -116,34 +121,7 @@ public partial class MainWindowViewModel : ObservableObject
 
             GuidStr = guid.ToString();
             MessageBox.Show("设备注册成功。");
-        }
-        catch (Exception ex)
-        {
-            MessageBox.Show($"遭遇异常:{ex.Message}");
-        }
-    }
-    [RelayCommand]
-    private void Save()
-    {
-        if(string.IsNullOrWhiteSpace(GuidStr) || !Guid.TryParse(GuidStr,out var guidStr))
-        {
-            MessageBox.Show("Guid错误!");
-            return;
-        }
 
-        try
-        {
-            _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
-            var deviceInfo = new Models.DeviceInfo
-            {
-                DeviceModel = deviceModel,
-                DeviceSubModel = DeviceSubModel,
-                DeviceName = DeviceName,
-                Position = Position,
-                SoftwareVersion = SoftwareVersion,
-                Guid = guidStr,
-                DBConnectionString = DBConnectionString
-            };
             string jsonString = JsonSerializer.Serialize(deviceInfo);
             File.WriteAllText(jsonFilePath, jsonString);
         }
@@ -162,18 +140,21 @@ public partial class MainWindowViewModel : ObservableObject
 
         string jsonFromFile = File.ReadAllText(jsonFilePath);
         var deserializedDeviceInfo = JsonSerializer.Deserialize<Models.DeviceInfo>(jsonFromFile);
-        if(deserializedDeviceInfo is null)
+        if (deserializedDeviceInfo is null)
         {
             return;
         }
+
         DeviceModelIndex = (int)deserializedDeviceInfo.DeviceModel!;
-        DeviceModel = deserializedDeviceInfo.DeviceModel.ToString()??string.Empty;
+        DeviceModel = deserializedDeviceInfo.DeviceModel.ToString() ?? string.Empty;
         DeviceSubModel = deserializedDeviceInfo.DeviceSubModel ?? string.Empty;
         DeviceName = deserializedDeviceInfo.DeviceName ?? string.Empty;
         Position = deserializedDeviceInfo.Position ?? string.Empty;
         SoftwareVersion = deserializedDeviceInfo.SoftwareVersion ?? string.Empty;
         GuidStr = deserializedDeviceInfo.Guid.ToString() ?? string.Empty;
         DBConnectionString = deserializedDeviceInfo.DBConnectionString ?? string.Empty;
+
+        _isInitialized = true;
     }
 
     private void OnRequestFileReceived(object? sender, (Guid guid, ServiceBase.FileType fileType) e)

+ 1 - 2
EEMSUIClient/Views/MainWindow.xaml

@@ -53,8 +53,7 @@
             <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding DBConnectionString}" Height="30"/>
             <Label Grid.Row="6" Grid.Column="0" Content="Guid:" HorizontalAlignment="Right" Height="30"/>
             <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding GuidStr}" Height="30"/>
-            <Button Grid.Row="5" Grid.Column="2" Content="Register" Command="{Binding RegisterCommand}" Height="30" Width="100"/>
-            <Button Grid.Row="6" Grid.Column="2" Content="Save" Command="{Binding SaveCommand}" Height="30" Width="100"/>
+            <Button Grid.Row="6" Grid.Column="2" Content="Register" Command="{Binding RegisterCommand}" Height="30" Width="100"/>
         </Grid>
         <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="3,3,3,3" Background="#FFCECECE">