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>(x => { var _cacheService = x.GetService(); List deviceInfos = _cacheService.GetValueAsync("BaseDeviceInfoCache").Result; Dictionary opcs = new Dictionary(); if (deviceInfos != null) { foreach (var item in deviceInfos) { if (item.isFlag == 1) { IOpcService opcDev = null; if (item.deviceFacture.Contains("伊之密")) { opcDev = x.GetService(); } else { opcDev = x.GetService(); } if (opcDev != null) { var res = opcDev.ConnectAsync(item.networkAddress).Result; if (!res) { Console.WriteLine($"{item.deviceName}设备连接失败"); } else { opcs.Add(item, opcDev); } } } } } return opcs; }); } } }