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.

158 lines
4.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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.Serilog;
namespace Sln.Imm.Daemon.Business;
public class DeviceCollectionBusiness
{
private readonly SerilogHelper _serilog;
private readonly BaseDeviceInfoCacheService _cacheService;
private readonly IOpcService _opcUaService;
public DeviceCollectionBusiness(SerilogHelper serilogHelper, BaseDeviceInfoCacheService cacheService,OpcUaService opcUaService)
{
_serilog = serilogHelper;
_cacheService = cacheService;
_opcUaService = opcUaService;
this.Handle();
}
public async Task Handle()
{
bool isFalg = true;
var deviceInfos = await _cacheService.GetValueAsync("BaseDeviceInfoCache");
do
{
lock (string.Empty)
{
foreach (var item in deviceInfos)
{
Task.Run(async () =>
{
try
{
_serilog.Info($"开始采集{item.deviceName},设备数据");
var opcItemValues = await this.ReadParam(item);
this.SaveParam(item, opcItemValues, out List<DeviceParamValueDto> paramValues);
_serilog.Info($"{item.deviceName}数据采集完成:{JsonConvert.SerializeObject(paramValues)}");
}
catch (Exception e)
{
_serilog.Info($"{item.deviceName}数据读取异常:{e.Message}");
}
});
}
Task.Delay(1000 * 1).Wait();
}
} while (isFalg);
}
/// <summary>
/// 读取设备参数
/// </summary>
/// <param name="device"></param>
public async Task<List<OpcNode>> ReadParam(BaseDeviceInfo device)
{
try
{
if (device == null)
{
throw new ArgumentNullException($"设备信息不允许为空");
}
List<string> deviceParams = device.deviceParams.Select(x => x.paramAddr).ToList();
bool result = await _opcUaService.ConnectAsync(device.networkAddress);
if (!result)
{
throw new ArgumentNullException($"设备未连接");
}
List<OpcNode> infos = await _opcUaService.ReadNodeAsync(deviceParams);
//var infos = _opcService.BrowseNodesAsync("ns=2;s=Devices/分厂一/车间一/测试空设备");
await _opcUaService.DisconnectAsync();
return infos;
}
catch (Exception e)
{
throw new InvalidOperationException($"设备参数读取异常:{e.Message}");
}
}
/// <summary>
///
/// </summary>
/// <param name="device"></param>
/// <param name="opcItemValue"></param>
public void SaveParam(BaseDeviceInfo device, List<OpcNode> opcItemValues,
out List<DeviceParamValueDto> paramValues)
{
var deviceParams = device.deviceParams.ToList();
paramValues = deviceParams
.GroupJoin(opcItemValues,
param => param.paramAddr,
value => value.NodeId,
(param, values) => new { Param = param, Values = values })
.SelectMany(
x => x.Values.DefaultIfEmpty(),
(x, value) => new DeviceParamValueDto
{
deviceCode = x.Param.deviceCode,
paramCode = x.Param.paramCode,
paramName = x.Param.paramName,
netWork = x.Param.netWork,
paramAddr = x.Param.paramAddr,
paramType = x.Param.paramType,
isFlag = x.Param.isFlag,
paramValue = value.Value.ToString(),
})
.ToList();
//保存数据库
}
}