using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using Sln.Wcs.ElevatorSdk.Dto; using Sln.Wcs.Plc; using Serilog; using Sln.Wcs.Plc.Factory; namespace Sln.Wcs.ElevatorSdk { public static class ElevatorSetup { public static void AddElevatorSetup(this IServiceCollection services) { services.AddSingleton>(x => { var info = x.GetService>(); List ElevatorFactories = new List(); try { do { if (info != null) { foreach (var item in info) { if (item.IsEnabled) { PlcAbsractFactory _plc = InitPlc(x, item.plcType); var connectResult = _plc.Connect(item.plcIp, item.plcPort); if (connectResult) { Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } else { Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } _plc.ConfigKey = item.plcKey; if (ElevatorFactories.Contains(_plc)) { ElevatorFactories.Remove(_plc); } ElevatorFactories.Add(_plc); } } } } while (false); } catch (Exception e) { Log.Error($"PLC初始化连接异常:{e.Message}"); } return ElevatorFactories; }); } private static PlcAbsractFactory InitPlc(IServiceProvider serviceProvider, string plcType) { PlcAbsractFactory _plc = null; var _inovance = serviceProvider.GetRequiredService(); switch (plcType) { case "InovancePlc": _plc = _inovance; break; case "MelsecBinaryPlc": //_plc = _melsecBinary; break; case "OmronNJPlc": //_plc = _omronNj; break; case "SiemensPlc": //_plc = _siemens; break; default: break; } return _plc; } } }