/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { base.Dispose(disposing); if (!this.isDisposed) { if (disposing) { if (this.tcpClient != null) { this.tcpClient.Connected -= this.TcpClient_Connected; this.tcpClient.Disconnected -= this.TcpClient_Disconnected; this.tcpClient.DataAvailable -= this.TcpClient_DataAvailable; // Poll timer is killed by now if (this.tcpClient.IsConnected) { this.logger.Information(Invariant($"{Red}Disconnecting from PLC {this.Name}")); this.tcpClient.Disconnect(); } } this.tcpClient?.Dispose(); this.tcpClient = null; this.plcTxFifo = null; } this.isDisposed = true; } }
/// <summary> /// Initializes a new instance of the <see cref="Plc" /> class. /// </summary> /// <param name="name">The name.</param> /// <param name="deviceLogger">The device logger</param> /// <param name="configurationManager">The configuration manager.</param> public Plc(string name, IDeviceLogger deviceLogger, IConfigurationManager configurationManager) : base(deviceLogger) { this.Name = name; this.deviceLogger = deviceLogger ?? throw new ArgumentNullException("deviceLogger", "Device Logger cannot be null"); this.logger = deviceLogger.CreateComponentLogger(className); this.StateMessage = "PLC is not enabled"; this.State = GuiState.FAULT; bool success = IPAddress.TryParse("0.0.0.0", out this.ipAddress); this.isDisposed = false; this.port = 0; this.missedHeartbeats = 0; this.plcDataStream = string.Empty; this.plcDataStreamLock = new object(); this.regexHashEventSystem = new Regex("##(\\w*) (\\w*)(.*)"); this.regexfindParams = new Regex(" (([\\w]*)=((\"[a-zA-Z0-9._\\ ]*\")|(0x[0-9]*))|(\\w*))"); delay = 0; this.configurationManager = configurationManager ?? throw new ArgumentNullException("configurationManager", "ConfigurationManager cannot be null"); this.plcSettings = configurationManager.PLCSettings; configurationManager.PLCSettings.SettingsChanged += this.PLCSettings_SettingsChanged; success = IPAddress.TryParse("0.0.0.0", out this.defaultIPAddress); success = IPAddress.TryParse(this.plcSettings.IPAddress, out this.ipAddress); this.port = this.plcSettings.Port; this.plcTxFifo = new ConcurrentBlockingFifo <byte[]>(); this.plcInputEventData = new PlcInputEventData(); this.plcOutputEventData = new PlcOutputEventData(); this.plcSystemEventData = new PlcSystemEventData(); this.plcInputEventData.EventDataChanged += this.PlcInputEventData_EventDataChanged; this.plcOutputEventData.EventDataChanged += this.PlcOutputEventData_EventDataChanged; this.plcSystemEventData.EventDataChanged += this.PlcSystemEventData_EventDataChanged; this.plcSystemEventData.HeartbeatEvent += this.PlcSystemEventData_HeartbeatEvent; }