diff --git a/OperationsMainSiteGatewayServer/OperationsGatewayAppModule.cs b/OperationsMainSiteGatewayServer/OperationsGatewayAppModule.cs index f460bc6..feb97c8 100644 --- a/OperationsMainSiteGatewayServer/OperationsGatewayAppModule.cs +++ b/OperationsMainSiteGatewayServer/OperationsGatewayAppModule.cs @@ -1,33 +1,117 @@ -using Abp.Dependency; +using Abp.AspNetCore; +using Abp.AspNetCore.Configuration; +using Abp.Dependency; using Abp.Modules; +using Abp.Reflection.Extensions; +using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; +using YunDa.ISAS.Application.Core.Configuration; +using YunDa.ISAS.Configuration; +using YunDa.ISAS.Core; +using YunDa.ISAS.MongoDB.Configuration; +using YunDa.ISAS.Redis.Configuration; namespace Yunda.SOMS.OperationsMainSiteGatewayServer { - + [DependsOn( + typeof(AbpAspNetCoreModule) + )] public class OperationsGatewayAppModule : AbpModule { public override void PreInitialize() { - // 配置其他服务 + // 预初始化阶段,可以进行一些配置 + Console.WriteLine("PreInitialize: ABP Module Setup"); } public override void Initialize() { - // 自动注册程序集中的依赖 - IocManager.RegisterAssemblyByConvention(typeof(OperationsGatewayAppModule).Assembly); - - // 手动注册服务 - //IocManager.Register(); + // 初始化阶段 + IocManager.RegisterAssemblyByConvention(typeof(OperationsGatewayAppModule).GetAssembly()); + Console.WriteLine("Initialize: ABP Module Initialized"); } public override void PostInitialize() { - // 应用启动时的后期初始化逻辑 + var _appConfiguration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); + // 后初始化阶段 + Console.WriteLine("PostInitialize: ABP Module Post Initialization"); + Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString( + ISASConsts.ConnectionMysqlStringKey + ); + + //基于abpModule + // 输入时间格式(支持多种格式解析) + Configuration.Modules.AbpAspNetCore().InputDateTimeFormats = new List + { + // 标准日期时间格式 + "yyyy-MM-ddTHH:mm:ss", // ISO 8601 格式 + "yyyy-MM-ddTHH:mm:ssZ", // ISO 8601 UTC 格式 + "yyyy-MM-ddTHH:mm:ss.fff", // 带毫秒的 ISO 格式 + "yyyy-MM-ddTHH:mm:ss.fffZ", // 带毫秒的 ISO UTC 格式 + + // 自定义日期时间格式 + "yyyy-MM-dd HH:mm:ss", // 常见的日期时间格式 + "yyyy-MM-dd HH:mm:ss.fff", // 带毫秒的日期时间格式 + "yyyy-MM-dd", // 简单日期格式 + "yyyy/MM/dd HH:mm:ss", // 斜线分隔的日期时间 + "yyyy/MM/dd", // 斜线分隔的简单日期 + + // 带时间标识(AM/PM) + "yyyy-MM-dd hh:mm:ss tt", // 12 小时制,带 AM/PM + "yyyy/MM/dd hh:mm:ss tt", // 12 小时制,带 AM/PM 的斜线分隔 + + // 支持带时区偏移的格式 + "yyyy-MM-ddTHH:mm:sszzz", // 带时区的 ISO 格式 + "yyyy-MM-dd HH:mm:sszzz", // 带时区偏移的自定义格式 + + // 长格式(英文) + "ddd, dd MMM yyyy HH:mm:ss GMT",// HTTP 标准格式 + "dddd, dd MMMM yyyy HH:mm:ss", // 长格式带月份全名 + "ddd, dd MMM yyyy HH:mm:ss zzz" // 带时区的长格式 + + // Unix 时间戳(扩展支持可解析为 DateTime) + // 需要额外解析逻辑时也可以支持 + }; + + + // 输出时间格式 + Configuration.Modules.AbpAspNetCore().OutputDateTimeFormat = "yyyy-MM-dd HH:mm:ss"; // 自定义时间格式 + Debug.WriteLine("MySql数据库连接:" + _appConfiguration.GetConnectionString(ISASConsts.ConnectionMysqlStringKey)); + + #region 设置MogngoDBConfig + + var mongoConfiguration = Configuration.Modules.ISASConfiguration(); + ConfigurationHelper.SetMongoDBConfiguration(_appConfiguration, ref mongoConfiguration); + Console.WriteLine("MongoDB数据库连接:" + mongoConfiguration.HostString + ":" + mongoConfiguration.Port); + + #endregion 设置MogngoDBConfig + + #region 设置redisconfig + var redisConfiguration = Configuration.Modules.ISASConfiguration(); + ConfigurationHelper.SetRedisConfiguration(_appConfiguration, ref redisConfiguration); + Console.WriteLine("Redis数据库连接:" + redisConfiguration.Host + ":" + redisConfiguration.Port); + #endregion + + #region 设置AppConfig + + var appServiceConfiguration = Configuration.Modules.ISASConfiguration(); + ConfigurationHelper.SetAppServiceConfiguration(_appConfiguration, ref appServiceConfiguration); + + #endregion 设置AppConfig + } + + public override void Shutdown() + { + // 关闭时调用 + Console.WriteLine("Shutdown: ABP Module Shut down"); } } } diff --git a/OperationsMainSiteGatewayServer/Program.cs b/OperationsMainSiteGatewayServer/Program.cs index 8b07a53..ce93b31 100644 --- a/OperationsMainSiteGatewayServer/Program.cs +++ b/OperationsMainSiteGatewayServer/Program.cs @@ -1,5 +1,6 @@ using System; using Abp; +using Abp.Modules; using Castle.Core; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; @@ -22,7 +23,9 @@ namespace Yunda.SOMS.OperationsMainSiteGatewayServer try { - + // 初始化 ABP 应用程序 + var app = AbpBootstrapper.Create(); + app.Initialize(); var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); @@ -39,6 +42,7 @@ namespace Yunda.SOMS.OperationsMainSiteGatewayServer { e.Cancel = true; // 阻止程序立即退出 cts.Cancel(); // 触发取消 + app.Dispose(); }; try diff --git a/OperationsMainSiteGatewayServer/Yunda.SOMS.OperationsMainSiteGatewayServer.csproj b/OperationsMainSiteGatewayServer/Yunda.SOMS.OperationsMainSiteGatewayServer.csproj index 99f4513..762b988 100644 --- a/OperationsMainSiteGatewayServer/Yunda.SOMS.OperationsMainSiteGatewayServer.csproj +++ b/OperationsMainSiteGatewayServer/Yunda.SOMS.OperationsMainSiteGatewayServer.csproj @@ -19,6 +19,7 @@ + @@ -32,8 +33,10 @@ + + diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs index fc04fad..d4ec80b 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs @@ -148,7 +148,6 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server { await SendCustomMessageAsync(ctx, address, 0, 4, 0);//发送版本信息请求 } - UpdateConnectionContext(address, ctx); await UpdateCommunicationStateCountAsync(address, ctx); } catch (Exception ex) @@ -158,23 +157,20 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server }); } - private void UpdateConnectionContext(byte address, IChannelHandlerContext ctx) - { - _connections[address] = ctx; - } + private async Task UpdateCommunicationStateCountAsync(byte address, IChannelHandlerContext ctx) { - if (_connections.TryGetValue(address, out _)) + if (!_connections.ContainsKey(address)) { - //_communicationStateCounts[address]++; - //已经存在的装置地址 + await SendInitMsgToDeviceAsync(ctx, address); + _connections.TryAdd(address, ctx); } else { - //新加入的装置的地址 - await SendInitMsgToDeviceAsync(ctx, address); + _connections[address] =ctx; } + } private async Task SendInitMsgToDeviceAsync(IChannelHandlerContext ctx, byte address)