Преглед изворни кода

EEMSUIClient:
Feature:
1. Add the function to save remote ip information.
2. Adapt the modified interfaces.

SenGao пре 12 часа
родитељ
комит
e7f226817d

+ 15 - 0
EEMSUIClient/Models/AddressInfo.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EEMSUIClient.Models
+{
+    public class AddressInfo
+    {
+        public string Ip { get; set; } = string.Empty;
+        public int Port { get; set; }
+        public string HubName { get; set; } = string.Empty;
+    }
+}

+ 1 - 1
EEMSUIClient/Models/DeviceInfo.cs

@@ -24,7 +24,7 @@ public class DeviceInfo
             Position = this.Position,
             SoftwareVersion = this.SoftwareVersion,
             Guid = this.Guid,
-            IP = this.IP,
+            IP = this.IP ?? string.Empty,
             Port = this.Port,
             DBConnectionString = this.DBConnectionString
         };

+ 6 - 12
EEMSUIClient/Services/ClientService.cs

@@ -3,7 +3,7 @@ using ServiceBase;
 
 namespace EEMSUIClient.Services
 {
-    public class ClientService : IClientService, IClientProvider
+    public class ClientService : IClientService, IClientBaseProvider
     {
         private readonly IClientCaller _clientCaller;
         private readonly HubBase _hubBase;
@@ -12,11 +12,12 @@ namespace EEMSUIClient.Services
 
         public ClientService()
         {
-            _clientCaller = new ClientCaller();
-            _hubBase = new HubBase();
+            _hubBase = new HubBase(this);
+            _clientCaller = _hubBase;
         }
 
-        public event EventHandler<(Guid guid, FileType fileType)>? RequestFileReceived;
+        public string? RecipePath { get; set; }
+        public string? ConfigPath { get; set; }
 
         public bool Initialize(string ip, int port, string hub)
         {
@@ -26,7 +27,7 @@ namespace EEMSUIClient.Services
                 return false;
             }
 
-            if (!_hubBase.Initialize(this))
+            if (!_hubBase.Initialize())
             {
                 //log
                 return false;
@@ -46,13 +47,6 @@ namespace EEMSUIClient.Services
             return _clientCaller.RegisterDevice(deviceInfo.Convert()).Result;
         }
 
-        public Task<bool> RequestFile(Guid guid, FileType fileType)
-        {
-            //log
-            RequestFileReceived?.Invoke(null, (guid, fileType));
-            return Task.FromResult(true);
-        }
-
         protected virtual void Dispose(bool disposing)
         {
             if (!disposedValue)

+ 0 - 2
EEMSUIClient/Services/IClientService.cs

@@ -8,7 +8,5 @@ namespace EEMSUIClient.Services
         bool Initialize(string ip, int port, string hub);
 
         Guid RegisterDevice(Models.DeviceInfo deviceInfo);
-
-        event EventHandler<(Guid guid, FileType fileType)>? RequestFileReceived;
     }
 }

+ 38 - 8
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -1,5 +1,6 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
+using EEMSUIClient.Models;
 using EEMSUIClient.Services;
 using GeneralData;
 using System.IO;
@@ -12,7 +13,8 @@ public partial class MainWindowViewModel : ObservableObject
 {
     private readonly IClientService _clientService;
 
-    private readonly string jsonFilePath = $"{Environment.CurrentDirectory}/DeviceInformation.json";
+    private readonly string _ipAddressFilePath = $"{Environment.CurrentDirectory}/IpAddressInformation.json";
+    private readonly string _deviceInfoFilePath = $"{Environment.CurrentDirectory}/DeviceInformation.json";
     private bool _isInitialized = false;
 
     [ObservableProperty]
@@ -58,8 +60,8 @@ public partial class MainWindowViewModel : ObservableObject
     public MainWindowViewModel(IClientService clientService)
     {
         _clientService = clientService;
-        _clientService.RequestFileReceived += OnRequestFileReceived;
-        Initialize();
+        InitializeIpAddress();
+        InitializeDeviceInfo();
     }
 
     public bool IsNotConnected => !IsConnected;
@@ -82,6 +84,15 @@ public partial class MainWindowViewModel : ObservableObject
         }
 
         IsConnected = true;
+
+        var addressInfo = new AddressInfo()
+        {
+            Ip= IpAddress,
+            Port=int.Parse(Port),
+            HubName=HubName,
+        };
+        string jsonString = JsonSerializer.Serialize(addressInfo);
+        File.WriteAllText(_ipAddressFilePath, jsonString);
     }
 
     [RelayCommand]
@@ -122,8 +133,8 @@ public partial class MainWindowViewModel : ObservableObject
             GuidStr = guid.ToString();
             MessageBox.Show("设备注册成功。");
 
-            string jsonString = JsonSerializer.Serialize(deviceInfo);
-            File.WriteAllText(jsonFilePath, jsonString);
+            var jsonString = JsonSerializer.Serialize(deviceInfo);
+            File.WriteAllText(_deviceInfoFilePath, jsonString);
         }
         catch (Exception ex)
         {
@@ -131,14 +142,33 @@ public partial class MainWindowViewModel : ObservableObject
         }
     }
 
-    private void Initialize()
+    private void InitializeIpAddress()
+    {
+        if (!Path.Exists(_ipAddressFilePath))
+        {
+            return;
+        }
+
+        string jsonFromFile = File.ReadAllText(_ipAddressFilePath);
+        var deserializedAddressInfo = JsonSerializer.Deserialize<AddressInfo>(jsonFromFile);
+        if (deserializedAddressInfo is null)
+        {
+            return;
+        }
+
+        IpAddress = deserializedAddressInfo.Ip;
+        Port = deserializedAddressInfo.Port.ToString();
+        HubName = deserializedAddressInfo.HubName;
+    }
+
+    private void InitializeDeviceInfo()
     {
-        if (!Path.Exists(jsonFilePath))
+        if (!Path.Exists(_deviceInfoFilePath))
         {
             return;
         }
 
-        string jsonFromFile = File.ReadAllText(jsonFilePath);
+        string jsonFromFile = File.ReadAllText(_deviceInfoFilePath);
         var deserializedDeviceInfo = JsonSerializer.Deserialize<Models.DeviceInfo>(jsonFromFile);
         if (deserializedDeviceInfo is null)
         {