using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Edge.Core.Processor; using Edge.Core.Processor.Dispatcher; using Edge.Core.UniversalApi; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using MQTTnet.AspNetCore; using MQTTnet.Diagnostics; using MQTTnet.Protocol; using MQTTnet.Server; using Edge.WebHost.Hubs; using Swashbuckle.AspNetCore.SwaggerGen; namespace Edge.WebHost { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection aspNetCoreServices) { aspNetCoreServices.AddCors(options => options.AddPolicy("cors", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin())); aspNetCoreServices.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); });//Startup.Services.GetRequiredService()); aspNetCoreServices.AddControllersWithViews(); // .AddRazorOptions(opt => //{ // opt.ViewLocationFormats.Add("/AspNetCoreWebApi/Views/{1}/{0}.cshtml"); // opt.ViewLocationFormats.Add("/AspNetCoreWebApi/Views/Shared/{0}.cshtml"); //}); // Register the Swagger generator, defining 1 or more Swagger documents aspNetCoreServices.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Universal Api - WebApi", Version = "v1" }); c.DocumentFilter(); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); aspNetCoreServices.AddMqttWebSocketServerAdapter(); aspNetCoreServices.AddSignalR(); //var mqttServerOptionsBuilder = new MqttServerOptionsBuilder() // .WithoutDefaultEndpoint() // //.WithDefaultEndpointPort(mqttServerPort) // .WithConnectionValidator( // c => // { // c.ReasonCode = MqttConnectReasonCode.Success; // }).WithSubscriptionInterceptor( // c => // { // c.AcceptSubscription = true; // }).WithApplicationMessageInterceptor( // c => // { // c.AcceptPublish = true; // }); aspNetCoreServices .AddHostedMqttServer(mqttServer => mqttServer.WithoutDefaultEndpoint()) .AddMqttConnectionHandler() .AddConnections(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //app.UseCors(); app.UseCors("cors"); // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Universal Api - WebApi V1"); }); //if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // else //{ // app.UseExceptionHandler("/Home/Error"); //} app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapHub("/alarmHub"); }); //app.UseEndpoints(endpoints => //{ // endpoints.MapControllerRoute( // name: "WebConsole", // pattern: "WebConsole/{controller=Home}/{action=Index}/{id?}"); //}); //app.UseEndpoints(endpoints => //{ // endpoints.MapMqtt("/mqtt"); //}); //MqttNetGlobalLogger.LogMessagePublished += (s, e) => //{ // if (e.LogMessage?.Exception != null) // { // var trace = $">> [{e.LogMessage.Timestamp:O}] [{e.LogMessage.ThreadId}] [{e.LogMessage.Source}] [{e.LogMessage.Level}]: {e.LogMessage.Message}"; // trace += Environment.NewLine + e.LogMessage.Exception.ToString(); // //AppInstance?.logger?.LogInformation("Mqtt Server or client component exceptioned: " + Environment.NewLine + trace); // } //}; //app.UseMqttEndpoint("/mqtt"); app.UseEndpoints(endpoints => { endpoints.MapMqtt("/mqtt"); }); app.UseMqttServer(mqttServer => { //mqttServer.UseClientConnectedHandler(e => //{ // //var debug = e.ClientId; //}); //mqttServer.UseClientDisconnectedHandler(e => //{ // //var debug = e.ClientId; //}); //mqttServer.UseApplicationMessageReceivedHandler(async e => //{ // //var debug = e.ApplicationMessage.Topic + " " + // // Encoding.UTF8.GetString(e.ApplicationMessage.Payload); // //if (this.logger.IsEnabled(LogLevel.Debug)) // //{ // // this.logger.LogDebug($"+ received msg on Topic = {e.ApplicationMessage.Topic}"); // // this.logger.LogDebug($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}"); // // this.logger.LogDebug($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}"); // // this.logger.LogDebug($"+ Retain = {e.ApplicationMessage.Retain}"); // //} //}); }); } } }