|
|
|
|
@ -40,11 +40,11 @@ public class DeviceCollectionBusiness
|
|
|
|
|
|
|
|
|
|
private readonly BaseDeviceInfoCacheService _cacheService;
|
|
|
|
|
|
|
|
|
|
private readonly List<IOpcService> _opcs;
|
|
|
|
|
private readonly Dictionary<BaseDeviceInfo, IOpcService> _opcs;
|
|
|
|
|
|
|
|
|
|
private readonly IBaseService<BaseDeviceParamVal> _paramValService;
|
|
|
|
|
|
|
|
|
|
public DeviceCollectionBusiness(SerilogHelper serilogHelper, BaseDeviceInfoCacheService cacheService, List<IOpcService> opcs, IBaseService<BaseDeviceParamVal> paramValService)
|
|
|
|
|
public DeviceCollectionBusiness(SerilogHelper serilogHelper, BaseDeviceInfoCacheService cacheService, Dictionary<BaseDeviceInfo, IOpcService> opcs, IBaseService<BaseDeviceParamVal> paramValService)
|
|
|
|
|
{
|
|
|
|
|
_serilog = serilogHelper;
|
|
|
|
|
_cacheService = cacheService;
|
|
|
|
|
@ -54,69 +54,109 @@ public class DeviceCollectionBusiness
|
|
|
|
|
//this.Handle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Handle2()
|
|
|
|
|
public async Task Handle()
|
|
|
|
|
{
|
|
|
|
|
bool isFalg = true;
|
|
|
|
|
bool isFlag = 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)
|
|
|
|
|
|
|
|
|
|
foreach (var (item, opcua) in _opcs)
|
|
|
|
|
{
|
|
|
|
|
//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);
|
|
|
|
|
_serilog.Info($"开始采集{item.deviceName}");
|
|
|
|
|
var opcItemValues = await this.ReadParam(item, opcua);
|
|
|
|
|
|
|
|
|
|
//this.SaveParam(item, opcItemValues, out List<DeviceParamValueDto> paramValues);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i == 1000)
|
|
|
|
|
{
|
|
|
|
|
isFalg = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await opcUa.DisconnectAsync();
|
|
|
|
|
Thread.Sleep(2000);
|
|
|
|
|
|
|
|
|
|
_serilog.Info($"读取完成断开连接");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1000 * 1);
|
|
|
|
|
} while (isFalg);
|
|
|
|
|
} 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>
|
|
|
|
|
@ -132,20 +172,8 @@ public class DeviceCollectionBusiness
|
|
|
|
|
|
|
|
|
|
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 opcUa.ReadNodeAsync(deviceParams);
|
|
|
|
|
|
|
|
|
|
//var infos = _opcService.BrowseNodesAsync("ns=2;s=Devices/分厂一/车间一/测试空设备");
|
|
|
|
|
|
|
|
|
|
//await _opcUaService.DisconnectAsync();
|
|
|
|
|
|
|
|
|
|
return infos;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@ -162,67 +190,34 @@ public class DeviceCollectionBusiness
|
|
|
|
|
/// <param name="opcItemValues">OPC节点值列表</param>
|
|
|
|
|
/// <param name="paramValues">输出参数值DTO列表</param>
|
|
|
|
|
public void SaveParam(BaseDeviceInfo device, List<OpcNode> opcItemValues,
|
|
|
|
|
out List<DeviceParamValueDto> paramValues)
|
|
|
|
|
out List<BaseDeviceParamVal> paramValues)
|
|
|
|
|
{
|
|
|
|
|
var deviceParams = device.deviceParams.ToList();
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
// 使用字典提高查找效率
|
|
|
|
|
var opcValueDict = opcItemValues.ToDictionary(v => v.NodeId, v => v);
|
|
|
|
|
|
|
|
|
|
// 构建参数值列表和数据库实体列表
|
|
|
|
|
var paramValueList = new List<DeviceParamValueDto>();
|
|
|
|
|
var dbEntityList = new List<BaseDeviceParamVal>();
|
|
|
|
|
|
|
|
|
|
foreach (var param in deviceParams)
|
|
|
|
|
paramValues = new List<BaseDeviceParamVal>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 查找对应的OPC值
|
|
|
|
|
opcValueDict.TryGetValue(param.paramAddr, out var opcNode);
|
|
|
|
|
|
|
|
|
|
var paramValue = opcNode?.Value?.ToString() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
// 构建DTO
|
|
|
|
|
paramValueList.Add(new DeviceParamValueDto
|
|
|
|
|
foreach (OpcNode opcItem in opcItemValues)
|
|
|
|
|
{
|
|
|
|
|
deviceCode = param.deviceCode,
|
|
|
|
|
paramCode = param.paramCode,
|
|
|
|
|
paramName = param.paramName,
|
|
|
|
|
netWork = param.netWork,
|
|
|
|
|
paramAddr = param.paramAddr,
|
|
|
|
|
paramType = param.paramType,
|
|
|
|
|
isFlag = param.isFlag,
|
|
|
|
|
paramValue = paramValue,
|
|
|
|
|
});
|
|
|
|
|
BaseDeviceParamVal deviceParamVal = new BaseDeviceParamVal();
|
|
|
|
|
|
|
|
|
|
// 构建数据库实体(批量插入)
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|