Browse Source

Create EEMS Center Server
Create Compress Method in Universal

Zixuan 1 week ago
parent
commit
ebed77a969

+ 0 - 1
Data_ViewModel/GlobalData/DeviceData.cs

@@ -1,5 +1,4 @@
 using System.Collections.ObjectModel;
-using System.Net.NetworkInformation;
 using CommunityToolkit.Mvvm.ComponentModel;
 using GeneralData;
 using UICommon.DataType;

+ 1 - 0
Directory.Packages.props

@@ -5,6 +5,7 @@
   <ItemGroup>
     <PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.0" />
     <PackageVersion Include="Mapster" Version="7.4.0" />
+    <PackageVersion Include="Microsoft.AspNetCore.SignalR.Common" Version="8.0.19" />
     <PackageVersion Include="netDxf" Version="2023.11.10" />
     <PackageVersion Include="netDxf.netstandard" Version="3.0.1" />
     <PackageVersion Include="Prism.DryIoc" Version="9.0.537" />

+ 19 - 0
EEMSCenter/EEMSCenter.csproj

@@ -0,0 +1,19 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+	<PropertyGroup>
+		<OutputType>WinExe</OutputType>
+		<TargetFramework>net8.0-windows</TargetFramework>
+		<OutputPath>$(SolutionDir)Binary\Server</OutputPath>
+		<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+		<ImplicitUsings>enable</ImplicitUsings>
+		<Nullable>enable</Nullable>
+		<UseWPF>true</UseWPF>
+		<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
+	</PropertyGroup>
+	<ItemGroup>
+		<FrameworkReference Include="Microsoft.AspNetCore.App" />
+	</ItemGroup>
+	<ItemGroup>
+	  <PackageReference Include="Microsoft.AspNetCore.SignalR.Common" />
+	</ItemGroup>
+</Project>

+ 16 - 0
EEMSCenter/HostLifetime.cs

@@ -0,0 +1,16 @@
+using Microsoft.Extensions.Hosting;
+
+namespace EEMSCenter;
+
+internal class HostLifetime : IHostedService
+{
+    public Task StartAsync(CancellationToken cancellationToken)
+    {
+        return Task.CompletedTask;
+    }
+
+    public Task StopAsync(CancellationToken cancellationToken)
+    {
+        return Task.CompletedTask;
+    }
+}

+ 7 - 0
EEMSCenter/Hubs/ClientsHub.cs

@@ -0,0 +1,7 @@
+using Microsoft.AspNetCore.SignalR;
+
+namespace EEMSCenter.Hubs;
+
+internal class ClientsHub : Hub
+{
+}

+ 12 - 0
EEMSCenter/Hubs/UIHub.cs

@@ -0,0 +1,12 @@
+using Microsoft.AspNetCore.SignalR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EEMSCenter.Hubs;
+
+internal class UIHub : Hub
+{
+}

+ 51 - 0
EEMSCenter/Program.cs

@@ -0,0 +1,51 @@
+using EEMSCenter.Hubs;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.Extensions.DependencyInjection;
+using System.Windows;
+
+namespace EEMSCenter;
+
+internal class Program
+{
+    public static WebApplication? WebApplication { get; private set; }
+
+    static void Main(string[] args)
+    {
+        Mutex mutex = new(true, "7E862400-8020-BE75-5266-B2C4BEB54075", out bool flag);
+        if (!flag)
+            return;
+
+        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
+        TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
+
+        WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
+        builder.Services.AddSignalR(
+            options =>
+            {
+                options.EnableDetailedErrors = true;
+                options.MaximumReceiveMessageSize = 262144;//256k
+                //options.ClientTimeoutInterval = TimeSpan.FromSeconds(5);
+                //options.KeepAliveInterval = TimeSpan.FromSeconds(10);
+                //options.MaximumParallelInvocationsPerClient = 5;
+            });
+
+        //Host Service
+        builder.Services.AddHostedService<HostLifetime>();
+
+        WebApplication = builder.Build();
+        WebApplication.MapHub<UIHub>("/UIHub");
+        WebApplication.MapHub<ClientsHub>("/ClientHub");
+        WebApplication.RunAsync($"http://127.0.0.1:50054").Wait();
+    }
+
+    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+    {
+        string? s = e.ExceptionObject?.ToString();
+        MessageBox.Show(s is null ? "Unknow UnhandledException" : s);
+    }
+
+    private static void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
+    {
+        //_expLog.Fatal(e.Exception!.ToString()!);
+    }
+}

