using OxyPlot; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TeamAAS_VP.Core.Robots; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; namespace TeamAAS_VP.Core.PLCs { public class PlcService { private readonly Dictionary _plcCollection; public PlcService() { _plcCollection = new Dictionary(); } public bool CreatePlc(Guid id, PlcInfo plcInfo) { IPlc plc; if (plcInfo.CommunicationType == Enums.CommunicationType.OPC_UA_Client) { plc = new OPCuaClientPLC(plcInfo); } else if (plcInfo.CommunicationType == Enums.CommunicationType.Mitsubishi_MC) { plc = new MitsubishiMcPLC(plcInfo); } else { return false; } if (_plcCollection.ContainsKey(id)) { var currenplc = _plcCollection[id]; currenplc.Dispose(); } _plcCollection[id] = plc; return true; } public Task CreateRobotAsync(Guid id, PlcInfo plcInfo) { IPlc plc; if (plcInfo.CommunicationType == Enums.CommunicationType.OPC_UA_Client) { plc = new OPCuaClientPLC(plcInfo); } else if (plcInfo.CommunicationType == Enums.CommunicationType.Mitsubishi_MC) { plc = new MitsubishiMcPLC(plcInfo); } else { return Task.FromResult(false); } if (_plcCollection.ContainsKey(id)) { var currenplc = _plcCollection[id]; currenplc.Dispose(); } _plcCollection[id] = plc; return Task.FromResult(true); } public IPlc GetPlc(Guid id) { if (!_plcCollection.ContainsKey(id)) { return null; } return _plcCollection[id]; } public Task GetPlcAsync(Guid id) { if (!_plcCollection.ContainsKey(id)) { return null; } return Task.FromResult(_plcCollection[id]); } public void UnRegisterPlc(Guid id) { if (_plcCollection.ContainsKey(id)) { var plc = _plcCollection[id]; plc.Dispose(); } } public Task UnRegisterPlcAsync(Guid id) { if (_plcCollection.ContainsKey(id)) { var plc = _plcCollection[id]; plc.Dispose(); } return Task.CompletedTask; } } }