diff --git a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/BoardCardDevice/BoardCardInfoAppService.cs b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/BoardCardDevice/BoardCardInfoAppService.cs index 57c05b2..ea7280a 100644 --- a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/BoardCardDevice/BoardCardInfoAppService.cs +++ b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/BoardCardDevice/BoardCardInfoAppService.cs @@ -396,7 +396,7 @@ namespace YunDa.ISAS.Application.GeneralInformation { eventDescription.AppendLine($"更改了设备的生产日期: {deviceBoardCard.ProductionDate} -> {input.ProductionDate}"); } - boardHistory.EventDescription = eventDescription.ToString(); + boardHistory.EventDescription = eventDescription.ToString().TrimEnd(','); boardHistory.EventRecordType = EventRecordTypeEnum.Board; boardHistory.Remark = input.Remark; _boardCardHistoryRepository.Insert(boardHistory); @@ -807,7 +807,7 @@ namespace YunDa.ISAS.Application.GeneralInformation RecodeDate = DateTime.Now, ContentJson = json, ContentNewJson = newJson, - EventDescription = eventDescription.ToString(), + EventDescription = eventDescription.ToString().TrimEnd(','), EventRecordType = eventRecordType, SeqNo = deviceBoards.Count > 0 ? deviceBoards.Max(t => t.SeqNo) + 1 : 1, IsSend = false // 默认为未发送,您可以根据需要调整 diff --git a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtectionDeviceAppService.cs b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtectionDeviceAppService.cs index d604fc0..692405b 100644 --- a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtectionDeviceAppService.cs +++ b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtectionDeviceAppService.cs @@ -394,7 +394,7 @@ namespace YunDa.ISAS.Application.GeneralInformation eventDescription.AppendLine($"更改了设备的生产日期: {protectionDevice.EquipmentInfo.ProductionDate} -> {input.ProductionDate}"); } deviceHistory.Name = protectionDevice.Name; - deviceHistory.EventDescription = eventDescription.ToString(); + deviceHistory.EventDescription = eventDescription.ToString().TrimEnd(','); deviceHistory.EventRecordType = EventRecordTypeEnum.Device; deviceHistory.Remark = input.Remark; _protectionDeviceHistoryRepository.Insert(deviceHistory); @@ -1271,6 +1271,7 @@ namespace YunDa.ISAS.Application.GeneralInformation } return rst; } + #if DEBUG /// /// 填充出厂编号 diff --git a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtecttionDeviceRedisAppService.cs b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtecttionDeviceRedisAppService.cs index 03cbdb4..f772f48 100644 --- a/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtecttionDeviceRedisAppService.cs +++ b/src/YunDa.Application/YunDa.ISAS.Application/GeneralInformation/ProtectionDevice/ProtecttionDeviceRedisAppService.cs @@ -1,4 +1,5 @@ using Abp.Application.Services; +using Abp.Auditing; using Abp.Authorization; using Abp.Collections.Extensions; using Abp.Domain.Repositories; @@ -9,10 +10,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ToolLibrary.LogHelper; using YunDa.ISAS.Application.Core; using YunDa.ISAS.Application.Core.Session; using YunDa.ISAS.Application.Core.SwaggerHelper; using YunDa.ISAS.DataTransferObject; +using YunDa.ISAS.Redis.Repositories; +using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto; using YunDa.SOMS.DataTransferObject.MainStationMaintenanceInfo.OperationReport; using YunDa.SOMS.Entities.GeneralInformation; @@ -25,16 +29,18 @@ namespace YunDa.ISAS.Application.GeneralInformation.ProtectionDevice private readonly IRepository _secondaryCircuitProtectionDeviceRepository; - + private readonly IRedisRepository _protectionDeviceCommInfoRedis; public ProtecttionDeviceRedisAppService(ISessionAppService sessionAppService, IRepository secondaryCircuitRepository, IRepository protectionDeviceInfoRepository, + IRedisRepository protectionDeviceCommInfoRedis, IRepository secondaryCircuitProtectionDeviceRepository ) : base(sessionAppService) { _protectionDeviceInfoRepository = protectionDeviceInfoRepository; _secondaryCircuitRepository = secondaryCircuitRepository; _secondaryCircuitProtectionDeviceRepository = secondaryCircuitProtectionDeviceRepository; + _protectionDeviceCommInfoRedis = protectionDeviceCommInfoRedis; } /// /// 获取装置自检信息 @@ -198,6 +204,35 @@ namespace YunDa.ISAS.Application.GeneralInformation.ProtectionDevice } + return rst; + } + /// + /// 查询保护装置在线情况 + /// + /// + /// + [AbpAllowAnonymous] + [HttpGet] + [DisableAuditing] + [ShowApi] + public async Task>> GetProtectionDeviceCommInfoAsync(Guid stationId) + { + string protectionDeviceCommInfoRedisKey = "protectionDeviceCommInfo"; + + RequestResult> rst = new RequestResult>(); + try + { + var datas = await _protectionDeviceCommInfoRedis.HashSetGetAllAsync(protectionDeviceCommInfoRedisKey); + rst.ResultData = datas.ToList(); + rst.Flag = true; + } + catch (Exception ex) + { + rst.Message = ex.Message; + + Log4Helper.Error(this.GetType(), "查询保护装置在线情况", ex); + } + return rst; } } diff --git a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/GeneralInformation/ProtectionDeviceInfoDto/ProtectionDeviceCommInfoOutput.cs b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/GeneralInformation/ProtectionDeviceInfoDto/ProtectionDeviceCommInfoOutput.cs index aaffa9e..35132c1 100644 --- a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/GeneralInformation/ProtectionDeviceInfoDto/ProtectionDeviceCommInfoOutput.cs +++ b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/GeneralInformation/ProtectionDeviceInfoDto/ProtectionDeviceCommInfoOutput.cs @@ -1,7 +1,9 @@ -using System; +using MessagePack; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto @@ -9,6 +11,7 @@ namespace YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoD /// /// 保护装置通信地址 /// + [MessagePackObject(keyAsPropertyName: true)] public class ProtectionDeviceCommInfoOutput { /// @@ -16,7 +19,6 @@ namespace YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoD /// public Guid EquipmentInfoId { get; set; } public string EquipmentInfoName { get; set; } - /// /// 保护装置id /// @@ -37,6 +39,11 @@ namespace YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoD public string Sntp1 { get; set; } public string Sntp2 { get; set; } public bool IsZY { get; set; } + //[MessagePackFormatter(typeof(DateTimeAsUnixTimeFormatter))] + [IgnoreMember] + [JsonIgnore] + public DateTime LastComTime { get; set; } + public bool IsOnline { get; set; } } } diff --git a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/YunDa.ISAS.DataTransferObject.xml b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/YunDa.ISAS.DataTransferObject.xml index d3e7b2e..8c558e2 100644 --- a/src/YunDa.Application/YunDa.ISAS.DataTransferObject/YunDa.ISAS.DataTransferObject.xml +++ b/src/YunDa.Application/YunDa.ISAS.DataTransferObject/YunDa.ISAS.DataTransferObject.xml @@ -15670,6 +15670,16 @@ 信号类型的枚举 + + + 装置信息 + + + + + 基线版本 + + 设备参数信息 @@ -15700,6 +15710,11 @@ 板卡信息 + + + 通信参数 + + 装置自检 diff --git a/src/YunDa.Domain/YunDa.ISAS.Redis/ISASRedisModule.cs b/src/YunDa.Domain/YunDa.ISAS.Redis/ISASRedisModule.cs index 4d41e8b..db9e420 100644 --- a/src/YunDa.Domain/YunDa.ISAS.Redis/ISASRedisModule.cs +++ b/src/YunDa.Domain/YunDa.ISAS.Redis/ISASRedisModule.cs @@ -103,8 +103,9 @@ namespace YunDa.ISAS.Redis IocManager.Register, string>, RedisRepository, string>>(); IocManager.Register, string>, RedisRepository, string>>(); IocManager.Register, RedisRepository>(); + IocManager.Register, RedisRepository>(); + - } } } diff --git a/src/YunDa.Domain/YunDa.ISAS.Redis/Repositories/RedisRepository.cs b/src/YunDa.Domain/YunDa.ISAS.Redis/Repositories/RedisRepository.cs index abaffc0..5994815 100644 --- a/src/YunDa.Domain/YunDa.ISAS.Redis/Repositories/RedisRepository.cs +++ b/src/YunDa.Domain/YunDa.ISAS.Redis/Repositories/RedisRepository.cs @@ -65,14 +65,13 @@ namespace YunDa.ISAS.Redis.Repositories } public async Task PublishAsync(string channel,TEntity entity) { - //string jsonMessage = JsonSerializer.Serialize(message); + var jsonMessage = GetSerializeObjectString(entity); //序列化 - // 显式指定 RedisChannel var redisChannel = new RedisChannel(channel, RedisChannel.PatternMode.Literal); - // 发布消息 return await _subscriber.PublishAsync(redisChannel, jsonMessage); } + public event Action OnMessageReceived; // 订阅接口,支持模式匹配 public void Subscribe(string pattern) diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs index a3c5f2c..fc0a4b5 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/DataAnalysis/MonitoringDataService.cs @@ -115,7 +115,6 @@ namespace Yunda.ISAS.DataMonitoringServer.DataAnalysis await _protectionDeviceDataCenter.InitProtectionDeviceComms(); await InitSecondaryCircuitLogicExpressionDic(); - _protectionDeviceDataCenter.InitDevices(); } _dataCollectionTask.CollectionStart(settingModel, startWebsocket);//开启数据采集 } diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceDataCenter.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceDataCenter.cs index d37d31b..3247484 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceDataCenter.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceDataCenter.cs @@ -11,6 +11,7 @@ using ToolLibrary.LogHelper; using Yunda.ISAS.DataMonitoringServer.DataAnalysis; using Yunda.SOMS.DataMonitoringServer.TcpSocket.Server; using YunDa.ISAS.Redis.Repositories; +using YunDa.SOMS.Commdb.Models; using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto; namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle { @@ -25,6 +26,8 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle ProtectionDeviceSelfCheckHandle _protectionDeviceSelfCheckHandle; ProtectionDeviceBCodeHandle _protectionDeviceBCodeHandle; IRedisRepository _deviceBoardStatesRedis; + IRedisRepository _protectionDeviceCommInfoRedis; + DotNettyTcpServer _dotNettyTcpServer; string deviceBoardStatesRedisKey = "deviceBoardStates"; ConcurrentBag _processes = new ConcurrentBag(); @@ -37,6 +40,7 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle , ProtectionDeviceRunInfoHandle protectionDeviceRunInfoHandle ,ProtectionDeviceBCodeHandle protectionDeviceBCodeHandle , IRedisRepository deviceBoardStatesRedis + , IRedisRepository protectionDeviceCommInfoRedis ) { _webApiRequest = webApiRequest; @@ -47,44 +51,34 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle _protectionDeviceRunInfoHandle = protectionDeviceRunInfoHandle; _deviceBoardStatesRedis = deviceBoardStatesRedis; _protectionDeviceSelfCheckHandle = protectionDeviceSelfCheckHandle; + _protectionDeviceCommInfoRedis = protectionDeviceCommInfoRedis; } - public void InitDevices() - { - try - { - _devices = _webApiRequest.GetProtectionDeviceCommInfos("神池南"); - } - catch (Exception ex) - { - Log4Helper.Error(this.GetType(), "初始化装置IO错误", ex); - } - } + - public List ProtectionDeviceComms { get; set; } public async Task InitProtectionDeviceComms() { MonitoringEventBus.LogHandler("开始启动103客户端", "装置定值"); await Task.Run(async () => - { - List devices = _webApiRequest.GetProtectionDeviceCommInfos("神池南"); - if (devices == null) + { + _devices = _webApiRequest.GetProtectionDeviceCommInfos("神池南"); + if (_devices == null) { MonitoringEventBus.LogHandler("没有获取到装置数据", "获取信息"); return; } - _protectionDeviceDZDataHandle.Init(devices); - MonitoringEventBus.LogHandler($"共获取{devices.Count}条装置数据", "装置定值"); + _protectionDeviceDZDataHandle.Init(_devices); + MonitoringEventBus.LogHandler($"共获取{_devices.Count}条装置数据", "装置定值"); string currentDirectory = AppDomain.CurrentDomain.BaseDirectory; string sourceDirectory = Path.Combine(currentDirectory, "DeviceComm"); string destDirectoryBase = Path.Combine(currentDirectory, "DeviceComms", "DeviceComm"); // 定义一个并行处理每个设备的过程 - Parallel.ForEach(devices, device => + Parallel.ForEach(_devices, device => { try { @@ -105,20 +99,36 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle } } - else + else if(IsDeviceOnline(device.GatewayIP2)) { - MonitoringEventBus.LogHandler($"装置地址:{device.DeviceAddr},ip:{device.GatewayIP1} {(online ? "在线" : "离线")}", "装置定值"); + string destDirectory = $"{destDirectoryBase}_{device.DeviceAddr}"; + CopyDirectory(sourceDirectory, destDirectory); + string configFilePath = Path.Combine(destDirectory, "cfg", "scmgateway.ini"); + ModifyDeviceAddrInConfig(configFilePath, device.GatewayIP2, device.DeviceAddr); + string exePath = Path.Combine(destDirectory, "scmgateway.exe"); + var process = StartExe(exePath); + if (process != null) + { + MonitoringEventBus.LogHandler($"装置地址:{device.DeviceAddr},ip:{device.GatewayIP2} 启动成功", "装置定值"); + _processes.Add(process); + } } + else + { + device.IsOnline = false; + MonitoringEventBus.LogHandler($"装置地址:{device.DeviceAddr},ip1:{device.GatewayIP1},ip2:{device.GatewayIP2} {(online ? "在线" : "离线")}", "装置状态"); + } + device.IsOnline = true; + } catch (Exception ex) { - MonitoringEventBus.LogHandler($"{ex.Message}", "错误信息"); - } }); - + string protectionDeviceCommInfoRedisKey = "protectionDeviceCommInfo"; + _protectionDeviceCommInfoRedis.HashSetUpdateManyAsync(protectionDeviceCommInfoRedisKey, _devices.Select(t => t.DeviceAddr.ToString()).ToList(), _devices); MonitoringEventBus.LogHandler("开始启动TCP服务端", "装置定值"); await _dotNettyTcpServer.RunServerAsync(); }); diff --git a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs index 72b14e2..3662931 100644 --- a/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs +++ b/src/YunDa.Server/Yunda.ISAS.DataMonitoringServer/ProtectionDeviceHandle/ProtectionDeviceRunInfoHandle.cs @@ -32,11 +32,14 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle DotNettyTcpServer _dotNettyTcpServer; private readonly RedisDataRepository _redisDataRepository; private readonly IRedisRepository _deviceBoardStatesRedis; + IRedisRepository _protectionDeviceCommInfoRedis; + public ProtectionDeviceRunInfoHandle( FtpFile ftpFile, DotNettyTcpServer dotNettyTcpServer, IRedisRepository redisRepository, WebApiRequest webApiRequest, + IRedisRepository protectionDeviceCommInfoRedis, RedisDataRepository redisDataRepository ) { @@ -46,12 +49,16 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle _redisDataRepository = redisDataRepository; _dotNettyTcpServer.MessageReceived += OnMessageReceived; // 订阅事件 _deviceBoardStatesRedis = redisRepository; + _protectionDeviceCommInfoRedis = protectionDeviceCommInfoRedis; } //Dictionary _deviceRunStates = new Dictionary(); //private ConcurrentDictionary _communicationStateCounts = new(); int _commCount = 0; - private void OnMessageReceived(byte address, byte[] message, byte functionType) + string protectionDeviceCommInfoRedisKey = "protectionDeviceCommInfo"; + string protectionDeviceCommInfoRedisChannel = "protectionDeviceCommInfosChannel"; + + private async void OnMessageReceived(byte address, byte[] message, byte functionType) { try { @@ -79,6 +86,13 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle } } } + device.LastComTime = DateTime.Now; + if (!device.IsOnline) + { + await _protectionDeviceCommInfoRedis.HashSetUpdateOneAsync(protectionDeviceCommInfoRedisKey, device.DeviceAddr.ToString(), device); + await _protectionDeviceCommInfoRedis.PublishAsync(protectionDeviceCommInfoRedisChannel, device); + } + device.IsOnline = true; } _commCount++; @@ -88,6 +102,15 @@ namespace Yunda.SOMS.DataMonitoringServer.ProtectionDeviceHandle } } + foreach (var item in ProtectionDeviceDataCenter._devices) + { + if (DateTime.Now - item.LastComTime>TimeSpan.FromSeconds(30)&& item.IsOnline) + { + item.IsOnline = false; + await _protectionDeviceCommInfoRedis.HashSetUpdateOneAsync(protectionDeviceCommInfoRedisKey, item.DeviceAddr.ToString(), item); + await _protectionDeviceCommInfoRedis.PublishAsync(protectionDeviceCommInfoRedisChannel, item); + } + } } catch (Exception ex) {