| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Device;
- using Microsoft.AspNetCore.SignalR.Client;
- using System.Threading.Channels;
- using System.Threading.Tasks;
- using Universal;
- namespace TestSignalRClient;
- internal class HubSender : SenderBase
- {
- public bool Initialize(string ip, int port, string hub, int retry = 40)
- {
- if (_hubConnection is not null)
- return false;
- HubConnection temp = new HubConnectionBuilder()
- .WithUrl($"http://{ip}:{port}/{hub}")
- .WithAutomaticReconnect()
- .Build();
- temp.On<Guid, int>("RequestFile", RequestFile);
- //temp.On<byte, Mini8Data>("UpdateMini8", UpdateMini8);
- //temp.On<byte, byte, ChannelData>("UpdateSingleChannel", receiver.UpdateSingleChannel);
- //temp.On<byte, List<ChannelData>>("UpdateChannel", receiver.UpdateChannels);
- //temp.On<TemperatureConfig>("UpdateFiles", receiver.UpdateFiles);
- //temp.On("ClearFiles", receiver.ClearFiles);
- //temp.On<byte, Mini8Address>("UpdateAddress", receiver.UpdateAddress);
- //temp.On<TemperatureConfig>("CurrentFile", receiver.CurrentFile);
- //temp.On<byte, byte, float>("AlarmNotify", receiver.AlarmNotify);
- //temp.On<byte, byte>("AlarmTcBrockenNotify", receiver.AlarmTcBrockenNotify);
- //temp.On<byte, bool>("Mini8Connect", receiver.Mini8Connect);
- //temp.On<int, DateTime>("UpdateDataBaseInfo", receiver.UpdateDataBaseInfo);
- //temp.On<byte, byte, ChannelData>("ChannelDataUpdate", receiver.ChannelDataUpdate);
- //temp.ServerTimeout = TimeSpan.FromSeconds(5.0);
- for (int i = 1; i <= retry; i++)
- {
- try
- {
- temp.StartAsync().Wait();
- _hubConnection = temp;
- break;
- }
- catch
- {
- if (i == retry)
- return false;
- Thread.Sleep(1000);
- }
- }
- DeviceInfo deviceInfo = new()
- {
- DeviceModel = GeneralData.DeviceModel.Proxima,
- DeviceSubModel = "Test",
- DeviceName = "TestName",
- IP = "127.0.0.1",
- Port = 50002,
- UpdateTime = DateTime.Now
- };
- base.Invoke<Guid, DeviceInfo>("RegisterDevice", deviceInfo, out Guid returnValue);
- Console.WriteLine(returnValue);
- return true;
- }
- private async Task RequestFile(Guid guid, int i)
- {
- Console.WriteLine($"RequestFile {guid} {i}");
- using MemoryStream stream = new();
- Compressor.CompressZipFileDirectory(new(@"E:\Recipes"), stream);
- await SendFile(guid, stream);
- }
- public async Task<bool> SendFile(Guid guid, MemoryStream stream)
- {
- if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
- return false;
- for (int i = 0; i < buffer.Count; i++)
- {
- if (!await base.Send<Guid, byte[], int, int>("FilePack", guid, buffer[i], i + 1, buffer.Count))
- return false;
-
- }
- return true;
- }
- }
|