Browse Source

Create test UI client

Zixuan 4 hours ago
parent
commit
e06c34abba

+ 2 - 2
DataBase/DB_Proxima/RemoteToLocal.cs

@@ -315,7 +315,7 @@ public class RemoteToLocal
                         switch (system.Key)
                         {
                             case string s when s.StartsWith("Heater"):
-                                _orm.Insert("Heater", CreateData<Heater>(guid, time, item));
+                                _orm.Insert("Heater", CreateData<Heater>(guid, time, item)!);
                                 continue;
                             case string s when s.StartsWith("AlarmSignalHeater"):
                                 alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
@@ -327,7 +327,7 @@ public class RemoteToLocal
                     }
                     continue;
                 case string s when s.StartsWith("Stocker"):
-                    _orm.Insert("Stocker", CreateData<Stocker>(guid, time, item));
+                    _orm.Insert("Stocker", CreateData<Stocker>(guid, time, item)!);
                     continue;
                 case string s when s.StartsWith("LP"):
                     continue;

+ 1 - 1
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -191,7 +191,7 @@ public partial class MainWindowViewModel : ObservableObject
         try
         {
             _realtimeData.Clear();
-            for (int i = 0; i < 100; i++)
+            for (int i = 0; i < 10; i++)
             {
                 _realtimeData[i.ToString()] = DateTime.Now.ToString();
             }

+ 3 - 1
Server/EEMSService/HubSender/ClientCaller.cs

@@ -2,6 +2,7 @@
 
 public class ClientCaller : IClientProvider
 {
+    #region Internal Send
     private Task<bool> SendAsync(Guid guid, string name)
     {
         if (string.IsNullOrEmpty(name))
@@ -114,6 +115,7 @@ public class ClientCaller : IClientProvider
         }
         return Task.FromResult(true);
     }
+    #endregion
 
     Task<bool> IClientProvider.RequestFile(Guid guid, FileType fileType)
     {
@@ -124,4 +126,4 @@ public class ClientCaller : IClientProvider
     {
         return this.SendAsync(guid, "PushFile", guid, fileType, buffer, current, total);
     }
-}
+}

+ 23 - 8
Server/EEMSService/HubSender/UICaller.cs

@@ -1,19 +1,32 @@
-
-
-namespace EEMSServerCore.HubSender;
+namespace EEMSServerCore.HubSender;
 
 internal class UICaller : IUIProvider
 {
-    public Task<bool> UpdateDevice(DeviceInfo device)
+    Task<bool> IUIProvider.UpdateDevice(DeviceInfo device)
+    {
+        return this.SendAsync("UpdateDevice", device);
+    }
+
+    Task<bool> IUIProvider.UpdateDeviceList(IEnumerable<DeviceInfo> device)
     {
-        throw new NotImplementedException();
+        return this.SendAsync("UpdateDeviceList", device);
+
     }
 
-    public Task<bool> UpdateDeviceList(IEnumerable<DeviceInfo> device)
+    Task<bool> IUIProvider.InsertNewDevice(DeviceInfo deviceInfo)
     {
-        throw new NotImplementedException();
-    }  
+        return this.SendAsync("InsertNewDevice", deviceInfo);
+    }
 
+    Task<bool> IUIProvider.UpdateRealtimeData(Guid guid, Dictionary<string, object> data)
+    {
+        return this.SendAsync("UpdateRealtimeData", guid, data);
+    }
+    Task<bool> IUIProvider.OnlineStatusNotify(Guid guid, bool onlineStatus)
+    {
+        return this.SendAsync("OnlineStatusNotify", guid, onlineStatus);
+    }
+    #region Internal Send
     private Task<bool> SendAsync(string name)
     {
         if (string.IsNullOrEmpty(name))
@@ -109,4 +122,6 @@ internal class UICaller : IUIProvider
         return Task.FromResult(true);
     }
 
+
+    #endregion
 }

+ 3 - 3
Server/EEMSService/Hubs/ClientsFileHub.cs

@@ -5,8 +5,8 @@ namespace EEMSServerCore.Hubs;
 
 internal partial class ClientsMainHub
 {
-    private static readonly ConcurrentDictionary<Guid, MemoryStream> _streamBuffer = [];
-    public static readonly ConcurrentDictionary<Guid, string> _savePath = [];
+    private readonly ConcurrentDictionary<Guid, MemoryStream> _streamBuffer = [];
+    public readonly ConcurrentDictionary<Guid, string> _savePath = [];
     public Task<bool> FilePack(Guid guid, byte[] buffer, int current, int total)
     {
         Debug.WriteLine($"FilePack {guid} {current} {total}");
@@ -43,4 +43,4 @@ internal partial class ClientsMainHub
         if (_streamBuffer.TryRemove(guid, out MemoryStream? memoryStream) && memoryStream is not null)
             memoryStream.Dispose();
     }
-}
+}

