using System.Reflection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; 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 Sln.Iot.Serial; using TouchSocket.Sockets; using TouchSocket.Core; using Fleck; namespace Sln.Iot { /// /// /// internal class Program { public static IServiceProvider? ServiceProvider = null; 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}"); var _server = ServiceProvider.GetService(); _server.Init(appConfig.listernPort); var _webSocket = ServiceProvider.GetService(); _webSocket.Init(); _server.ReceivedBufferRequestInfoEvent += (client, info) => { bool isRet = false; BaseBusiness _business = null; int bodyLength = 0; switch (info.DataType) { case 0x08: //校时指令 _business = ServiceProvider.GetService(); break; case 0x21: //登录指令 _business = ServiceProvider.GetService(); break; case 0x24: //心跳指令 _business = ServiceProvider.GetService(); break; case 0x83: //电能指令 bodyLength = 70; _business = ServiceProvider.GetService(); break; case 0x84: //水 bodyLength = 58; _business = ServiceProvider.GetService(); break; case 0x85: //物联网环境 bodyLength = info.BufferLength; _business = ServiceProvider.GetService(); break; default: break; } if (_business != null) { Parallel.Invoke( //() => _business.ResponseHandle(client, info.buffer), () => _business.BufferAnalysis(client, info, bodyLength) ); } }; ServiceProvider.UseSerialPortExtensions(); 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; }); //services.AddSingleton(provider => //{ // TcpClient tcpClient = new TcpClient(); // tcpClient.Setup(new TouchSocketConfig() // .SetRemoteIPHost("127.0.0.1:6000") // .ConfigureContainer(a => // { // a.AddConsoleLogger(); // })); // tcpClient.Connect(); // return tcpClient; //}); 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() .WithSingletonLifetime()); services.AddSingleton(typeof(SerilogHelper)); services.AddSingleton(typeof(TcpService)); services.AddSqlSugarSetup(); } } }