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

Update EEMS Server Core

Zixuan 1 hete%!(EXTRA string=óta)
szülő
commit
3e245fec15

+ 1 - 0
EEMSCenterUI/EEMSCenterUI.csproj

@@ -18,6 +18,7 @@
 		<FrameworkReference Include="Microsoft.AspNetCore.App" />
 	</ItemGroup>
 	<ItemGroup>
+	  <ProjectReference Include="..\Server\EEMSService\EEMSService.csproj" />
 	  <ProjectReference Include="..\UICommon\UICommon.csproj" />
 	</ItemGroup>
 	<ItemGroup>

+ 72 - 2
EEMSCenterUI/ViewModels/MainWindowViewModel.cs

@@ -1,16 +1,86 @@
 using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using EEMSService;
+using Microsoft.AspNetCore.SignalR;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
+using UICommon.DataType;
 
 namespace EEMSCenterUI.ViewModels;
 
-internal class MainWindowViewModel : ObservableObject
+internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerProvider
 {
     public MainWindowViewModel()
     {
-        
+        this.Server = new()
+        {
+            IP = "127.0.0.1",
+            Port = 50054
+        };
     }
+
+    [ObservableProperty]
+    private ServiceEndpoint _Server;
+
+
+    [RelayCommand]
+    private void StartService()
+    {
+        EEMSBaseServer baseServer = new();
+        baseServer.Initialize(this);
+        Task.Factory.StartNew(async () =>
+        {
+            if (!await baseServer.StartServiceAsync(this.Server.IP!, this.Server.Port))
+                MessageBox.Show("Service Start Failed");
+        }, TaskCreationOptions.LongRunning);
+    }
+
+    void IEEMSBaseServerProvider.Started()
+    {
+        Task.Factory.StartNew(() =>
+        {
+            MessageBox.Show($"Service Started {this.Server.IP}:{this.Server.Port}");
+        });
+    }
+
+    void IEEMSBaseServerProvider.OnConnected(string ip, ushort port, ServiceHub serviceHub)
+    {
+        ServiceEndpoint serviceEndpoint = new()
+        {
+            IP = ip,
+            Port = port,
+            Hub = serviceHub
+        };
+        App.Current.Dispatcher?.Invoke(() =>
+        {
+            Clients[$"{ip}{port}{serviceHub}"] = serviceEndpoint;
+        });
+    }
+
+    public void OnDisConnected(string ip, ushort port, ServiceHub serviceHub)
+    {
+        App.Current.Dispatcher?.Invoke(() =>
+        {
+            Clients.TryRemove($"{ip}{port}{serviceHub}", out _);
+        });
+    }
+
+    [ObservableProperty]
+    ObservableDictionary<string, ServiceEndpoint> _Clients = [];
 }
+
+public partial class ServiceEndpoint : ObservableObject
+{
+    [ObservableProperty]
+    private string? _IP;
+
+    [ObservableProperty]
+    private ushort _Port;
+
+    [ObservableProperty]
+    private ServiceHub _Hub;
+}

+ 24 - 0
EEMSCenterUI/Views/MainWindow.xaml

@@ -42,5 +42,29 @@
                 </Button>
             </Grid>
         </Border>
+
+        <Grid Grid.Row="1">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="auto"/>
+                <RowDefinition/>
+            </Grid.RowDefinitions>
+            <StackPanel>
+                <TextBox Text="{Binding Server.IP}"/>
+                <TextBox Text="{Binding Server.Port}"/>
+                <Button Command="{Binding StartServiceCommand}">Start Service</Button>
+            </StackPanel>
+
+            <ItemsControl ItemsSource="{Binding Clients}" Grid.Row="2">
+                <ItemsControl.ItemTemplate>
+                    <DataTemplate>
+                        <StackPanel Orientation="Horizontal">
+                            <TextBlock Text="{Binding Value.IP}"/>
+                            <TextBlock Margin="16,0" Text="{Binding Value.Port}"/>
+                            <TextBlock Text="{Binding Value.Hub}"/>
+                        </StackPanel>
+                    </DataTemplate>
+                </ItemsControl.ItemTemplate>
+            </ItemsControl>
+        </Grid>
     </Grid>
 </Window>

+ 22 - 6
Server/EEMSService/EEMSBaseServer.cs

@@ -1,13 +1,12 @@
-
-
-namespace EEMSService;
+namespace EEMSService;
 
 public class EEMSBaseServer : IDisposable
 {
 
     public WebApplication? WebApplication { get; private set; }
 
-    public bool Initialize(long messageSize = 26144)//26144:256kb
+    private IEEMSBaseServerProvider _provider;
+    public bool Initialize(IEEMSBaseServerProvider provider, long messageSize = 26144)//26144:256kb
     {
         if (WebApplication is not null)
             return false;
@@ -16,7 +15,7 @@ public class EEMSBaseServer : IDisposable
         builder.Services.AddSignalR(
             options =>
             {
-                options.EnableDetailedErrors = true; 
+                options.EnableDetailedErrors = true;
                 options.MaximumReceiveMessageSize = messageSize;
             });
 
@@ -24,6 +23,8 @@ public class EEMSBaseServer : IDisposable
         builder.Services.AddSingleton<DeviceManager>();
         builder.Services.AddSingleton<ClientManager>();
         builder.Services.AddSingleton<UISender>();
+        builder.Services.AddSingleton<IEEMSBaseServerProvider>(provider);
+        this._provider = provider;
 
         WebApplication = builder.Build();
         WebApplication.MapHub<UIHub>("/UIHub");
@@ -31,7 +32,7 @@ public class EEMSBaseServer : IDisposable
         return true;
     }
 
-    public async Task<bool> StartService(string ip, ushort port)
+    public async Task<bool> StartServiceAsync(string ip, ushort port)
     {
         if (WebApplication is null)
             return false;
@@ -86,3 +87,18 @@ public class EEMSBaseServer : IDisposable
     }
     #endregion
 }
