Parcourir la source

Update File Receiver

Zixuan il y a 14 heures
Parent
commit
a8e8a63ca5

+ 50 - 1
EEMSCenterUI/ViewModels/MainWindowViewModel.cs

@@ -1,5 +1,9 @@
 
 using ServiceBase;
+using SqlSugar;
+using System;
+using System.IO;
+using Universal;
 
 namespace EEMSCenterUI.ViewModels;
 
@@ -43,10 +47,55 @@ internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerPr
     private void RequestFile()
     {
         Guid.TryParse(this.GUID, out Guid guid);
-        bool? b= this._clientProvider?.RequestFile(guid, FileType.Config).Result;
+        bool? b = this._clientProvider?.RequestFile(guid, FileType.Config).Result;
     }
 
 
+    [RelayCommand]
+    private async Task SendFile()
+    {
+        if (_clientProvider is null || this.GUID is null)
+        {
+            MessageBox.Show("Send File Failed");
+            return;
+        }
+
+        if (!Guid.TryParse(this.GUID, out Guid guid))
+        {
+            MessageBox.Show("Send File Failed");
+            return;
+        }
+
+
+        using MemoryStream stream = new();
+        Compressor.CompressZipFileDirectory(new(@"E:\RTConfig"), stream);
+
+        if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
+        {
+            MessageBox.Show("Send File Failed");
+            return;
+        }
+
+
+        for (int i = 0; i < buffer.Count; i++)
+        {
+            try
+            {
+                if (!await _clientProvider.PushFile(guid, FileType.Config, buffer[i], i + 1, buffer.Count))
+                {
+                    MessageBox.Show("Send File Failed");
+                    return;
+                }
+
+            }
+            catch
+            {
+                MessageBox.Show("Send File Failed");
+                return;
+            }
+        }
+    }
+
 
     void IEEMSBaseServerProvider.Started()
     {

+ 45 - 3
Server/EEMSClientCore/ClientProvider.cs

@@ -1,10 +1,43 @@
-using System.Diagnostics;
+using System.Collections.Concurrent;
+using System.Diagnostics;
 using Universal;
 
 namespace EEMSClientCore;
 
 public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider baseProvider) : IClientProvider
 {
+    private static readonly ConcurrentDictionary<FileType, MemoryStream> _streamBuffer = [];
+    public static readonly ConcurrentDictionary<FileType, string> _savePath = [];
+
+    //Receive Files from Server
+    public Task<bool> PushFile(Guid guid, FileType fileType, byte[] buffer, int current, int total)
+    {
+        if (!_streamBuffer.TryGetValue(fileType, out MemoryStream? stream) || stream is null)
+        {
+            if (current != 1)
+                return Task.FromResult(false);
+
+            stream = new();
+            _streamBuffer[fileType] = stream;
+            Timer timer = new(FileCleanerTimerCallback, fileType, 600000, Timeout.Infinite);
+        }
+
+        stream.Write(buffer);
+
+        if (current != total)
+            return Task.FromResult(true);
+
+        string path = Path.Combine(Environment.CurrentDirectory, "Receive");
+        if (!Compressor.DecompressZipFile(new(path), stream))
+            return Task.FromResult(false);
+
+        if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
+            memoryStream.Dispose();
+
+        return Task.FromResult(true);
+    }
+
+
     public Task<bool> RequestFile(Guid guid, FileType fileType)
     {
         Debug.WriteLine($"RequestFile {guid} {fileType}");
@@ -21,7 +54,7 @@ public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider base
 
         if (!Directory.Exists(filePath))
             return Task.FromResult(false);
-        
+
         using MemoryStream stream = new();
         Compressor.CompressZipFileDirectory(new(filePath), stream);
 
@@ -47,10 +80,19 @@ public class ClientProvider(IClientCaller clientCaller, IClientBaseProvider base
         }
         return true;
     }
+
+    private void FileCleanerTimerCallback(object? state)
+    {
+        if (state is not FileType fileType)
+            return;
+
+        if (_streamBuffer.TryRemove(fileType, out MemoryStream? memoryStream) && memoryStream is not null)
+            memoryStream.Dispose();
+    }
 }
 
 public interface IClientBaseProvider
 {
     public string? RecipePath { get; set; }
     public string? ConfigPath { get; set; }
-}  
+}

+ 24 - 0
Server/EEMSService/HubSender/ClientCaller.cs

@@ -96,8 +96,32 @@ public class ClientCaller : IClientProvider
         return Task.FromResult(true);
     }
 
+    private Task<bool> SendAsync<T1, T2, T3, T4, T5>(Guid guid, string name, T1 para1, T2 para2, T3 para3, T4 para4, T5 para5)
+    {
+        if (string.IsNullOrEmpty(name))
+            return Task.FromResult(false);
+
+        if (ClientManager.DeviceClients.TryGetValue(guid, out ISingleClientProxy? client) || client is null)
+            return Task.FromResult(false);
+
+        try
+        {
+            client.SendAsync(name, para1, para2, para3, para4, para5);
+        }
+        catch
+        {
+            return Task.FromResult(false);
+        }
+        return Task.FromResult(true);
+    }
+
     Task<bool> IClientProvider.RequestFile(Guid guid, FileType fileType)
     {
         return this.SendAsync(guid, "RequestFile", guid, fileType);
     }
+
+    Task<bool> IClientProvider.PushFile(Guid guid, FileType fileType, byte[] buffer, int current, int total)
+    {
+        return this.SendAsync(guid, "ReceivedFile", guid, fileType, buffer, current, total);
+    }
 }

+ 5 - 4
Server/EEMSService/Hubs/ClientsFileHub.cs

@@ -1,4 +1,5 @@
-using System.Windows;
+using System.Diagnostics;
+using System.Windows;
 
 namespace EEMSServerCore.Hubs;
 
@@ -8,7 +9,7 @@ internal partial class ClientsMainHub
     public static readonly ConcurrentDictionary<Guid, string> _savePath = [];
     public Task<bool> FilePack(Guid guid, byte[] buffer, int current, int total)
     {
-        Console.WriteLine($"FilePack {guid} {current} {total}");
+        Debug.WriteLine($"FilePack {guid} {current} {total}");
         if (!_streamBuffer.TryGetValue(guid, out MemoryStream? stream) || stream is null)
         {
             if (current != 1)
@@ -23,8 +24,8 @@ internal partial class ClientsMainHub
 
         if (current != total)
             return Task.FromResult(true);
-
-        if (!Compressor.DecompressZipFile(Environment.CurrentDirectory, stream))
+        string path = Path.Combine(Environment.CurrentDirectory, "Receive");
+        if (!Compressor.DecompressZipFile(new(path), stream))
             return Task.FromResult(false);
 
         if (_streamBuffer.TryRemove(guid, out MemoryStream? memoryStream) && memoryStream is not null)

+ 2 - 1
Server/ServiceBase/ClientInterfaces.cs

@@ -18,4 +18,5 @@ public interface IClientCaller
 public interface IClientProvider
 {
     Task<bool> RequestFile(Guid guid, FileType fileType);
-}
+    Task<bool> PushFile(Guid guid, FileType fileType, byte[] buffer, int current, int total);
+}

+ 11 - 0
Server/ServiceBase/FilePacker.cs

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