You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

142 lines
4.8 KiB

  1. using System;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.EntityFrameworkCore.Internal;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Scripting.Utils;
  9. using MQTTnet.Server.Configuration;
  10. using MQTTnet.Server.Logging;
  11. using MQTTnet.Server.Mqtt;
  12. using MQTTnet.Server.Scripting;
  13. using MQTTnet.Server.Scripting.DataSharing;
  14. namespace MQTTnet.Server
  15. {
  16. public class Startup
  17. {
  18. public Startup(IConfiguration configuration)
  19. {
  20. var builder = new ConfigurationBuilder()
  21. .AddJsonFile("appsettings.json")
  22. .AddEnvironmentVariables();
  23. Configuration = builder.Build();
  24. }
  25. public IConfigurationRoot Configuration { get; }
  26. public void Configure(
  27. IApplicationBuilder application,
  28. IHostingEnvironment environment,
  29. MqttServerService mqttServerService,
  30. PythonScriptHostService pythonScriptHostService,
  31. DataSharingService dataSharingService,
  32. SettingsModel settings)
  33. {
  34. if (environment.IsDevelopment())
  35. {
  36. application.UseDeveloperExceptionPage();
  37. }
  38. else
  39. {
  40. application.UseHsts();
  41. }
  42. application.UseStaticFiles();
  43. application.UseHttpsRedirection();
  44. application.UseMvc();
  45. ConfigureWebSocketEndpoint(application, mqttServerService, settings);
  46. dataSharingService.Configure();
  47. pythonScriptHostService.Configure();
  48. mqttServerService.Configure();
  49. }
  50. public void ConfigureServices(IServiceCollection services)
  51. {
  52. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  53. services.AddSingleton(ReadSettings());
  54. services.AddSingleton<PythonIOStream>();
  55. services.AddSingleton<PythonScriptHostService>();
  56. services.AddSingleton<DataSharingService>();
  57. services.AddSingleton<MqttNetLoggerWrapper>();
  58. services.AddSingleton<CustomMqttFactory>();
  59. services.AddSingleton<MqttServerService>();
  60. services.AddSingleton<MqttServerStorage>();
  61. services.AddSingleton<MqttClientConnectedHandler>();
  62. services.AddSingleton<MqttClientDisconnectedHandler>();
  63. services.AddSingleton<MqttClientSubscribedTopicHandler>();
  64. services.AddSingleton<MqttClientUnsubscribedTopicHandler>();
  65. services.AddSingleton<MqttConnectionValidator>();
  66. services.AddSingleton<MqttSubscriptionInterceptor>();
  67. services.AddSingleton<MqttApplicationMessageInterceptor>();
  68. }
  69. private SettingsModel ReadSettings()
  70. {
  71. var settings = new Configuration.SettingsModel();
  72. Configuration.Bind("MQTT", settings);
  73. return settings;
  74. }
  75. private static void ConfigureWebSocketEndpoint(
  76. IApplicationBuilder application,
  77. MqttServerService mqttServerService,
  78. SettingsModel settings)
  79. {
  80. if (settings?.WebSocketEndPoint?.Enabled != true)
  81. {
  82. return;
  83. }
  84. if (string.IsNullOrEmpty(settings.WebSocketEndPoint.Path))
  85. {
  86. return;
  87. }
  88. var webSocketOptions = new WebSocketOptions
  89. {
  90. KeepAliveInterval = TimeSpan.FromSeconds(settings.WebSocketEndPoint.KeepAliveInterval),
  91. ReceiveBufferSize = settings.WebSocketEndPoint.ReceiveBufferSize
  92. };
  93. if (settings.WebSocketEndPoint.AllowedOrigins?.Any() == true)
  94. {
  95. webSocketOptions.AllowedOrigins.AddRange(settings.WebSocketEndPoint.AllowedOrigins);
  96. }
  97. application.UseWebSockets(webSocketOptions);
  98. application.Use(async (context, next) =>
  99. {
  100. if (context.Request.Path == settings.WebSocketEndPoint.Path)
  101. {
  102. if (context.WebSockets.IsWebSocketRequest)
  103. {
  104. using (var webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false))
  105. {
  106. await mqttServerService.RunWebSocketConnectionAsync(webSocket, context).ConfigureAwait(false);
  107. }
  108. }
  109. else
  110. {
  111. context.Response.StatusCode = 400;
  112. }
  113. }
  114. else
  115. {
  116. await next().ConfigureAwait(false);
  117. }
  118. });
  119. }
  120. }
  121. }