using HighWayIot.Log4net; using HighWayIot.Plc.Impl; using System; using System.Collections.Generic; namespace HighWayIot.Plc { public sealed class PlcPool { private static readonly Lazy lazy = new Lazy(() => new PlcPool()); public static PlcPool Instance { get { return lazy.Value; } } private LogHelper logHelper = LogHelper.Instance; private Dictionary keyValuePairs = new Dictionary(); private PlcPool() { } /// /// 初始化Plc /// /// /// /// /// public void InitPlc(string plcType, string ip, int port, string key) { IPlc _plc = null; switch (plcType) { case "InovancePlc": _plc = new InovancePlc(); break; case "MelsecBinaryPlc": _plc = new MelsecBinaryPlc(); break; case "OmronNJPlc": _plc = new OmronNJPlc(); break; case "SiemensPlc": _plc = new SiemensPlc(); break; default: break; } var connectResult = _plc.Connect(ip, port); if (connectResult) { keyValuePairs.Add(key, _plc); } } /// /// 获取PLC /// /// /// public IPlc GetPlcByKey(string key) { return keyValuePairs[key]; } /// /// 获取所有PLC信息 /// /// public Dictionary GetAll() { return keyValuePairs; } /// /// 关闭所有连接 /// /// public bool disConnectAll() { bool result = false; try { foreach (var kv in keyValuePairs) { if (kv.Value != null) { kv.Value.DisConnect(); } } result = true; } catch (Exception ex) { logHelper.Error("关闭PLC连接异常", ex); } return result; } } }