Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using AmrControl;
  2. using AmrControl.ADS;
  3. using AmrControl.Clients;
  4. using AmrControl.Common;
  5. using AmrControl.DB;
  6. using AmrControl.JGR;
  7. using AmrControl.mq;
  8. using AmrControl.services;
  9. using AmrControl.workstation;
  10. using Microsoft.AspNetCore.SignalR;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using Newtonsoft.Json;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. var builder = WebApplication.CreateBuilder(args);
  18. var configuration = builder.Configuration;
  19. // Add services to the container.
  20. //builder.Services.AddRazorPages();
  21. //添加signalR服务
  22. //builder.Services.AddSignalR();
  23. //添加控制器,并使用newtonsoftjson作为json序列化与反序列化工具
  24. builder.Services.AddControllers().AddNewtonsoftJson();
  25. builder.Services.AddControllers();
  26. var apphelper = new AppHelper(builder.Configuration);
  27. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  28. builder.Services.AddEndpointsApiExplorer();
  29. builder.Services.AddSwaggerGen();
  30. //AddSingleton :每次都是相同的实例
  31. //builder.Services.AddSingleton<ITaskExecManager, TaskExecManager>();
  32. //builder.Services.AddSingleton<IRegistryService, RegistryService>();
  33. //var mqHost = builder.Configuration["mq:host"];
  34. //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost))
  35. //{
  36. // builder.Services.AddSingleton<IRabbitmqService, RabbitmqService>();
  37. //}
  38. //builder.Services.AddSingleton<IJGR, JGRManager>();
  39. //builder.Services.AddSingleton(typeof(MyDbHandler), sp => {
  40. // //return new MyDbHandler(builder.Configuration.GetConnectionString("MySql"));
  41. // return new MyDbHandler(builder.Configuration.GetConnectionString("Sqlite"));
  42. //});
  43. //builder.Services.AddSingleton(typeof(IJGR), sp => {
  44. // var scope = sp.CreateScope();
  45. // var hubContext = scope.ServiceProvider.GetRequiredService<IHubContext<ChatHub>>();
  46. // var context = scope.ServiceProvider.GetRequiredService<MyDbHandler>();
  47. // var taskExecManager = scope.ServiceProvider.GetRequiredService<ITaskExecManager>();
  48. // var registrySerivce = scope.ServiceProvider.GetRequiredService<IRegistryService>();
  49. // return new JGRManager(hubContext, context, builder.Configuration, taskExecManager, registrySerivce);
  50. //});
  51. //添加工位的采集控制
  52. //builder.Services.AddSingleton<IWorkstation, MsWorkstation>();
  53. builder.Services.AddSingleton(typeof(MsWorkstation), sp => {
  54. return new MsWorkstation(configuration, sp);
  55. });
  56. builder.Services.AddSingleton(typeof(MqttClient), sp => {
  57. return new MqttClient(configuration, sp);
  58. });
  59. var app = builder.Build();
  60. // Configure the HTTP request pipeline.
  61. if (app.Environment.IsDevelopment())
  62. {
  63. app.UseSwagger();
  64. app.UseSwaggerUI();
  65. }
  66. //app.UseHttpsRedirection();
  67. app.UseAuthorization();
  68. //自定义网页入口
  69. DefaultFilesOptions options = new DefaultFilesOptions();
  70. //options.DefaultFileNames.Clear();
  71. //options.DefaultFileNames.Add("/index.html");
  72. //app.UseDefaultFiles(options);
  73. //允许静态文件
  74. //app.UseStaticFiles();
  75. //app.UseStaticFiles(new StaticFileOptions
  76. //{
  77. // FileProvider = new PhysicalFileProvider(
  78. // Path.Combine(AppContext.BaseDirectory, "files")),
  79. // RequestPath = ""
  80. //});
  81. //app.MapRazorPages();
  82. //app.MapHub<ChatHub>("/chatHub");
  83. app.MapControllers();
  84. //var hubContext = app.Services.GetService<IHubContext<ChatHub>>();
  85. ////初始化小车控制
  86. //var jgr = builder.Services.BuildServiceProvider().GetRequiredService<IJGR>();
  87. //if (jgr != null)
  88. //{
  89. // jgr.Init(app.Services);
  90. // jgr.SetHub(hubContext);
  91. //}
  92. //if (!string.IsNullOrEmpty(mqHost) && !string.IsNullOrWhiteSpace(mqHost))
  93. //{
  94. // var rabbitmqService = builder.Services.BuildServiceProvider().GetRequiredService<IRabbitmqService>();
  95. // if(rabbitmqService != null)
  96. // {
  97. // rabbitmqService.Init();
  98. // }
  99. //}
  100. //初始化sqlite数据库
  101. //var sqlite = builder.Services.BuildServiceProvider().GetRequiredService<MyDbHandler>();
  102. //if(sqlite != null)
  103. //{
  104. // sqlite.InitSqlite();
  105. //}
  106. //int size = Marshal.SizeOf<JGR_Tc_Model>();
  107. //初始化工位的采集控制
  108. IServiceProvider provider = builder.Services.BuildServiceProvider();
  109. var mqtt = provider.GetService<MqttClient>();
  110. if (null != mqtt)
  111. {
  112. mqtt.CreateClient();
  113. }
  114. var ws = provider.GetService<MsWorkstation>();
  115. if (null != ws)
  116. {
  117. ws.Stop();
  118. }
  119. //程序退出事件
  120. AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
  121. //app 执行
  122. app.Run();
  123. void CurrentDomain_ProcessExit(object? sender, EventArgs e)
  124. {
  125. AmrManager.ProcessExit = true;
  126. //throw new NotImplementedException();
  127. }
  128. //var builder = Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
  129. //{
  130. // webBuilder.UseUrls("http://*:8000")
  131. // .UseStartup<Startup>()
  132. // .UseKestrel(opt => opt.Limits.MaxRequestBodySize = null);
  133. //})
  134. //.UseDefaultServiceProvider(options =>
  135. //{
  136. // options.ValidateScopes = false;
  137. //});
  138. ////程序退出事件
  139. //AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
  140. //void CurrentDomain_ProcessExit(object? sender, EventArgs e)
  141. //{
  142. // AmrManager.ProcessExit = true;
  143. // //throw new NotImplementedException();
  144. //}
  145. //builder.Build().Run();