123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using AmrControl;
- using AmrControl.ADS;
- using AmrControl.Clients;
- using AmrControl.Common;
- using AmrControl.DB;
- using AmrControl.JGR;
- using AmrControl.mq;
- using AmrControl.services;
- using AmrControl.workstation;
- using Microsoft.AspNetCore.SignalR;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.FileProviders;
- using Microsoft.Extensions.Hosting;
- using Newtonsoft.Json;
- using System.Runtime.InteropServices;
- using System.Text;
- var builder = WebApplication.CreateBuilder(args);
- var configuration = builder.Configuration;
- // Add services to the container.
- //builder.Services.AddRazorPages();
- //添加signalR服务
- //builder.Services.AddSignalR();
- //添加控制器,并使用newtonsoftjson作为json序列化与反序列化工具
- builder.Services.AddControllers().AddNewtonsoftJson();
- builder.Services.AddControllers();
- var apphelper = new AppHelper(builder.Configuration);
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
- builder.Services.AddEndpointsApiExplorer();
- builder.Services.AddSwaggerGen();
- //AddSingleton :每次都是相同的实例
- //builder.Services.AddSingleton<ITaskExecManager, TaskExecManager>();
- //builder.Services.AddSingleton<IRegistryService, RegistryService>();
- //var mqHost = builder.Configuration["mq:host"];
- //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost))
- //{
- // builder.Services.AddSingleton<IRabbitmqService, RabbitmqService>();
- //}
- //builder.Services.AddSingleton<IJGR, JGRManager>();
- //builder.Services.AddSingleton(typeof(MyDbHandler), sp => {
- // //return new MyDbHandler(builder.Configuration.GetConnectionString("MySql"));
- // return new MyDbHandler(builder.Configuration.GetConnectionString("Sqlite"));
- //});
- //builder.Services.AddSingleton(typeof(IJGR), sp => {
- // var scope = sp.CreateScope();
- // var hubContext = scope.ServiceProvider.GetRequiredService<IHubContext<ChatHub>>();
- // var context = scope.ServiceProvider.GetRequiredService<MyDbHandler>();
- // var taskExecManager = scope.ServiceProvider.GetRequiredService<ITaskExecManager>();
- // var registrySerivce = scope.ServiceProvider.GetRequiredService<IRegistryService>();
- // return new JGRManager(hubContext, context, builder.Configuration, taskExecManager, registrySerivce);
- //});
- //添加工位的采集控制
- //builder.Services.AddSingleton<IWorkstation, MsWorkstation>();
- builder.Services.AddSingleton(typeof(MsWorkstation), sp => {
- return new MsWorkstation(configuration, sp);
- });
- builder.Services.AddSingleton(typeof(MqttClient), sp => {
- return new MqttClient(configuration, sp);
- });
- var app = builder.Build();
- // Configure the HTTP request pipeline.
- if (app.Environment.IsDevelopment())
- {
- app.UseSwagger();
- app.UseSwaggerUI();
- }
- //app.UseHttpsRedirection();
- app.UseAuthorization();
- //自定义网页入口
- DefaultFilesOptions options = new DefaultFilesOptions();
- //options.DefaultFileNames.Clear();
- //options.DefaultFileNames.Add("/index.html");
- //app.UseDefaultFiles(options);
- //允许静态文件
- //app.UseStaticFiles();
- //app.UseStaticFiles(new StaticFileOptions
- //{
- // FileProvider = new PhysicalFileProvider(
- // Path.Combine(AppContext.BaseDirectory, "files")),
- // RequestPath = ""
- //});
- //app.MapRazorPages();
- //app.MapHub<ChatHub>("/chatHub");
- app.MapControllers();
- //var hubContext = app.Services.GetService<IHubContext<ChatHub>>();
- ////初始化小车控制
- //var jgr = builder.Services.BuildServiceProvider().GetRequiredService<IJGR>();
- //if (jgr != null)
- //{
- // jgr.Init(app.Services);
- // jgr.SetHub(hubContext);
- //}
- //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost))
- //{
- // var rabbitmqService = builder.Services.BuildServiceProvider().GetRequiredService<IRabbitmqService>();
- // if(rabbitmqService != null)
- // {
- // rabbitmqService.Init();
- // }
- //}
- //初始化sqlite数据库
- //var sqlite = builder.Services.BuildServiceProvider().GetRequiredService<MyDbHandler>();
- //if(sqlite != null)
- //{
- // sqlite.InitSqlite();
- //}
- //int size = Marshal.SizeOf<JGR_Tc_Model>();
- //初始化工位的采集控制
- IServiceProvider provider = builder.Services.BuildServiceProvider();
- var mqtt = provider.GetService<MqttClient>();
- if (null != mqtt)
- {
- mqtt.CreateClient();
- }
- var ws = provider.GetService<MsWorkstation>();
- if (null != ws)
- {
- ws.Stop();
- }
- //程序退出事件
- AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
- //app 执行
- app.Run();
- void CurrentDomain_ProcessExit(object? sender, EventArgs e)
- {
- AmrManager.ProcessExit = true;
- //throw new NotImplementedException();
- }
- //var builder = Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
- //{
- // webBuilder.UseUrls("http://*:8000")
- // .UseStartup<Startup>()
- // .UseKestrel(opt => opt.Limits.MaxRequestBodySize = null);
- //})
- //.UseDefaultServiceProvider(options =>
- //{
- // options.ValidateScopes = false;
- //});
- ////程序退出事件
- //AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
- //void CurrentDomain_ProcessExit(object? sender, EventArgs e)
- //{
- // AmrManager.ProcessExit = true;
- // //throw new NotImplementedException();
- //}
- //builder.Build().Run();
|