EEMSBaseServer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using SqlSugar;
  2. using SqlSugarORM;
  3. namespace EEMSServerCore;
  4. public class EEMSBaseServer : IDisposable
  5. {
  6. public WebApplication? WebApplication { get; private set; }
  7. public bool Initialize(IEEMSBaseServerProvider provider, out IClientProvider? clientProvider, long messageSize = 26144)//26144:256kb
  8. {
  9. clientProvider = null;
  10. if (WebApplication is not null)
  11. return false;
  12. SqlSugarCustom orm = new();
  13. orm.Initialize();
  14. orm.Open("Database=EEMSServer;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True", DbType.PostgreSQL, true);
  15. orm.CreateTable<DeviceInfo>("Devices");
  16. WebApplicationBuilder builder = WebApplication.CreateBuilder();
  17. builder.Services.AddSignalR(
  18. options =>
  19. {
  20. options.EnableDetailedErrors = true;
  21. options.MaximumReceiveMessageSize = messageSize;
  22. });
  23. clientProvider = new ClientCaller();
  24. builder.Services.AddHostedService<HostLifeTime>();
  25. builder.Services.AddSingleton<DeviceManager>();
  26. builder.Services.AddSingleton<ClientManager>();
  27. builder.Services.AddSingleton<IUIProvider, UICaller>();
  28. builder.Services.AddSingleton<IClientProvider>(clientProvider);
  29. builder.Services.AddSingleton<IEEMSBaseServerProvider>(provider);
  30. builder.Services.AddSingleton<SqlSugarCustom>(orm);
  31. WebApplication = builder.Build();
  32. WebApplication.MapHub<UIHub>("/UIHub");
  33. WebApplication.MapHub<ClientsMainHub>("/ClientHub");
  34. return true;
  35. }
  36. public async Task<bool> StartServiceAsync(string ip, ushort port)
  37. {
  38. if (WebApplication is null)
  39. return false;
  40. if (string.IsNullOrEmpty(ip) || port < 1000)
  41. return false;
  42. try
  43. {
  44. await WebApplication.RunAsync($"http://{ip}:{port}");
  45. }
  46. catch
  47. {
  48. return false;
  49. }
  50. return true;
  51. }
  52. #region Dispose
  53. private bool disposedValue;
  54. protected virtual void Dispose(bool disposing)
  55. {
  56. if (!disposedValue)
  57. {
  58. if (disposing)
  59. {
  60. // TODO: dispose managed state (managed objects)
  61. this.WebApplication?.DisposeAsync().AsTask().Wait();
  62. this.WebApplication = null;
  63. }
  64. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  65. // TODO: set large fields to null
  66. disposedValue = true;
  67. }
  68. }
  69. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  70. // ~EEMS_Server()
  71. // {
  72. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  73. // Dispose(disposing: false);
  74. // }
  75. public void Dispose()
  76. {
  77. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  78. Dispose(disposing: true);
  79. GC.SuppressFinalize(this);
  80. }
  81. #endregion
  82. }
  83. public interface IEEMSBaseServerProvider
  84. {
  85. void Started();
  86. void OnConnected(string ip, ushort port, ServiceHub serviceHub);
  87. void OnDisConnected(string ip, ushort port, ServiceHub serviceHub);
  88. }
  89. public enum ServiceHub
  90. {
  91. Undefined,
  92. ClinetHub,
  93. UIHub
  94. }