You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.7 KiB
C#

3 months ago
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;
3 months ago
static async Task Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
ServiceProvider = services.BuildServiceProvider();
ServiceProvider.UseSerilogExtensions();
var appConfig = ServiceProvider.GetService<AppConfig>();
var log = ServiceProvider.GetService<SerilogHelper>();
log.Info($"系统启动成功,日志存放位置:{appConfig.logPath}");
_devControlBusiness = ServiceProvider.GetService<DevControlBusiness>();
3 months ago
var _server = ServiceProvider.GetService<TcpServer>();
_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<CheckTimeBusiness>();
break;
case 0x33: //登录指令
3 months ago
_business = ServiceProvider.GetService<LoginBusiness>();
break;
case 0x36: //心跳指令
3 months ago
_business = ServiceProvider.GetService<HeartBusiness>();
break;
case 0x34: //物联网环境
3 months ago
bodyLength = info.BufferLength;
_business = ServiceProvider.GetService<LocalBusiness>();
3 months ago
break;
default:
break;
}
if (_business != null)
{
Parallel.Invoke(
() => _business.ResponseHandle(client, info),
3 months ago
() => _business.BufferAnalysis(client, info, bodyLength)
);
}
};
var webApiServer = ServiceProvider.GetService<WebApiServer>();
webApiServer.Init();
3 months ago
await Task.Delay(-1);
3 months ago
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<AppConfig>(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<AppConfig>();
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));
3 months ago
services.AddSqlSugarSetup();
}
}