HubSender.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Device;
  2. using Microsoft.AspNetCore.SignalR.Client;
  3. using System.Threading.Channels;
  4. using System.Threading.Tasks;
  5. using Universal;
  6. namespace TestSignalRClient;
  7. internal class HubSender : SenderBase
  8. {
  9. public bool Initialize(string ip, int port, string hub, int retry = 40)
  10. {
  11. if (_hubConnection is not null)
  12. return false;
  13. HubConnection temp = new HubConnectionBuilder()
  14. .WithUrl($"http://{ip}:{port}/{hub}")
  15. .WithAutomaticReconnect()
  16. .Build();
  17. temp.On<Guid, int>("RequestFile", RequestFile);
  18. //temp.On<byte, Mini8Data>("UpdateMini8", UpdateMini8);
  19. //temp.On<byte, byte, ChannelData>("UpdateSingleChannel", receiver.UpdateSingleChannel);
  20. //temp.On<byte, List<ChannelData>>("UpdateChannel", receiver.UpdateChannels);
  21. //temp.On<TemperatureConfig>("UpdateFiles", receiver.UpdateFiles);
  22. //temp.On("ClearFiles", receiver.ClearFiles);
  23. //temp.On<byte, Mini8Address>("UpdateAddress", receiver.UpdateAddress);
  24. //temp.On<TemperatureConfig>("CurrentFile", receiver.CurrentFile);
  25. //temp.On<byte, byte, float>("AlarmNotify", receiver.AlarmNotify);
  26. //temp.On<byte, byte>("AlarmTcBrockenNotify", receiver.AlarmTcBrockenNotify);
  27. //temp.On<byte, bool>("Mini8Connect", receiver.Mini8Connect);
  28. //temp.On<int, DateTime>("UpdateDataBaseInfo", receiver.UpdateDataBaseInfo);
  29. //temp.On<byte, byte, ChannelData>("ChannelDataUpdate", receiver.ChannelDataUpdate);
  30. //temp.ServerTimeout = TimeSpan.FromSeconds(5.0);
  31. for (int i = 1; i <= retry; i++)
  32. {
  33. try
  34. {
  35. temp.StartAsync().Wait();
  36. _hubConnection = temp;
  37. break;
  38. }
  39. catch
  40. {
  41. if (i == retry)
  42. return false;
  43. Thread.Sleep(1000);
  44. }
  45. }
  46. DeviceInfo deviceInfo = new()
  47. {
  48. DeviceModel = GeneralData.DeviceModel.Proxima,
  49. DeviceSubModel = "Test",
  50. DeviceName = "TestName",
  51. IP = "127.0.0.1",
  52. Port = 50002,
  53. UpdateTime = DateTime.Now
  54. };
  55. base.Invoke<Guid, DeviceInfo>("RegisterDevice", deviceInfo, out Guid returnValue);
  56. Console.WriteLine(returnValue);
  57. return true;
  58. }
  59. private async Task RequestFile(Guid guid, int i)
  60. {
  61. Console.WriteLine($"RequestFile {guid} {i}");
  62. using MemoryStream stream = new();
  63. Compressor.CompressZipFileDirectory(new(@"E:\Recipes"), stream);
  64. await SendFile(guid, stream);
  65. }
  66. public async Task<bool> SendFile(Guid guid, MemoryStream stream)
  67. {
  68. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  69. return false;
  70. for (int i = 0; i < buffer.Count; i++)
  71. {
  72. if (!await base.Send<Guid, byte[], int, int>("FilePack", guid, buffer[i], i + 1, buffer.Count))
  73. return false;
  74. }
  75. return true;
  76. }
  77. }