+ 19 - 3
Server/EEMSService/Hubs/ClientsMainHub.cs

@@ -3,13 +3,16 @@ using SqlSugarORM;
 
 namespace EEMSServerCore.Hubs;
 
-internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider, SqlSugarCustom orm) : Hub, IClientCaller
+internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider, SqlSugarCustom orm, IUIProvider uiCaller) : Hub, IClientCaller
 {
     public override Task OnConnectedAsync()
     {
         if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
             provider?.OnConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
 
+        if (deviceManager.TryGetDevice(Context.ConnectionId, out DeviceInfo? device) && device is not null && device.Guid is not null)
+            uiCaller.OnlineStatusNotify(device.Guid.Value, true);
+
         return base.OnConnectedAsync();
     }
 
@@ -18,7 +21,10 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
         deviceManager.RemoveDevice(Context.ConnectionId, out DeviceInfo? device);
 
         if (device is not null && device.Guid.HasValue)
+        {
             ClientManager.DeviceClients.TryRemove(device.Guid.Value, out _);
+            uiCaller.OnlineStatusNotify(device.Guid.Value, false);
+        }
 
         if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
             provider?.OnDisConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
@@ -28,20 +34,30 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
 
     public Task<Guid> RegisterDevice(DeviceInfo deviceInfo)
     {
-        deviceInfo.Guid ??= Guid.NewGuid();
         deviceInfo.IP ??= string.Empty;
+        bool newDevice = (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty);
+        deviceInfo.Guid ??= Guid.NewGuid();
 
         if (!orm.AddOrUpdate("Devices", deviceInfo, t => t.Guid == deviceInfo.Guid))
             return Task.FromResult(Guid.Empty);
 
+
         deviceManager.LoginDevice(Context.ConnectionId, deviceInfo);
         ClientManager.DeviceClients[deviceInfo.Guid.Value] = Clients.Caller;
+
+        if (newDevice)
+            uiCaller.InsertNewDevice(deviceInfo);
+        else
+            uiCaller.UpdateDevice(deviceInfo);
+
         return Task.FromResult(deviceInfo.Guid.Value);
     }
 
     public Task<bool> UpdateRealTimeData(Dictionary<string, object> realtimeData)
     {
+        if (!deviceManager.TryGetDevice(Context.ConnectionId, out DeviceInfo? device) || device is null || device.Guid is null)
+            return Task.FromResult(false);
 
-        return Task.FromResult(true);
+        return uiCaller.UpdateRealtimeData(device.Guid.Value, realtimeData);
     }
 }

+ 0 - 11
Server/EEMSUIClientCore/ClientCaller.cs

@@ -1,11 +0,0 @@
-using ServiceBase;
-
-namespace EEMSUIClientCore;
-
-public class ClientCaller : IUICaller
-{
-    Task<bool> IUICaller.RequestDeviceLists()
-    {
-        return HubBase.Send("RegisterDevice");
-    }
-}

+ 3 - 0
Server/EEMSUIClientCore/HubBase.cs

@@ -33,6 +33,9 @@ public class HubBase : IDisposable
 
         temp.On<IEnumerable<DeviceInfo>>("UpdateDeviceList", _provider.UpdateDeviceList);
         temp.On<DeviceInfo>("UpdateDevice", _provider.UpdateDevice);
