From 4a3438a2189c6504ab09891f9d3d6a93b316c162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 2 Oct 2022 18:37:27 +0200 Subject: [PATCH 1/3] feat(ModbusClient): dispose modbus client on error --- Plugins.Modbus/Contracts/IModbusClient.cs | 4 +- Plugins.Modbus/Services/ModbusClient.cs | 11 ++++- Plugins.Modbus/Services/ModbusService.cs | 58 ++++++++++++++++++----- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/Plugins.Modbus/Contracts/IModbusClient.cs b/Plugins.Modbus/Contracts/IModbusClient.cs index c55c5b482..f7f08100a 100644 --- a/Plugins.Modbus/Contracts/IModbusClient.cs +++ b/Plugins.Modbus/Contracts/IModbusClient.cs @@ -1,6 +1,6 @@ namespace Plugins.Modbus.Contracts; -public interface IModbusClient +public interface IModbusClient : IDisposable { Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, @@ -9,4 +9,4 @@ Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort qua bool DiconnectIfConnected(); Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult); Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult); -} \ No newline at end of file +} diff --git a/Plugins.Modbus/Services/ModbusClient.cs b/Plugins.Modbus/Services/ModbusClient.cs index 65d564cd8..e984d7135 100644 --- a/Plugins.Modbus/Services/ModbusClient.cs +++ b/Plugins.Modbus/Services/ModbusClient.cs @@ -111,11 +111,14 @@ private async Task GetRegisterValue(byte unitIdentifier, ushort starting var tmpArrayPowerComplete = ReadHoldingRegisters(unitIdentifier, startingAddress, quantity).ToArray(); return tmpArrayPowerComplete; } - catch (Exception) + catch (Exception ex) { + _logger.LogError(ex, "Could not get register value."); if (IsConnected) { + _logger.LogDebug("Disconnecting Modcus Client..."); Disconnect(); + _logger.LogDebug("Modbus Client disconnected."); } throw; @@ -141,4 +144,10 @@ public static byte[] StringToByteArray(string hex) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); } + + public void Dispose() + { + DiconnectIfConnected(); + _semaphoreSlim.Dispose(); + } } diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index dac715403..b9bb5fa55 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -24,9 +24,20 @@ public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddres var modbusClient = GetModbusClient(ipAddressString, port); - var value = await modbusClient.ReadInt32Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadInt32Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } + } public async Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, @@ -38,9 +49,19 @@ public async Task ReadInt16Value(byte unitIdentifier, ushort startingAddr connectDelay, timeout, minimumResult); var modbusClient = GetModbusClient(ipAddressString, port); - var value = await modbusClient.ReadInt16Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadInt16Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } } public async Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, @@ -51,10 +72,19 @@ public async Task ReadFloatValue(byte unitIdentifier, ushort startingAddr nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout, minimumResult); var modbusClient = GetModbusClient(ipAddressString, port); - - var value = await modbusClient.ReadFloatValue(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadFloatValue(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } } private IModbusClient GetModbusClient(string ipAddressString, int port) @@ -66,7 +96,7 @@ private IModbusClient GetModbusClient(string ipAddressString, int port) AppDomain.CurrentDomain.ProcessExit += OnProcessExit; } - var keyString = $"{ipAddressString}:{port}"; + var keyString = GetKeyString(ipAddressString, port); if (_modbusClients.Any(c => c.Key == keyString)) { _logger.LogDebug("Use exising modbusClient"); @@ -82,6 +112,12 @@ private IModbusClient GetModbusClient(string ipAddressString, int port) return modbusClient; } + private string GetKeyString(string ipAddressString, int port) + { + var keyString = $"{ipAddressString}:{port}"; + return keyString; + } + private void OnProcessExit(object? sender, EventArgs e) { _logger.LogTrace("Closing all open connections..."); From 299e9aea7da87f5dec94fc99fab7f4dfabd20640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 2 Oct 2022 18:39:10 +0200 Subject: [PATCH 2/3] feat(CICD): create specific tag --- .github/workflows/alphaRelease.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/alphaRelease.yml b/.github/workflows/alphaRelease.yml index 729dc8eae..9c5c4f273 100644 --- a/.github/workflows/alphaRelease.yml +++ b/.github/workflows/alphaRelease.yml @@ -145,4 +145,4 @@ jobs: file: ./Plugins.Modbus/Dockerfile platforms: linux/amd64,linux/arm64,linux/arm/v7 push: true - tags: pkuehnel/teslasolarchargermodbusplugin:alpha + tags: pkuehnel/teslasolarchargermodbusplugin:autoDispose From a895672b3ac57015938b5fdc394827c64a296a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 2 Oct 2022 18:39:10 +0200 Subject: [PATCH 3/3] Revert "feat(CICD): create specific tag" This reverts commit 299e9aea7da87f5dec94fc99fab7f4dfabd20640. --- .github/workflows/alphaRelease.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/alphaRelease.yml b/.github/workflows/alphaRelease.yml index 9c5c4f273..729dc8eae 100644 --- a/.github/workflows/alphaRelease.yml +++ b/.github/workflows/alphaRelease.yml @@ -145,4 +145,4 @@ jobs: file: ./Plugins.Modbus/Dockerfile platforms: linux/amd64,linux/arm64,linux/arm/v7 push: true - tags: pkuehnel/teslasolarchargermodbusplugin:autoDispose + tags: pkuehnel/teslasolarchargermodbusplugin:alpha