#region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2025 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:Mr.Wen's MacBook Pro * 命名空间:Sln.Imm.Daemon.Business * 唯一标识:2152B53A-F33E-47F0-9112-21E05DFC903D * * 创建者:WenJY * 电子邮箱: * 创建时间:2025-09-11 09:42:39 * 版本:V1.0.0 * 描述: * *-------------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using Newtonsoft.Json; using Sln.Imm.Daemon.Cache; using Sln.Imm.Daemon.Model.dao; using Sln.Imm.Daemon.Model.dto; using Sln.Imm.Daemon.Opc; using Sln.Imm.Daemon.Opc.Impl; using Sln.Imm.Daemon.Repository.service.@base; using Sln.Imm.Daemon.Serilog; namespace Sln.Imm.Daemon.Business; public class DeviceCollectionBusiness { private readonly SerilogHelper _serilog; private readonly BaseDeviceInfoCacheService _cacheService; private readonly List _opcs; private readonly IBaseService _paramValService; public DeviceCollectionBusiness(SerilogHelper serilogHelper, BaseDeviceInfoCacheService cacheService, List opcs, IBaseService paramValService) { _serilog = serilogHelper; _cacheService = cacheService; _opcs = opcs; _paramValService = paramValService; //this.Handle(); } public async Task Handle2() { bool isFalg = true; var deviceInfos = await _cacheService.GetValueAsync("BaseDeviceInfoCache"); _opcs. } public async Task Handle(IOpcService opcUa) { bool isFalg = true; var deviceInfos = await _cacheService.GetValueAsync("BaseDeviceInfoCache"); int i = 0; do { i++; foreach (var item in deviceInfos) { //bool result = await _opcUaService.ConnectAsync(item.networkAddress); //if (!result) //{ // throw new ArgumentNullException($"设备未连接"); //} //Task.Run(async () => //{ // //_opcUaService.DisconnectAsync(); //}); try { _serilog.Info($"开始采集{item.deviceName},设备数据,第{i}次"); var opcItemValues = await this.ReadParam(item, opcUa); //this.SaveParam(item, opcItemValues, out List paramValues); _serilog.Info($"{item.deviceName}数据采集完成:{JsonConvert.SerializeObject(opcItemValues)}"); } catch (Exception e) { _serilog.Info($"{item.deviceName}数据读取异常:{e.Message}"); } } if (i == 1000) { isFalg = false; await opcUa.DisconnectAsync(); _serilog.Info($"读取完成断开连接"); } await Task.Delay(1000 * 1); } while (isFalg); } /// /// 读取设备参数 /// /// public async Task> ReadParam(BaseDeviceInfo device,IOpcService opcUa) { try { if (device == null) { throw new ArgumentNullException($"设备信息不允许为空"); } List deviceParams = device.deviceParams.Select(x => x.paramAddr).ToList(); //bool result = await _opcUaService.ConnectAsync(device.networkAddress); //if (!result) //{ // throw new ArgumentNullException($"设备未连接"); //} List infos = await opcUa.ReadNodeAsync(deviceParams); //var infos = _opcService.BrowseNodesAsync("ns=2;s=Devices/分厂一/车间一/测试空设备"); //await _opcUaService.DisconnectAsync(); return infos; } catch (Exception e) { throw new InvalidOperationException($"设备参数读取异常:{e.Message}"); } } /// /// 保存设备参数值到数据库 /// /// 设备信息 /// OPC节点值列表 /// 输出参数值DTO列表 public void SaveParam(BaseDeviceInfo device, List opcItemValues, out List paramValues) { var deviceParams = device.deviceParams.ToList(); var now = DateTime.Now; // 使用字典提高查找效率 var opcValueDict = opcItemValues.ToDictionary(v => v.NodeId, v => v); // 构建参数值列表和数据库实体列表 var paramValueList = new List(); var dbEntityList = new List(); foreach (var param in deviceParams) { // 查找对应的OPC值 opcValueDict.TryGetValue(param.paramAddr, out var opcNode); var paramValue = opcNode?.Value?.ToString() ?? string.Empty; // 构建DTO paramValueList.Add(new DeviceParamValueDto { deviceCode = param.deviceCode, paramCode = param.paramCode, paramName = param.paramName, netWork = param.netWork, paramAddr = param.paramAddr, paramType = param.paramType, isFlag = param.isFlag, paramValue = paramValue, }); // 构建数据库实体(批量插入) dbEntityList.Add(new BaseDeviceParamVal { DEVICE_ID = device.objid, DEVICE_CODE = device.deviceCode, PARAM_CODE = param.paramCode, PARAM_NAME = param.paramName, PARAM_VALUE = paramValue, COLLECT_TIME = now, RECORD_TIME = now, PARAM_TYPE = param.paramType, }); } paramValues = paramValueList; // 批量保存到数据库(最高效的方式) if (dbEntityList.Count > 0) { try { _paramValService.Insert(dbEntityList); _serilog.Info($"设备{device.deviceCode}成功保存{dbEntityList.Count}条参数值到数据库"); } catch (Exception ex) { _serilog.Info($"设备{device.deviceCode}保存参数值到数据库失败:{ex.Message}"); throw; } } } }