+ 6 - 0
EEMSMain.sln

@@ -66,6 +66,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CAD2XMAL", "CAD2XMAL\CAD2XMAL.csproj", "{8E249AE0-505B-4481-8C83-42C71561370E}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EEMSCenter", "EEMSCenter\EEMSCenter.csproj", "{9C18B022-F837-4736-B298-480AC29F8362}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -160,6 +162,10 @@ Global
 		{8E249AE0-505B-4481-8C83-42C71561370E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{8E249AE0-505B-4481-8C83-42C71561370E}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{8E249AE0-505B-4481-8C83-42C71561370E}.Release|Any CPU.Build.0 = Release|Any CPU
+		{9C18B022-F837-4736-B298-480AC29F8362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9C18B022-F837-4736-B298-480AC29F8362}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9C18B022-F837-4736-B298-480AC29F8362}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9C18B022-F837-4736-B298-480AC29F8362}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 0 - 4
Module/DashBoard/ViewModel/DashBoardMainViewModel.cs

@@ -6,7 +6,6 @@ internal partial class DashBoardMainViewModel : ObservableObject
 {
     public DashBoardMainViewModel(DeviceCollection deviceCollection, IDialogService dialogService)
     {
-        this._DeviceCollection = deviceCollection;
         this._DialogService = dialogService;
         this.Devices = deviceCollection.DeviceList;
         deviceCollection.DeviceDataList.CollectionChanged += DeviceDataList_CollectionChanged;
@@ -24,11 +23,9 @@ internal partial class DashBoardMainViewModel : ObservableObject
             this.DeviceData[device.DeviceModel] ??= [];
             this.DeviceData[device.DeviceModel].Add(device);
         }
-
     }
 
     private readonly IDialogService _DialogService;
-    private readonly DeviceCollection _DeviceCollection;
 
     [ObservableProperty]
     private ObservableDictionary<Guid, DeviceInfo_VM> _Devices = [];
@@ -40,7 +37,6 @@ internal partial class DashBoardMainViewModel : ObservableObject
     private void Warning(DeviceData_VM device)
     {
         IDialogParameters para = new DialogParameters() { { "alarm", device.Alarms! } };
-
         this._DialogService.Show("WarningList", para, null);
     }
 }

+ 54 - 13
Test/Program.cs

@@ -1,7 +1,10 @@
-using Microsoft.Win32;
+using Microsoft.IdentityModel.Tokens;
+using Microsoft.Win32;
 using System.Data;
 using System.Diagnostics;
+using System.Net.Sockets;
 using System.Text;
+using Universal;
 
 namespace Test;
 
@@ -9,14 +12,8 @@ internal class Program
 {
     static void Main()
     {
-        int[,] timeStamp = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
-        int s = timeStamp[0, 1];
-        int length = timeStamp.Length;
-        foreach (var item in timeStamp)
-        {
-
-        }
-        var t = timeStamp.GetValue(0,0);
+        CompressTest test = new();
+        test.Test();
 
         //DBTest test = new();
         //test.Initialize();
@@ -25,12 +22,12 @@ internal class Program
         //test.TestOthers();
         //test.TestRaw();
 
-        ProcessTest processTest = new();
-        processTest.Initialize();
-        processTest.BackUp("10.4.6.48", 5432, "postgres", "123456", "D://source_db_dump.custom", "tin01_db");
+        //ProcessTest processTest = new();
+        //processTest.Initialize();
+        //processTest.BackUp("10.4.6.48", 5432, "postgres", "123456", "D://source_db_dump.custom", "tin01_db");
         //processTest.BackUpSingleTable("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630.PM1");
         //processTest.BackUpByDateTime("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630");
-        processTest.Restore("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "DBTestTIN");
+        //processTest.Restore("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "DBTestTIN");
     }
 }
 
@@ -131,4 +128,48 @@ internal class ProcessTest
         return true;
     }
 
