ClientService.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using EEMSClientCore;
  2. using ServiceBase;
  3. namespace EEMSUIClient.Services
  4. {
  5. public class ClientService : IClientService, IClientProvider
  6. {
  7. private readonly IClientCaller _clientCaller;
  8. private readonly ClientCaller _hubBase;
  9. private bool disposedValue;
  10. public ClientService()
  11. {
  12. _clientCaller = new ClientCaller();
  13. _hubBase = new HubBase();
  14. }
  15. public event EventHandler<(Guid guid, FileType fileType)>? RequestFileReceived;
  16. public bool Initialize(string ip, int port, string hub)
  17. {
  18. if (string.IsNullOrWhiteSpace(ip) || port < 0 || string.IsNullOrWhiteSpace(hub))
  19. {
  20. //log
  21. return false;
  22. }
  23. if (!_hubBase.Initialize(this))
  24. {
  25. //log
  26. return false;
  27. }
  28. if (!_hubBase.Open(ip, port, hub))
  29. {
  30. //log
  31. return false;
  32. }
  33. return true;
  34. }
  35. public Guid RegisterDevice(Models.DeviceInfo deviceInfo)
  36. {
  37. return _clientCaller.RegisterDevice(deviceInfo.Convert()).Result;
  38. }
  39. public Task<bool> RequestFile(Guid guid, FileType fileType)
  40. {
  41. //log
  42. RequestFileReceived?.Invoke(null, (guid, fileType));
  43. return Task.FromResult(true);
  44. }
  45. protected virtual void Dispose(bool disposing)
  46. {
  47. if (!disposedValue)
  48. {
  49. if (disposing)
  50. {
  51. // TODO: dispose managed state (managed objects)
  52. _hubBase?.Dispose();
  53. }
  54. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  55. // TODO: set large fields to null
  56. disposedValue = true;
  57. }
  58. }
  59. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  60. // ~ClientService()
  61. // {
  62. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  63. // Dispose(disposing: false);
  64. // }
  65. public void Dispose()
  66. {
  67. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  68. Dispose(disposing: true);
  69. GC.SuppressFinalize(this);
  70. }
  71. }
  72. }