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

Update UI=>Server=>Clilent

Zixuan 3 nap óta
szülő
commit
c5d5e7071b

+ 1 - 1
Data/Device/DeviceInfo.cs

@@ -13,7 +13,7 @@ public class DeviceInfo
     public string? Position { get; set; }
     public string? SoftwareVersion { get; set; }
     public string IP { get; set; } = string.Empty;
-    public ushort Port { get; set; }
+    public int Port { get; set; }
     public string? DBConnectionString { get; set; }
     public DateTime UpdateTime { get; set; }
     [SugarColumn(IsIgnore = true)]

+ 19 - 0
DataBase/SqlSugarORM/SqlSugarCustom.cs

@@ -318,6 +318,25 @@ public class SqlSugarCustom
         return true;
     }
 
+
+    public bool Update<T>(string tableName, T data) where T : class, new()
+    {
+        if (this.Client is null)
+            return false;
+
+        try
+        {
+            if (string.IsNullOrEmpty(tableName))
+                return this.Client.Updateable(data).ExecuteCommand() != 0;
+            else
+                return this.Client.Updateable(data).AS(tableName).ExecuteCommand() != 0;
+        }
+        catch
+        {
+            return false;
+        }
+    }
+
     #region Disopse
     protected virtual void Dispose(bool disposing)
     {

+ 1 - 1
Data_ViewModel/GlobalData/DeviceInfo.cs

@@ -17,7 +17,7 @@ public partial class DeviceInfo_VM : ObservableObject
     [ObservableProperty]
     private string? _IP;
     [ObservableProperty]
-    private ushort _Port;
+    private int _Port;
     [ObservableProperty]
     private string? _DBConnectionString;
 

+ 27 - 12
EEMSMain/Service/UIProvider.cs

@@ -24,21 +24,36 @@ internal class UIProvider(DeviceCollection deviceCollection) : IUIProvider
 
     Task<bool> IUIProvider.UpdateDevice(DeviceInfo deviceInfo)
     {
-        DeviceInfo_VM device = deviceInfo.Adapt<DeviceInfo_VM>();
-
-        if (device.Guid is null)
+        if (deviceInfo.Guid is null)
             return Task.FromResult(true);
-        DeviceData_VM deviceData = new()
-        {
-            DeviceId = device.Guid.Value,
-            DeviceModel = device.DeviceModel,
-            OtherInfo = [],
-            Alarms = []
-        };
+
         App.Current.Dispatcher.Invoke(() =>
         {
-            deviceCollection.DeviceDataList[device.Guid.Value] = deviceData;
-            deviceCollection.DeviceList[device.Guid.Value] = device;
+            if (!deviceCollection.DeviceList.TryGetValue(deviceInfo.Guid.Value, out DeviceInfo_VM? device) || device is null)
+            {
+                device = deviceInfo.Adapt<DeviceInfo_VM>();
+                deviceCollection.DeviceList[deviceInfo.Guid.Value] = device;
+            }
+            else
+            {
+                deviceInfo.Adapt(device);
+            }
+
+            if (!deviceCollection.DeviceDataList.TryGetValue(deviceInfo.Guid.Value, out DeviceData_VM? deviceData) || deviceData is null)
+            {
+                deviceData = new()
+                {
+                    DeviceId = deviceInfo.Guid.Value,
+                    DeviceModel = device.DeviceModel,
+                    OtherInfo = [],
+                    Alarms = []
+                };
+                deviceCollection.DeviceDataList[deviceInfo.Guid.Value] = deviceData;
+            }
+            else
+            {
+                deviceData.DeviceModel = device.DeviceModel;
+            }
         });
 
         return Task.FromResult(true);

+ 2 - 2
EEMSMain/ViewModels/MainWindowViewModel.cs

@@ -169,7 +169,7 @@ public partial class MainWindowViewModel : ObservableObject
             IEnumerable<DeviceInfo> devices = this._uiCaller.RequestDeviceLists().Result;
 
             if (devices is null || !devices.Any())
-                return;
+                goto End;
 
             App.Current.Dispatcher?.Invoke(() =>
             {
@@ -192,7 +192,7 @@ public partial class MainWindowViewModel : ObservableObject
 
                 }
             });
-
+        End:
             MessageBox.Show($"Server Connected");
         });
 

+ 3 - 30
EEMSUIClient/Services/ClientService.cs

@@ -111,37 +111,10 @@ public class ClientService(
         }
     }
 
-
-    #region Dispose
-    private bool disposedValue;
-    protected virtual void Dispose(bool disposing)
-    {
-        if (!disposedValue)
-        {
-            if (disposing)
-            {
-                // TODO: dispose managed state (managed objects)
-                clientCaller = null;
-            }
-
-            // TODO: free unmanaged resources (unmanaged objects) and override finalizer
-            // TODO: set large fields to null
-            disposedValue = true;
-        }
-    }
-
-    // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
-    // ~ClientService()
-    // {
-    //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
-    //     Dispose(disposing: false);
-    // }
-
     public void Dispose()
     {
-        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
-        Dispose(disposing: true);
-        GC.SuppressFinalize(this);
+        clientCaller?.Dispose();
+        clientCaller = null;
     }
