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.

223 lines
6.5 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.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 Dictionary<BaseDeviceInfo, IOpcService> _opcs;
private readonly IBaseService<BaseDeviceParamVal> _paramValService;
public DeviceCollectionBusiness(SerilogHelper serilogHelper, BaseDeviceInfoCacheService cacheService, Dictionary<BaseDeviceInfo, IOpcService> opcs, IBaseService<BaseDeviceParamVal> paramValService)
{
_serilog = serilogHelper;
_cacheService = cacheService;
_opcs = opcs;
_paramValService = paramValService;
//this.Handle();
}
public async Task Handle()
{
bool isFlag = true;
int i = 0;
do
{
i++;
foreach (var (item, opcua) in _opcs)
{
try
{
_serilog.Info($"开始采集{item.deviceName}");
var opcItemValues = await this.ReadParam(item, opcua);
this.SaveParam(item, opcItemValues, out List<BaseDeviceParamVal> paramValues);
_serilog.Info($"{item.deviceName}数据采集完成:{JsonConvert.SerializeObject(opcItemValues)}");
if(i == 10)
{
isFlag = false;
opcua.DisconnectAsync().Wait();
_serilog.Info($"读取完成断开连接");
}
}
catch (Exception e)
{
_serilog.Info($"{item.deviceName}数据读取异常:{e.Message}");
isFlag = false;
opcua.DisconnectAsync().Wait();
}
}
Thread.Sleep(2000);
} while (isFlag);
}
//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<DeviceParamValueDto> 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);
//}
/// <summary>
/// 读取设备参数
/// </summary>
/// <param name="device"></param>
public async Task<List<OpcNode>> ReadParam(BaseDeviceInfo device,IOpcService opcUa)
{
try
{
if (device == null)
{
throw new ArgumentNullException($"设备信息不允许为空");
}
List<string> deviceParams = device.deviceParams.Select(x => x.paramAddr).ToList();
List<OpcNode> infos = await opcUa.ReadNodeAsync(deviceParams);
return infos;
}
catch (Exception e)
{
throw new InvalidOperationException($"设备参数读取异常:{e.Message}");
}
}
/// <summary>
/// 保存设备参数值到数据库
/// </summary>
/// <param name="device">设备信息</param>
/// <param name="opcItemValues">OPC节点值列表</param>
/// <param name="paramValues">输出参数值DTO列表</param>
public void SaveParam(BaseDeviceInfo device, List<OpcNode> opcItemValues,
out List<BaseDeviceParamVal> paramValues)
{
paramValues = new List<BaseDeviceParamVal>();
try
{
foreach (OpcNode opcItem in opcItemValues)
{
BaseDeviceParamVal deviceParamVal = new BaseDeviceParamVal();
var paramInfo = device.deviceParams.Where(x => x.paramAddr == opcItem.NodeId).FirstOrDefault();
if (paramInfo != null)
{
deviceParamVal.paramCode = paramInfo.paramCode;
deviceParamVal.paramName = paramInfo.paramName;
}
deviceParamVal.deviceCode = device.deviceCode;
deviceParamVal.deviceId = device.objid;
deviceParamVal.paramValue = opcItem.Value.ToString();
deviceParamVal.paramType = opcItem.DataType;
deviceParamVal.collectTime = DateTime.Now;
paramValues.Add(deviceParamVal);
}
_paramValService.Insert(paramValues);
}catch (Exception e)
{
throw new InvalidOperationException($"设备参数保存异常:{e.Message}");
}
}
}