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.
73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Sln.Imm.Daemon.Cache;
|
|
using Sln.Imm.Daemon.Config;
|
|
using Sln.Imm.Daemon.Model.dao;
|
|
using Sln.Imm.Daemon.Opc.Impl;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sln.Imm.Daemon.Opc
|
|
{
|
|
public static class OpcDeviceFactorySetup
|
|
{
|
|
public static void AddOpcDeviceFactorySetup(this IServiceCollection services)
|
|
{
|
|
services.AddSingleton<Dictionary<BaseDeviceInfo, IOpcService>>(x =>
|
|
{
|
|
var _cacheService = x.GetService<BaseDeviceInfoCacheService>();
|
|
|
|
List<BaseDeviceInfo> deviceInfos = _cacheService.GetValueAsync("BaseDeviceInfoCache").Result;
|
|
|
|
Dictionary<BaseDeviceInfo, IOpcService> opcs = new Dictionary<BaseDeviceInfo, IOpcService>();
|
|
if (deviceInfos != null)
|
|
{
|
|
foreach (var item in deviceInfos)
|
|
{
|
|
if (item.isFlag == 1)
|
|
{
|
|
IOpcService opcDev = null;
|
|
if (item.deviceFacture.Contains("伊之密"))
|
|
{
|
|
opcDev = x.GetService<OpcUaService>();
|
|
}
|
|
else
|
|
{
|
|
opcDev = x.GetService<OpcDaService>();
|
|
}
|
|
|
|
if (opcDev != null)
|
|
{
|
|
try
|
|
{
|
|
var res = opcDev.ConnectAsync(item.networkAddress).Result;
|
|
|
|
if (!res)
|
|
{
|
|
Console.WriteLine($"{item.deviceName}设备连接失败");
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"{item.deviceName}设备连接成功");
|
|
opcs.Add(item, opcDev);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{item.deviceName}设备连接异常:{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return opcs;
|
|
});
|
|
}
|
|
}
|
|
}
|