using System.Reflection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Sln.Iot.Business; using Sln.Iot.Business.@base; using Sln.Iot.Config; using Sln.Iot.Repository; using Sln.Iot.Serilog; using Sln.Iot.Socket; using TouchSocket.Sockets; namespace Sln.Iot; class Program { public static IServiceProvider? ServiceProvider = null; private static DevControlBusiness _devControlBusiness; static async Task Main(string[] args) { var services = new ServiceCollection(); ConfigureServices(services); ServiceProvider = services.BuildServiceProvider(); ServiceProvider.UseSerilogExtensions(); var appConfig = ServiceProvider.GetService(); var log = ServiceProvider.GetService(); log.Info($"系统启动成功,日志存放位置:{appConfig.logPath}"); _devControlBusiness = ServiceProvider.GetService(); var _server = ServiceProvider.GetService(); _server.Init(appConfig.listernPort); _server.ReceivedBufferRequestInfoEvent += (client, info) => { bool isRet = false; BaseBusiness _business = null; int bodyLength = 0; switch (info.DataType) { case 0x08: _business = ServiceProvider.GetService(); break; case 0x32: bodyLength = 1; _business = ServiceProvider.GetService(); break; case 0x33: //登录指令 _business = ServiceProvider.GetService(); break; case 0x36: //心跳指令 _business = ServiceProvider.GetService(); break; case 0x34: //物联网环境 bodyLength = info.BufferLength; _business = ServiceProvider.GetService(); break; case 0x39: bodyLength = 8; _business = ServiceProvider.GetService(); break; default: break; } if (_business != null) { Parallel.Invoke( () => _business.ResponseHandle(client, info), () => _business.BufferAnalysis(client, info, bodyLength) ); } }; var webApiServer = ServiceProvider.GetService(); webApiServer.Init(); await Task.Delay(-1); } private static void ConfigureServices(IServiceCollection services) { services.AddSingleton(provider => { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfiguration configuration = configurationBuilder.Build(); var ap = configuration.GetSection("AppConfig").Get(); return ap; }); Assembly[] assemblies = { Assembly.LoadFrom("Sln.Iot.Repository.dll"), Assembly.LoadFrom("Sln.Iot.Socket.dll"), Assembly.LoadFrom("Sln.Iot.Common.dll"), Assembly.LoadFrom("Sln.Iot.Business.dll"), }; services.Scan(scan => scan.FromAssemblies(assemblies) .AddClasses() .AsImplementedInterfaces() .AsSelf() .WithTransientLifetime()); services.AddSingleton(typeof(SerilogHelper)); services.AddSingleton(typeof(TcpService)); services.AddSingleton(typeof(WebApiServer)); services.AddSqlSugarSetup(); } }