+
+
+public interface IEEMSBaseServerProvider
+{
+    void Started();
+    void OnConnected(string ip, ushort port, ServiceHub serviceHub);
+    void OnDisConnected(string ip, ushort port, ServiceHub serviceHub);
+}
+
+public enum ServiceHub
+{
+    Undefined,
+    ClinetHub,
+    UIHub
+}

+ 2 - 1
Server/EEMSService/EEMSService.csproj

@@ -1,10 +1,11 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
 	<PropertyGroup>
-		<TargetFramework>net8.0</TargetFramework>
+		<TargetFramework>net8.0-windows</TargetFramework>
 		<ImplicitUsings>enable</ImplicitUsings>
 		<Nullable>enable</Nullable>
 		<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
+		<UseWPF>true</UseWPF>
 	</PropertyGroup>
 	<ItemGroup>
 		<FrameworkReference Include="Microsoft.AspNetCore.App" />

+ 21 - 8
Server/EEMSService/Hubs/ClientsHub.cs

@@ -1,9 +1,19 @@
-namespace EEMSService.Hubs;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Http.Features;
+using System.Net;
 
-internal partial class ClientsHub(DeviceManager deviceManager, ClientManager clientManager) : HubBase
+namespace EEMSService.Hubs;
+
+internal partial class ClientsHub(DeviceManager deviceManager, ClientManager clientManager, IEEMSBaseServerProvider provider) : HubBase
 {
     public override Task OnConnectedAsync()
     {
+
+
+        if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
+        {
+            provider?.OnConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
+        }
         return base.OnConnectedAsync();
     }
 
@@ -13,7 +23,10 @@ internal partial class ClientsHub(DeviceManager deviceManager, ClientManager cli
 
         if (device is not null && device.Guid.HasValue)
             clientManager.DeviceClients.TryRemove(device.Guid.Value, out _);
-
+        if (Context.Features.Get<IHttpConnectionFeature>() is IHttpConnectionFeature feature)
+        {
+            provider?.OnDisConnected(feature.RemoteIpAddress!.ToString(), (ushort)feature.RemotePort, ServiceHub.ClinetHub);
+        }
         return base.OnDisconnectedAsync(exception);
     }
 
@@ -22,11 +35,11 @@ internal partial class ClientsHub(DeviceManager deviceManager, ClientManager cli
         deviceInfo.Guid ??= Guid.NewGuid();
         deviceManager.LoginDevice(Context.ConnectionId, deviceInfo);
         clientManager.DeviceClients[deviceInfo.Guid.Value] = Clients.Caller;
-        Task.Factory.StartNew(() =>
-        {
-            Thread.Sleep(2000);
-            clientManager.DeviceClients[deviceInfo.Guid.Value].SendAsync("RequestFile", Guid.NewGuid(), 1);
-        });
+        //Task.Factory.StartNew(() =>
+        //{
+        //    Thread.Sleep(2000);
+        //    clientManager.DeviceClients[deviceInfo.Guid.Value].SendAsync("RequestFile", Guid.NewGuid(), 1);
+        //});
         return Task.FromResult(deviceInfo.Guid.Value);
     }
 }

+ 4 - 2
Server/EEMSService/Hubs/FileHub.cs

@@ -1,4 +1,6 @@
-namespace EEMSService.Hubs;
+using System.Windows;
+
+namespace EEMSService.Hubs;
 
 internal partial class ClientsHub : HubBase
 {
@@ -28,7 +30,7 @@ internal partial class ClientsHub : HubBase
         if (_streamBuffer.TryRemove(guid, out MemoryStream? memoryStream) && memoryStream is not null)
             memoryStream.Dispose();
 
-
+        MessageBox.Show("File Received");
         return Task.FromResult(true);
     }
 

+ 1 - 3
TestSignalRClient/HubSender.cs

@@ -1,7 +1,5 @@
 using Device;
 using Microsoft.AspNetCore.SignalR.Client;
-using System.Threading.Channels;
-using System.Threading.Tasks;
 using Universal;
 
 namespace TestSignalRClient;
@@ -83,7 +81,7 @@ internal class HubSender : SenderBase
         {
             if (!await base.Send<Guid, byte[], int, int>("FilePack", guid, buffer[i], i + 1, buffer.Count))
                 return false;
-            
+
         }
         return true;
     }