-    #endregion
+
 }

+ 7 - 0
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -71,6 +71,13 @@ public partial class MainWindowViewModel
     }
 
     [RelayCommand]
+    private void Disconnect()
+    {
+        clientService?.Dispose();
+        IsConnected = false;
+    }
+
+    [RelayCommand]
     private void Register()
     {
         if (string.IsNullOrWhiteSpace(Running.DeviceInfo.DeviceSubModel) ||

+ 9 - 3
EEMSUIClient/Views/MainWindow.xaml

@@ -40,8 +40,13 @@
             <RowDefinition Height="4"/>
             <RowDefinition Height="*"/>
         </Grid.RowDefinitions>
-        <Border Grid.Row="0" Background="#FFCECECE" IsEnabled="{Binding IsNotConnected}">
-            <Grid Margin="8">
+        <Grid Grid.Row="0" Background="#FFCECECE">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition/>
+                <ColumnDefinition Width="0"/>
+                <ColumnDefinition Width="auto"/>
+            </Grid.ColumnDefinitions>
+            <Grid Margin="8"  IsEnabled="{Binding IsNotConnected}">
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="auto"/>
                     <ColumnDefinition Width="8"/>
@@ -73,7 +78,8 @@
                 <Button Grid.Column="12" Content="Connect" Command="{Binding ConnectCommand}"/>
 
             </Grid>
-        </Border>
+            <Button Grid.Column="2" Width="120" Margin="8" Command="{Binding DisconnectCommand}">Disconnect</Button>
+        </Grid>
 
         <Border Grid.Row="2" Background="#FFCECECE">
             <Grid Margin="8" Height="30">

+ 14 - 4
Module/DeviceManagement/ViewModels/Dialog/EditDeviceViewModel.cs

@@ -1,8 +1,11 @@
-using Mapster;
+using Device;
+using Mapster;
+using ServiceBase;
+using System.Threading.Tasks;
 
 namespace DeviceManagement.ViewModels.Dialog;
 
-internal partial class EditDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware
+internal partial class EditDeviceViewModel(DeviceCollection deviceCollection, IUICaller uICaller) : ObservableObject, IDialogAware
 {
     public DialogCloseListener RequestClose { get; set; }
     [ObservableProperty]
@@ -34,9 +37,9 @@ internal partial class EditDeviceViewModel(DeviceCollection deviceCollection) :
     }
 
     [RelayCommand]
-    private void SaveDevice()
+    private async Task SaveDevice()
     {
-        if (this.TempDevice is null||this.TempDevice.Guid is null)
+        if (this.TempDevice is null || this.TempDevice.Guid is null)
         {
             MessageBox.Show("Error");
             return;
@@ -48,7 +51,14 @@ internal partial class EditDeviceViewModel(DeviceCollection deviceCollection) :
             return;
         }
 
+        if (!uICaller.UpdateDeviceInfo(this.TempDevice.Adapt<DeviceInfo>()).Result)
+        {
+            MessageBox.Show("Device Offline");
+            return;
+        }
+
         this.TempDevice.Adapt(device);
+
         MessageBox.Show("设备信息修改成功!", "设备信息", MessageBoxButton.OK, MessageBoxImage.Information);
         this.RequestClose.Invoke();
 

+ 1 - 1
Server/EEMSClientCore/ClientCaller.cs

@@ -36,6 +36,6 @@ public partial class ClientCaller : IClientCaller
 
     void IDisposable.Dispose()
     {
-        throw new NotImplementedException();
+        _HubConnection?.DisposeAsync().AsTask().Wait();
     }
 }

+ 1 - 0
Server/EEMSClientCore/HubBase.cs

@@ -29,6 +29,7 @@ public partial class ClientCaller(IClientBaseProvider baseProvider)
 
         temp.On<Guid, FileType>("RequestFile", _clientProvider.RequestFile);
         temp.On<Guid, FileType, byte[], int, int>("PushFile", _clientProvider.PushFile);
+        temp.On<Guid, DeviceInfo>("UpdateDeviceInfo", _clientProvider.UpdateDeviceInfo);
 
         for (int i = 1; i <= retry; i++)
         {

+ 5 - 5
Server/EEMSService/HubSender/ClientCaller.cs

@@ -8,7 +8,7 @@ public class ClientCaller : IClientProvider
     {
         if (string.IsNullOrEmpty(name))
             return Task.FromResult(false);
-        if (ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
+        if (!ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
             return Task.FromResult(false);
 
         try
@@ -27,7 +27,7 @@ public class ClientCaller : IClientProvider
         if (string.IsNullOrEmpty(name))
             return Task.FromResult(false);
 
-        if (ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
+        if (!ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
             return Task.FromResult(false);
 
         try
@@ -65,7 +65,7 @@ public class ClientCaller : IClientProvider
         if (string.IsNullOrEmpty(name))
             return Task.FromResult(false);
 
-        if (ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
+        if (!ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
             return Task.FromResult(false);
 
         try
@@ -84,7 +84,7 @@ public class ClientCaller : IClientProvider
         if (string.IsNullOrEmpty(name))
             return Task.FromResult(false);
 
-        if (ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
+        if (!ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
             return Task.FromResult(false);
 
         try
@@ -130,6 +130,6 @@ public class ClientCaller : IClientProvider
 
     Task<bool> IClientProvider.UpdateDeviceInfo(Guid guid, DeviceInfo deviceInfo)
     {
-        return this.SendAsync(guid, "UpdateDeviceInfo", deviceInfo);
+        return this.SendAsync(guid, "UpdateDeviceInfo", guid, deviceInfo);
     }
 }

+ 2 - 1
Server/EEMSService/Hubs/ClientsMainHub.cs

@@ -20,7 +20,7 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
 
     public override Task OnDisconnectedAsync(Exception? exception)
     {
-        deviceManager.RemoveDevice(Context.ConnectionId, out DeviceInfo? device);
+        deviceManager.TryGetDevice(Context.ConnectionId, out DeviceInfo? device);
 
         if (device is not null && device.Guid.HasValue)
         {
@@ -44,6 +44,7 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
         if (!deviceManager.LoginDevice(Context.ConnectionId, deviceInfo, Clients.Caller, out bool newDevice))
             return Task.FromResult(Guid.Empty);
 
+        deviceInfo.IsConnected = true;
         if (newDevice)
             uiCaller.InsertNewDevice(deviceInfo);
         else

+ 12 - 3
Server/EEMSService/Hubs/UIHub.cs

@@ -1,6 +1,6 @@
 namespace EEMSServerCore.Hubs;
 
-internal class UIHub(DeviceManager deviceManager, ClientCaller clientCaller) : Hub, IUICaller
+internal class UIHub(DeviceManager deviceManager, IClientProvider clientProvider) : Hub, IUICaller
 {
     public override Task OnConnectedAsync()
     {
@@ -22,11 +22,20 @@ internal class UIHub(DeviceManager deviceManager, ClientCaller clientCaller) : H
         IEnumerable<DeviceInfo> results = devices.Values;
 
         return Task.FromResult(results);
-
     }
 
     public Task<bool> UpdateDeviceInfo(DeviceInfo deviceInfo)
     {
-        return Task.FromResult(true);
+        if (!deviceInfo.Guid.HasValue)
+            return Task.FromResult(false);
+        try
+        {
+            deviceManager.LoginDevice(deviceInfo, out _);
+            return clientProvider.UpdateDeviceInfo(deviceInfo.Guid.Value, deviceInfo);
+        }
+        catch
+        {
+            return Task.FromResult(false);
+        }
     }
 }

+ 6 - 2
Server/EEMSService/Managers/DeviceManager.cs

@@ -25,11 +25,14 @@ internal class DeviceManager(SqlSugarCustom orm)
         isNewDevice = false;
         if (!deviceInfo.Guid.HasValue)
             return false;
+
+        if (!orm.InsertOrUpdate(EEMSCenterConfig.DeviceTableName, deviceInfo, t => Guid.Equals(t.Guid, deviceInfo.Guid), out isNewDevice))
+            return false;
+
         DevicesFromGuid[deviceInfo.Guid.Value] = deviceInfo;
         DevicesFromConnectionID[connectionID] = deviceInfo;
         ClientManager.DeviceClients[deviceInfo.Guid.Value] = singleClient;
-
-        return orm.InsertOrUpdate(EEMSCenterConfig.DeviceTableName, deviceInfo, t => t.Guid == deviceInfo.Guid, out isNewDevice);
+        return true;
     }
 
     public bool LoginDevice(DeviceInfo deviceInfo, out bool isNewDevice)
@@ -38,6 +41,7 @@ internal class DeviceManager(SqlSugarCustom orm)
         if (!deviceInfo.Guid.HasValue)
             return false;
         DevicesFromGuid[deviceInfo.Guid.Value] = deviceInfo;
+
         return orm.InsertOrUpdate(EEMSCenterConfig.DeviceTableName, deviceInfo, t => t.Guid == deviceInfo.Guid, out isNewDevice);
     }
 

+ 3 - 1
Server/EEMSUIClientCore/UICaller.cs

@@ -7,7 +7,9 @@ public class UICaller : IUICaller
 {
     Task<bool> IUICaller.UpdateDeviceInfo(DeviceInfo deviceInfo)
     {
-        return HubBase.Send("UpdateDeviceInfo", deviceInfo);
+        HubBase.Invoke<bool, DeviceInfo>("UpdateDeviceInfo", deviceInfo, out bool Success);
+        //return HubBase.Send("UpdateDeviceInfo", deviceInfo);
+        return Task.FromResult(Success);
     }
 
     Task<IEnumerable<DeviceInfo>> IUICaller.RequestDeviceLists()