diff --git a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/EquipmentLiveData/TelesignalisationModel.cs b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/EquipmentLiveData/TelesignalisationModel.cs index 575f1b5..56b14dc 100644 --- a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/EquipmentLiveData/TelesignalisationModel.cs +++ b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/EquipmentLiveData/TelesignalisationModel.cs @@ -17,7 +17,6 @@ namespace YunDa.ISAS.DataTransferObject.EquipmentLiveData public int ResultValue { get; set; } = ConstantModel.DegaultValue; public bool IsSendSelfCheck { get; set; } - public virtual string ResultValueStr { get diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/DataCollection/DataSendTask.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/DataCollection/DataSendTask.cs index 33e5df8..06fac57 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/DataCollection/DataSendTask.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/DataCollection/DataSendTask.cs @@ -5,6 +5,7 @@ using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -24,6 +25,7 @@ using YunDa.ISAS.DataTransferObject.EquipmentLiveData; using YunDa.ISAS.Entities.DataMonitoring; using YunDa.ISAS.Redis.Entities.DataMonitorCategory; using YunDa.ISAS.Redis.Repositories; +using YunDa.SOMS.DataTransferObject.GeneralInformation.SecondaryCircuitDto; using YunDa.SOMS.DataTransferObject.MainStationMaintenanceInfo.OperationReport; using Z.Expressions; @@ -174,7 +176,7 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis.DataCollection tasks.Add(SetEnvirmentTemp(ycData)); // 执行所有任务并等待完成 - await Task.WhenAll(tasks); + //await Task.WhenAll(tasks); } catch (Exception ex) { @@ -247,7 +249,7 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis.DataCollection tasks.Add(_alarmAnalysis.HandleTelesignalAlarmAsync(yxData)); // 等待所有任务完成 - await Task.WhenAll(tasks); + //await Task.WhenAll(tasks); } catch (Exception ex) { @@ -255,58 +257,84 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis.DataCollection MonitoringEventBus.LogHandler(ex.Message, "错误信息"); } } - private async Task CheckSecondCuirtAlarm(string haskey) + private Task CheckSecondCuirtAlarm(string haskey) { if (_runningDataCache.SecondaryCircuitLogicExpressionDic.ContainsKey(haskey)) { - var listLogics = _runningDataCache.SecondaryCircuitLogicExpressionDic[haskey]; - foreach (var listLogic in listLogics) + return Task.Run(() => { - // 示例字符串 - - // 正则表达式匹配花括号中的内容 - string pattern = @"\{([^}]*)\}"; - Regex regex = new Regex(pattern); - - // 提取匹配内容 - MatchCollection matches = regex.Matches(listLogic.LogicExpression); - List extractedStrings = new List(); - foreach (Match match in matches) + lock (_runningDataCache.SecondaryCircuitLogicExpressionDic) { - var logichasKey = match.Groups[1].Value; - string telemeteringredisKey = _redisDataRepository.TelemeteringModelListRediskey + "_" + _settingModel.GetDatacatgoryName(_settingModel.DataSourceCategoryName); + // 获取原始数据 + var listLogics = _runningDataCache.SecondaryCircuitLogicExpressionDic[haskey]; - var ycLiveData = _redisDataRepository.TelemeteringModelListRedis.HashSetGetOne(telemeteringredisKey, logichasKey); - if (ycLiveData != null) - { - listLogic.LogicExpression.Replace(logichasKey, ycLiveData.ResultValue.ToString()); - } - string telesignalisationredisKey = _redisDataRepository.TelesignalisationModelListRediskey + "_" + _settingModel.GetDatacatgoryName(_settingModel.DataSourceCategoryName); + // 创建副本列表 + var copiedLogics = listLogics + .Select(logic => new LogicExpressionTelesignalisation + { + LogicExpression = logic.LogicExpression, + TelesignalisationAddr = logic.TelesignalisationAddr + }) + .ToList(); - var yxLiveData = _redisDataRepository.TelesignalisationModelListRedis.HashSetGetOne(telesignalisationredisKey, logichasKey); - if (yxLiveData != null) + foreach (var listLogic in copiedLogics) { - listLogic.LogicExpression.Replace(logichasKey, yxLiveData.ResultValue.ToString()); + string pattern = @"\{([^}]*)\}"; + Regex regex = new Regex(pattern); + + // 提取匹配内容 + MatchCollection matches = regex.Matches(listLogic.LogicExpression); + foreach (Match match in matches) + { + var logichasKey = match.Groups[1].Value; + string telemeteringredisKey = _redisDataRepository.TelemeteringModelListRediskey + "_" + _settingModel.GetDatacatgoryName(_settingModel.DataSourceCategoryName); + + var ycLiveData = _redisDataRepository.TelemeteringModelListRedis.HashSetGetOne(telemeteringredisKey, logichasKey); + if (ycLiveData != null) + { + listLogic.LogicExpression = listLogic.LogicExpression.Replace(logichasKey, ycLiveData.ResultValue.ToString()); + } + string telesignalisationredisKey = _redisDataRepository.TelesignalisationModelListRediskey + "_" + _settingModel.GetDatacatgoryName(_settingModel.DataSourceCategoryName); + + var yxLiveData = _redisDataRepository.TelesignalisationModelListRedis.HashSetGetOne(telesignalisationredisKey, logichasKey); + if (yxLiveData != null) + { + if (yxLiveData.RemoteType == RemoteTypeEnum.DoublePoint) + { + yxLiveData.ResultValue = yxLiveData.ResultValue > 2 ? 0 : yxLiveData.ResultValue - 1; + } + else + { + yxLiveData.ResultValue = yxLiveData.ResultValue > 2 ? 0 : yxLiveData.ResultValue; + } + listLogic.LogicExpression = listLogic.LogicExpression.Replace(logichasKey, yxLiveData.ResultValue.ToString()); + } + } + + try + { + listLogic.LogicExpression = listLogic.LogicExpression.Replace("{", "").Replace("}", ""); + var result = Eval.Execute(listLogic.LogicExpression); + if (result) + { + string telesignalisationredisKey = _redisDataRepository.TelesignalisationModelListRediskey + "_" + _settingModel.GetDatacatgoryName("无"); + var yxData = _redisDataRepository.TelesignalisationModelListRedis.HashSetGetOne(telesignalisationredisKey, listLogic.TelesignalisationAddr); + yxData.ResultValue = yxData.RemoteType == RemoteTypeEnum.DoublePoint ? 2 : 1; + _webApiRequest.SendVisualYx(yxData); + } + MonitoringEventBus.LogHandler($"判定结果为{result} 判定表达式{listLogic.LogicExpression}", "二次回路遥信判定"); + } + catch (Exception ex) + { + MonitoringEventBus.LogHandler(ex.Message, "错误信息"); + } } } - try - { - var result = Eval.Execute(listLogic.LogicExpression); - if (result) - { - string telesignalisationredisKey = _redisDataRepository.TelesignalisationModelListRediskey + "_" + _settingModel.GetDatacatgoryName("无"); - var yxData = _redisDataRepository.TelesignalisationModelListRedis.HashSetGetOne(telesignalisationredisKey, listLogic.TelesignalisationAddr); - yxData.ResultValue = yxData.RemoteType == RemoteTypeEnum.DoublePoint ? 2 : 1; - _webApiRequest.SendVisualYx(yxData); - } - } - catch (Exception ex) - { - MonitoringEventBus.LogHandler(ex.Message, "错误信息"); - } - } + }); } + return default; } + private Dictionary TelesignaleAlarmTempBuffDic = new Dictionary(); /// /// 发送装置自检信息 diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs index 0e5f6fb..1ea5a9a 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs @@ -173,7 +173,7 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis var list = _webApiRequest.GetSecondaryCircuitLogicExpressionList(); foreach (var item in list) { - if (!ycCheckList.ContainsKey(item.TelemeteringConfigurationId.Value)|| yxSendList.ContainsKey(item.TelesignalisationConfigurationId.Value)) + if (!ycCheckList.ContainsKey(item.TelemeteringConfigurationId.Value)|| !yxSendList.ContainsKey(item.TelesignalisationConfigurationId.Value)) { Debug.WriteLine("错误,没有找到遥测/遥信"); continue; @@ -214,7 +214,7 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis string pattern1 = @"\{([^}]*)\}"; Regex regex1 = new Regex(pattern1); // 提取匹配结果 - MatchCollection matches = regex.Matches(item.LogicalExpression); + MatchCollection matches = regex1.Matches(item.LogicalExpression); List extractedValues = new List(); var logicalExpression = $"({item.LogicalExpression})>={lowerLimit}&&({item.LogicalExpression})<={upperLimit}"; foreach (Match match in matches) diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/WebApiRequest.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/WebApiRequest.cs index 959c776..542376e 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/WebApiRequest.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/WebApiRequest.cs @@ -522,23 +522,27 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis /// /// 初始遥信遥测redis列表 /// - public void InitYXYCRedisList(DataSourceCategoryEnum dataSourceCategory) + public async Task InitYXYCRedisList(DataSourceCategoryEnum dataSourceCategory) { try { - var resObj = ToolLibrary + await _redisDataRepository.DeleCheckRedisCache(); + await Task.Delay(10); + var resObj = ToolLibrary .HttpHelper .HttpGetRequest (ConstantModel .RequestInitYXRedisListUri+ "?dataSourceCategory="+ (int)dataSourceCategory ); - + await Task.Delay(10); var resObj1 = ToolLibrary .HttpHelper .HttpGetRequest (ConstantModel .RequestInitYCRedisListUri + "?dataSourceCategory=" + (int)dataSourceCategory ); + await Task.Delay(10); + var resObj2 = ToolLibrary .HttpHelper @@ -546,13 +550,15 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis (ConstantModel .RequestInitYCRedisListUri + "?dataSourceCategory=" + (int)DataSourceCategoryEnum.None ); + await Task.Delay(10); + var resObj3 = ToolLibrary .HttpHelper .HttpGetRequest (ConstantModel .RequestInitYXRedisListUri + "?dataSourceCategory=" + (int)DataSourceCategoryEnum.None ); - + await Task.Delay(200); } catch (Exception ex) { diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataCenter/RedisRepository.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataCenter/RedisRepository.cs index e9740cc..6ba636b 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataCenter/RedisRepository.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataCenter/RedisRepository.cs @@ -124,5 +124,21 @@ namespace Yunda.ISAS.DataMonitoringServer.DataCenter var yxDatas = await TelesignalisationModelListRedis.HashSetGetAllAsync(redisKey); return yxDatas; } + + public async Task DeleCheckRedisCache() + { + if (true) + { + string redisKey = TelemeteringModelListRediskey + "_" + _settingModel.GetDatacatgoryName("无"); ; + // 从 Redis 中批量获取所有遥测数据 + await TelemeteringModelListRedis.DeleteKeyAsync(redisKey); + } + if (true) + { + string redisKey = TelesignalisationModelListRediskey + "_" + _settingModel.GetDatacatgoryName("无"); ; + // 从 Redis 中批量获取所有遥测数据 + await TelesignalisationModelListRedis.DeleteKeyAsync(redisKey); + } + } } } diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/FTPHandle/FtpFile.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/FTPHandle/FtpFile.cs index 3607e71..96b8c4b 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/FTPHandle/FtpFile.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/FTPHandle/FtpFile.cs @@ -39,9 +39,9 @@ namespace Yunda.SOMS.DataMonitoringServer.FTPHandle /// 要下载的文件名 /// 本地保存路径 /// 返回下载的文件路径或上次保存的文件路径 - public string GetFileFromFtp(string ftpHost, string fileapth, string fileName, string username = "root", string password = "root") + public string GetFileFromFtp(string ftpHost, string fileapth, string fileName, string deviceAddr,string username = "root", string password = "root") { - string localFilePath = Path.Combine(localDirectory, fileName); // 本地保存文件的完整路径 + string localFilePath = Path.Combine(localDirectory, deviceAddr, fileName); // 本地保存文件的完整路径 string remoteFilePath = Path.Combine(fileapth, fileName); // 远程FTP文件的路径 using (var client = new FtpClient(ftpHost, username, password)) { diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceIOInfoHandle.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceIOInfoHandle.cs index 933a32a..cd2a3cb 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceIOInfoHandle.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceIOInfoHandle.cs @@ -115,7 +115,7 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle string fileName = Path.GetFileName(result); // 获取路径 string directoryPath = Path.GetDirectoryName(result); - var localFile = _ftpFile.GetFileFromFtp(device.GatewayIP1, directoryPath, fileName); + var localFile = _ftpFile.GetFileFromFtp(device.GatewayIP1, directoryPath, fileName, address.ToString()); if (File.Exists(localFile)) { diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs index f763f40..77c4376 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs @@ -61,7 +61,7 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle if (_commCount == 0) { var device = _devices.FirstOrDefault(t => t.DeviceAddr == address); - var localFile = _ftpFile.GetFileFromFtp(device.GatewayIP1, "/nor/root/status/", "status.txt"); + var localFile = _ftpFile.GetFileFromFtp(device.GatewayIP1, "/nor/root/status/", "status.txt",address.ToString()); if (File.Exists(localFile)) { var data = ParseDeviceStatus(localFile); 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 7523032..7c3ceac 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyServerHandler.cs @@ -3,6 +3,7 @@ using DotNetty.Transport.Channels; using MySqlX.XDevAPI; using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Text; @@ -25,7 +26,7 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server private IChannelHandlerContext _context; // 定义事件,事件处理程序包含消息和功能描述 - private readonly Dictionary _connections; + private readonly ConcurrentDictionary _connections; private readonly Action _onMessageReceived; private readonly Action _onDeviceConnection; @@ -34,7 +35,7 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server Dictionary _deviceBoardStates = new Dictionary(); //0-离线 1-在线 - public DotNettyServerHandler(Dictionary connections, + public DotNettyServerHandler(ConcurrentDictionary connections, Action onMessageReceived, Action> deviceBoardStatesAction) { _connections = connections; @@ -88,17 +89,20 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server _deviceBoardStates[deviceAddress][0] = 0; MonitoringEventBus.LogHandler($"[{DateTime.Now}] {deviceAddress} 判定为离线(连续离线状态)", "定值错误信息"); await _connections[deviceAddress].CloseAsync(); - _connections.Remove(deviceAddress); + _connections.TryRemove(deviceAddress, out IChannelHandlerContext channelHandlerContext); continue; } // 检查是否超过10秒未更新 if ((DateTime.Now - device.LastUpdate).TotalSeconds > 10) { - _deviceBoardStates[deviceAddress][0] = 0; - MonitoringEventBus.LogHandler($"[{DateTime.Now}] {deviceAddress} 判定为离线(超过10秒未更新)", "定值错误信息"); - await _connections[deviceAddress].CloseAsync(); - _connections.Remove(deviceAddress); + if (_connections.ContainsKey(deviceAddress)) + { + _deviceBoardStates[deviceAddress][0] = 0; + MonitoringEventBus.LogHandler($"[{DateTime.Now}] {deviceAddress} 判定为离线(超过10秒未更新)", "定值错误信息"); + await _connections[deviceAddress].CloseAsync(); + _connections.TryRemove(deviceAddress, out IChannelHandlerContext channelHandlerContext); + } continue; } _deviceBoardStates[deviceAddress][0] = 1; @@ -236,7 +240,7 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server else { - _connections.Add(address, ctx); + _connections.TryAdd(address, ctx); for (byte i = 1; i < 6; i++) { await SendCustomMessageAsync(ctx, address, 0, 5, i); diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyTcpServer.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyTcpServer.cs index 04f4526..295d819 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyTcpServer.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/TcpSocket/Server/DotNettyTcpServer.cs @@ -15,12 +15,13 @@ using System.Diagnostics; using System.Windows.Media.Animation; using YunDa.ISAS.Entities.System; using Yunda.ISAS.DataMonitoringServer.DataAnalysis; +using System.Collections.Concurrent; namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server { public class DotNettyTcpServer : ISingletonDependency { - private readonly Dictionary connections = new Dictionary(); + private readonly ConcurrentDictionary connections = new ConcurrentDictionary(); // 定义 MessageReceived 事件 public event Action< byte, byte[], byte> MessageReceived; // 装置地址,功能码,消息 @@ -137,7 +138,7 @@ namespace Yunda.SOMS.DataMonitoringServer.TcpSocket.Server if (connections.TryGetValue(addr, out IChannelHandlerContext context)) { context.CloseAsync(); // 关闭连接 - connections.Remove(addr); // 从字典中移除该连接 + connections.TryRemove(addr,out IChannelHandlerContext channelHandlerContext); // 从字典中移除该连接 Debug.WriteLine($"断开 地址 为 {addr} 的连接"); } else diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/View/TestWindow.xaml.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/View/TestWindow.xaml.cs index 9701eea..530fa85 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/View/TestWindow.xaml.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/View/TestWindow.xaml.cs @@ -260,7 +260,7 @@ namespace Yunda.SOMS.DataMonitoringServer.WPF.View string filepath = "/nor/root/commcpu/cfg/"; // 调用函数并传入文件名 string fileName = "iodb_tp1.sql3"; // 传入的文件名 - string localFile = _ftpFile. GetFileFromFtp(ftpBaseUrl, ftpUsername, ftpPassword, filepath, fileName); + string localFile = _ftpFile. GetFileFromFtp(ftpBaseUrl, ftpUsername, ftpPassword, "1000",filepath, fileName); IODataHandle commDataHandle = new IODataHandle(); var data = commDataHandle.GetIODataDic(localFile); //DisplayData(dictionary: data.Item2 ); diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/ViewModel/MainViewModel.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/ViewModel/MainViewModel.cs index 1e0d955..793a68a 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/ViewModel/MainViewModel.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/WPF/ViewModel/MainViewModel.cs @@ -245,7 +245,7 @@ namespace Yunda.ISAS.DataMonitoringServer.WPF.ViewModel try { _showMsgActionBlock.Post(new Recoder() { MsgType = "启动信息", Msg = "开始连接远动机,请不要重复点击开始按钮,请稍等....", DateTime = DateTime.Now }); - _webApiRequest.UpdateMonitorServerAddressById(); + Task.Run(()=> _webApiRequest.UpdateMonitorServerAddressById()) ; _webApiRequest.InitYXYCRedisList((DataSourceCategoryEnum)SettingModel.GetDatacatgoryValue(SettingModel.DataSourceCategoryName)); monitoringDataService.DataServiceStart(settingModel); IsRunning = true; diff --git a/test/RedisSubscriberConsole/Program.cs b/test/RedisSubscriberConsole/Program.cs index bc1da0d..73af5d0 100644 --- a/test/RedisSubscriberConsole/Program.cs +++ b/test/RedisSubscriberConsole/Program.cs @@ -45,19 +45,8 @@ class Subscriber // 解析 JSON 消息 var entity = MessagePackSerializer.Deserialize>(message, MessagePack.Resolvers.ContractlessStandardResolver.Options); - - try - { - Console.WriteLine("解析到的键值对:"); - foreach (var kvp in entity) - { - Console.WriteLine($"{kvp.Key}: {kvp.Value}"); - } - } - catch (Exception ex) - { - Console.WriteLine(ex.StackTrace); - } + ProcessDictionary(entity); + }); @@ -66,7 +55,31 @@ class Subscriber conmper.Dispose(); } - + static void ProcessDictionary(Dictionary dictionary) + { + foreach (var kvp in dictionary) + { + Console.Write($"{kvp.Key}: "); + if (kvp.Value is byte[] nestedMessagePack) + { + try + { + // 进一步解析嵌套的 MessagePack 数据 + var nestedDict = MessagePackSerializer.Deserialize>(nestedMessagePack, MessagePack.Resolvers.ContractlessStandardResolver.Options); + Console.WriteLine(); + ProcessDictionary(nestedDict); // 递归解析 + } + catch + { + Console.WriteLine("(嵌套内容无法解析)"); + } + } + else + { + Console.WriteLine(kvp.Value); + } + } + } public class Message { public string name { get; set; }