+        temp.On<DeviceInfo>("InsertNewDevice", _provider.InsertNewDevice);
+        temp.On<Guid, Dictionary<string, object>>("UpdateRealtimeData", _provider.UpdateRealtimeData);
+        temp.On<Guid, bool>("OnlineStatusNotify", _provider.OnlineStatusNotify);
 
         for (int i = 1; i <= retry; i++)
         {

+ 17 - 0
Server/EEMSUIClientCore/UICaller.cs

@@ -0,0 +1,17 @@
+using Device;
+using ServiceBase;
+
+namespace EEMSUIClientCore;
+
+public class UICaller : IUICaller
+{
+    Task<bool> IUICaller.UpdateDeviceInfo(DeviceInfo deviceInfo)
+    {
+        return HubBase.Send("UpdateDeviceInfo", deviceInfo);
+    }
+
+    Task<bool> IUICaller.RequestDeviceLists()
+    {
+        return HubBase.Send("RequestDeviceLists");
+    }
+}

+ 0 - 2
Server/ServiceBase/ClientInterfaces.cs

@@ -6,9 +6,7 @@
 public interface IClientCaller
 {
     Task<Guid> RegisterDevice(DeviceInfo deviceInfo);
-
     Task<bool> FilePack(Guid guid, byte[] buffer, int current, int total);
-
     Task<bool> UpdateRealTimeData(Dictionary<string, object> realtimeData);
 }
 

+ 0 - 11
Server/ServiceBase/FilePacker.cs

@@ -1,11 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ServiceBase;
-
-internal class FilePacker
-{
-}

+ 4 - 0
Server/ServiceBase/UIInterfaces.cs

@@ -6,6 +6,7 @@
 public interface IUICaller
 {
     Task<bool> RequestDeviceLists();
+    Task<bool> UpdateDeviceInfo(DeviceInfo deviceInfo);
 }
 
 /// <summary>
@@ -13,8 +14,11 @@ public interface IUICaller
 /// </summary>
 public interface IUIProvider
 {
+    Task<bool> OnlineStatusNotify(Guid guid, bool onlineStatus);
     Task<bool> UpdateDeviceList(IEnumerable<DeviceInfo> device);
     Task<bool> UpdateDevice(DeviceInfo device);
+    Task<bool> InsertNewDevice(DeviceInfo deviceInfo);
+    Task<bool> UpdateRealtimeData(Guid guid, Dictionary<string, object> data);
 }
 
 public enum FileType : byte

+ 76 - 1
TestSignalRClient/Program.cs

@@ -1,12 +1,73 @@
 using Device;
 using EEMSClientCore;
+using EEMSUIClientCore;
 using ServiceBase;
+
 namespace TestSignalRClient;
 
 internal class Program
 {
     static void Main(string[] args)
     {
+        UITest test = new();
+        test.Test();
+
+        Thread.Sleep(-1);
+    }
+}
+
+internal class UITest : IUIProvider
+{
+
+    public void Test()
+    {
+        HubBase hubBase = new();
+        hubBase.Initialize(this);
+        bool b = hubBase.Open("127.0.0.1", 50054, "UIHub");
+        Console.WriteLine($"Open UI Client {b}");
+    }
+
+    public Task<bool> InsertNewDevice(DeviceInfo deviceInfo)
+    {
+        Console.WriteLine($"InsertNewDevice {deviceInfo.DeviceName} {deviceInfo.Guid}");
+        return Task.FromResult(true);
+    }
+    public Task<bool> UpdateDevice(DeviceInfo device)
+    {
+        Console.WriteLine($"UpdateDevice {device.DeviceName} {device.Guid}");
+        return Task.FromResult(true);
+    }
+
+    public Task<bool> UpdateDeviceList(IEnumerable<DeviceInfo> device)
+    {
+        foreach (var deviceInfo in device)
+        {
+            Console.WriteLine($"UpdateDeviceList {deviceInfo.DeviceName} {deviceInfo.Guid}");
+        }
+        return Task.FromResult(true);
+    }
+
+    public Task<bool> UpdateRealtimeData(Guid guid, Dictionary<string, object> data)
+    {
+        Console.WriteLine($"UpdateRealtimeData {guid} {data.Count}");
+        foreach (var item in data)
+        {
+            Console.WriteLine($"UpdateRealtimeData {item.Key} {item.Value}");
+        }
+        return Task.FromResult(true);
+    }
+
+    public Task<bool> OnlineStatusNotify(Guid guid, bool onlineStatus)
+    {
+        Console.WriteLine($"OnlineStatusNotify {guid} {onlineStatus}");
+        return Task.FromResult(true);
+    }
+}
+
+internal class ClientTest
+{
+    public void Test()
+    {
         IClientBaseProvider baseProvider = new ClientBaseProvider();
         ClientCaller hubSender = new(baseProvider);
         IClientCaller caller = hubSender;
@@ -20,7 +81,6 @@ internal class Program
 
         Guid guid = caller.RegisterDevice(deviceInfo).Result;
         Console.WriteLine(guid);
-        Thread.Sleep(-1);
     }
 }
 
@@ -28,4 +88,19 @@ internal class ClientBaseProvider : IClientBaseProvider
 {
     public string? RecipePath { get; set; }
     public string? ConfigPath { get; set; }
+    string? IClientBaseProvider.RecipePath { get; set; }
+    string? IClientBaseProvider.ConfigPath { get; set; }
+
+    bool IClientBaseProvider.AllowReceiveFile()
+    {
+        return true;
+    }
+
+    void IClientBaseProvider.FileReceivedNotify(FileType fileType, string path)
+    {
+    }
+
+    void IClientBaseProvider.StartFileReceiveNotify(FileType fileType)
+    {
+    }
 }

+ 1 - 0
TestSignalRClient/TestSignalRClient.csproj

@@ -14,6 +14,7 @@
   <ItemGroup>
     <ProjectReference Include="..\Data\Device\Device.csproj" />
     <ProjectReference Include="..\Server\EEMSClientCore\EEMSClientCore.csproj" />
+    <ProjectReference Include="..\Server\EEMSUIClientCore\EEMSUIClientCore.csproj" />
     <ProjectReference Include="..\Server\ServiceBase\ServiceBase.csproj" />
     <ProjectReference Include="..\Universal\Universal.csproj" />
   </ItemGroup>