Skip to content

Commit

Permalink
Merge pull request #397 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Oct 28, 2022
2 parents 77b9aea + 2d3df53 commit 7f86764
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Plugins.Modbus/Contracts/IModbusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public interface IModbusService
{
Task<object> ReadValue<T>(byte unitIdentifier, ushort startingAddress, ushort quantity,
string ipAddressString, int port, int connectDelay, int timeout, ModbusRegisterType modbusRegisterType) where T : struct;

Task<string> GetBinaryString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port,
int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType);
}
6 changes: 6 additions & 0 deletions Plugins.Modbus/Controllers/ModbusController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public ModbusController(IModbusService modbusService)
_modbusService = modbusService;
}

[HttpGet]
public Task<string> GetBinaryString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress,
int port, int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType)
=> _modbusService.GetBinaryString(unitIdentifier, startingAddress, quantity, ipAddress, port,
connectDelaySeconds, timeoutSeconds, modbusRegisterType);

[HttpGet]
public Task<object> GetTypedValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress,
int port, int connectDelaySeconds, int timeoutSeconds, ModbusValueType modbusValueType, ModbusRegisterType modbusRegisterType)
Expand Down
77 changes: 54 additions & 23 deletions Plugins.Modbus/Services/ModbusService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Plugins.Modbus.Contracts;
using System.Text;
using System.Threading;
using TeslaSolarCharger.Shared.Enums;

namespace Plugins.Modbus.Services;
Expand All @@ -18,30 +20,12 @@ public ModbusService(ILogger<ModbusService> logger, IServiceProvider serviceProv
public async Task<object> ReadValue<T>(byte unitIdentifier, ushort startingAddress, ushort quantity,
string ipAddressString, int port, int connectDelay, int timeout, ModbusRegisterType modbusRegisterType) where T : struct
{
_logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " +
"{connectDelay}, {timeout})",
nameof(ReadValue), unitIdentifier, startingAddress, quantity, ipAddressString, port,
connectDelay, timeout);
_logger.LogTrace("{method}<{type}>({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " +
"{connectDelay}, {timeout}, {modbusRegisterType})",
nameof(ReadValue), typeof(T), unitIdentifier, startingAddress, quantity, ipAddressString, port,
connectDelay, timeout, modbusRegisterType);

var modbusClient = GetModbusClient(ipAddressString, port);
byte[] byteArray;
if (timeout < 1)
{
_logger.LogDebug("Timeout is raised to minimum value of 1 second");
timeout = 1;
}
try
{
byteArray = await modbusClient.GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout, modbusRegisterType)
.ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not get byte array. Dispose modbus client");
modbusClient.Dispose();
_modbusClients.Remove(GetKeyString(ipAddressString, port));
throw;
}
var byteArray = await GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout, modbusRegisterType).ConfigureAwait(false);

if (typeof(T) == typeof(int))
{
Expand Down Expand Up @@ -77,6 +61,53 @@ public async Task<object> ReadValue<T>(byte unitIdentifier, ushort startingAddre

}

private async Task<byte[]> GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString,
int port, int connectDelay, int timeout, ModbusRegisterType modbusRegisterType)
{
_logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " +
"{connectDelay}, {timeout}, {modbusRegisterType})",
nameof(ReadValue), unitIdentifier, startingAddress, quantity, ipAddressString, port,
connectDelay, timeout, modbusRegisterType);
var modbusClient = GetModbusClient(ipAddressString, port);
byte[] byteArray;
if (timeout < 1)
{
_logger.LogDebug("Timeout is raised to minimum value of 1 second");
timeout = 1;
}

try
{
byteArray = await modbusClient.GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port,
connectDelay, timeout, modbusRegisterType)
.ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not get byte array. Dispose modbus client");
modbusClient.Dispose();
_modbusClients.Remove(GetKeyString(ipAddressString, port));
throw;
}

return byteArray;
}

public async Task<string> GetBinaryString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port,
int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType)
{
var byteArray = await GetByteArray(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, modbusRegisterType).ConfigureAwait(false);
byteArray = byteArray.Reverse().ToArray();
var stringbuilder = new StringBuilder();
foreach(var byteString in byteArray)
{
stringbuilder.Append(Convert.ToString(byteString, 2).PadLeft(8, '0'));
stringbuilder.Append(" ");
}

return stringbuilder.ToString();
}

private IModbusClient GetModbusClient(string ipAddressString, int port)
{
_logger.LogTrace("{method}({ipAddress}, {port})", nameof(GetModbusClient), ipAddressString, port);
Expand Down
6 changes: 3 additions & 3 deletions TeslaSolarCharger/Server/Services/ChargingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using TeslaSolarCharger.Server.Contracts;
using TeslaSolarCharger.Server.Resources;
using TeslaSolarCharger.Shared.Contracts;
Expand Down Expand Up @@ -428,8 +428,8 @@ private void UpdateEarliestTimesAfterSwitch(int carId)

var timespanUntilSwitchOff = _configurationWrapper.TimespanUntilSwitchOff();
var earliestSwitchOff = car.CarState.ShouldStopChargingSince + timespanUntilSwitchOff;
_logger.LogDebug("Should start charging since: {shoudStopChargingSince}", car.CarState.ShouldStopChargingSince);
_logger.LogDebug("Timespan until switch on: {timespanUntilSwitchOff}", timespanUntilSwitchOff);
_logger.LogDebug("Should stop charging since: {shoudStopChargingSince}", car.CarState.ShouldStopChargingSince);
_logger.LogDebug("Timespan until switch off: {timespanUntilSwitchOff}", timespanUntilSwitchOff);
_logger.LogDebug("Earliest switch off: {earliestSwitchOn}", earliestSwitchOff);
return earliestSwitchOff;
}
Expand Down

0 comments on commit 7f86764

Please sign in to comment.