+}
+
+internal class CompressTest
+{
+    public void Test()
+    {
+        using MemoryStream stream = new();
+
+        Compressor.CompressZipFileDirectory(new(@"E:\Recipes"), stream);
+        Console.WriteLine(this.SplitSpan(stream, CallBack) ? "Send Success" : "Send Failed");
+    }
+
+    private bool CallBack(byte[] input, int current, int total)
+    {
+        Console.WriteLine($"{input.Length} {current} / {total}");
+        return true;
+    }
+
+    private bool SplitSpan(MemoryStream stream, Func<byte[], int, int, bool> callBack, int packLength = 4096)
+    {
+        if (packLength < 256)
+            return false;
+
+        Span<byte> t = stream.ToArray();
+        int count = t.Length / packLength;
+
+        Span<byte> ts;
+        for (int i = 0; i <= count; i++)
+        {
+            if (i == count)
+            {
+                ts = t.Slice(i * packLength);
+                if (callBack?.Invoke(ts.ToArray(), i, count) != true)
+                    return false;
+                break;
+            }
+            ts = t.Slice(i * packLength, packLength);
+            if (callBack?.Invoke(ts.ToArray(), i, count) != true)
+                return false;
+        }
+
+        return true;
+    }
+
 }

+ 1 - 0
Test/Test.csproj

@@ -5,6 +5,7 @@
     <TargetFramework>net8.0</TargetFramework>
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
+    <ApplicationManifest>app.manifest</ApplicationManifest>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

+ 79 - 0
Test/app.manifest

@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
+  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
+    <security>
+      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
+        <!-- UAC Manifest Options
+             If you want to change the Windows User Account Control level replace the 
+             requestedExecutionLevel node with one of the following.
+
+        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
+        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
+        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
+
+            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
+            Remove this element if your application requires this virtualization for backwards
+            compatibility.
+        -->
+        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
+      </requestedPrivileges>
+    </security>
+  </trustInfo>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- A list of the Windows versions that this application has been tested on
+           and is designed to work with. Uncomment the appropriate elements
+           and Windows will automatically select the most compatible environment. -->
+
+      <!-- Windows Vista -->
+      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
+
+      <!-- Windows 7 -->
+      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
+
+      <!-- Windows 8 -->
+      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
+
+      <!-- Windows 8.1 -->
+      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
+
+      <!-- Windows 10 -->
+      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
+
+    </application>
+  </compatibility>
+
+  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
+       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
+       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
+       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. 
+       
+       Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
+  <!--
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
+      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
+    </windowsSettings>
+  </application>
+  -->
+
+  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
+  <!--
+  <dependency>
+    <dependentAssembly>
+      <assemblyIdentity
+          type="win32"
+          name="Microsoft.Windows.Common-Controls"
+          version="6.0.0.0"
+          processorArchitecture="*"
+          publicKeyToken="6595b64144ccf1df"
+          language="*"
+        />
+    </dependentAssembly>
+  </dependency>
+  -->
+
+</assembly>

+ 51 - 0
Universal/Compressor.cs

@@ -0,0 +1,51 @@
+using System.IO.Compression;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace Universal;
+
+public class Compressor
+{
+    public static bool CompressZipFileDirectory(DirectoryInfo sourceDirectory, DirectoryInfo zipFilePath, string fileName, out string? fullZippedPath)
+    {
+        fullZippedPath = null;
+
+        if (!sourceDirectory.Exists)
+            return false;
+
+        if (string.IsNullOrEmpty(fileName))
+            return false;
+
+        if (!fileName.EndsWith(".zip"))
+            fileName = $"{fileName}.zip";
+
+        fullZippedPath = Path.Combine(zipFilePath.FullName, fileName);
+        try
+        {
+            ZipFile.CreateFromDirectory(sourceDirectory.FullName, fullZippedPath, CompressionLevel.SmallestSize, false);
+        }
+        catch
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    public static bool CompressZipFileDirectory(DirectoryInfo sourceDirectory, Stream stream)
+    {
+        if (!sourceDirectory.Exists)
+            return false;
+        
+        try
+        {
+            ZipFile.CreateFromDirectory(sourceDirectory.FullName, stream, CompressionLevel.SmallestSize, false);
+        }
+        catch
+        {
+            return false;
+        }
+
+        return true;
+    }
+}

+ 1 - 1
Universal/Extensions.cs

@@ -104,7 +104,7 @@ public static class Extensions
 #endif
 
 //Require C# 14+
-#if false
+#if NET10_0_OR_GREATER
 
 public static class ExtensionTest
 {