Skip to content

Commit

Permalink
Merge pull request #400 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Nov 3, 2022
2 parents 7f86764 + 76d64e0 commit 357da21
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Plugins.Modbus/Contracts/IModbusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ Task<object> ReadValue<T>(byte unitIdentifier, ushort startingAddress, ushort qu

Task<string> GetBinaryString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port,
int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType);

Task<string> GetBinarySubString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType, int startIndex, int length);
}
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> GetBinarySubString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress,
int port, int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType, int startIndex, int length)
=> _modbusService.GetBinarySubString(unitIdentifier, startingAddress, quantity, ipAddress, port,
connectDelaySeconds, timeoutSeconds, modbusRegisterType, startIndex, length);

[HttpGet]
public Task<string> GetBinaryString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress,
int port, int connectDelaySeconds, int timeoutSeconds, ModbusRegisterType modbusRegisterType)
Expand Down
13 changes: 12 additions & 1 deletion Plugins.Modbus/Services/ModbusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ModbusService : IModbusService
private readonly IServiceProvider _serviceProvider;
private readonly Dictionary<string, IModbusClient> _modbusClients = new();

private readonly string _byteDelimiter = " ";

public ModbusService(ILogger<ModbusService> logger, IServiceProvider serviceProvider)
{
_logger = logger;
Expand Down Expand Up @@ -102,12 +104,21 @@ public async Task<string> GetBinaryString(byte unitIdentifier, ushort startingAd
foreach(var byteString in byteArray)
{
stringbuilder.Append(Convert.ToString(byteString, 2).PadLeft(8, '0'));
stringbuilder.Append(" ");
stringbuilder.Append(_byteDelimiter);
}

return stringbuilder.ToString();
}

public async Task<string> GetBinarySubString(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds,
int timeoutSeconds, ModbusRegisterType modbusRegisterType, int startIndex, int length)
{
var binaryString = await GetBinaryString(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds,
modbusRegisterType).ConfigureAwait(false);
binaryString = binaryString.Replace(_byteDelimiter, string.Empty);
return binaryString.Substring(startIndex, length);
}

private IModbusClient GetModbusClient(string ipAddressString, int port)
{
_logger.LogTrace("{method}({ipAddress}, {port})", nameof(GetModbusClient), ipAddressString, port);
Expand Down
4 changes: 0 additions & 4 deletions TeslaSolarCharger.Tests/TeslaSolarCharger.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
</p>

@code {

#region HelperProperties
private string? ModbusUnitIdentifier
{
get => _modbusUnitIdentifier;
Expand Down Expand Up @@ -143,6 +145,8 @@
}
}

#endregion

private string? _modbusUnitIdentifier;
private string? _startingAddress;
private string? _quantity;
Expand Down
8 changes: 8 additions & 0 deletions TeslaSolarCharger/Client/Pages/BaseConfiguration.razor
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ else
<label for="homeBatteryPowerXmlAttributeValueName">Home Battery Power XML Attribute Header Value</label>
<InputText id="homeBatteryPowerXmlAttributeValueName" @bind-Value="_dtoBaseConfiguration.HomeBatteryPowerXmlAttributeValueName" placeholder="AttributeValueName" class="form-control" />
</div>
<div class="form-group">
<label for="homeBatteryPowerInversionUrl">HomeBatteryPowerInversion Url</label>
<InputText id="homeBatteryPowerInversionUrl" @bind-Value="_dtoBaseConfiguration.HomeBatteryPowerInversionUrl" class="form-control" />
<div>
<small id="homeBatteryPowerInversionUrlHelp" class="form-text text-muted">Use this if you have to dynamically invert the home battery power. Note: Only 0 and 1 are allowed as response.</small>
</div>
</div>

<hr />
<div class="form-group">
<label for="currentInverterPowerUrl">Inverter Power Url</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public PossibleIssues(IssueKeys issueKeys)
{
issueKeys.InverterPowerNotAvailable, CreateIssue("Inverter power is not available",
IssueType.Warning,
"Does your inverter currently produce energy? Some inverters do not return any value if solar power is not available and you can ignore this issue.",
"Are all settings related to inverter power (url, extraction patterns, headers,...) correct?",
"Are there any firewall related issues preventing reading the inverter power value?"
)
Expand Down
15 changes: 15 additions & 0 deletions TeslaSolarCharger/Server/Services/PvValueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public async Task UpdatePvValues()
var homeBatteryPowerXmlPattern = _configurationWrapper.HomeBatteryPowerXmlPattern();
var homeBatteryPowerCorrectionFactor = (double)_configurationWrapper.HomeBatteryPowerCorrectionFactor();
var homeBatteryPower = await GetValueByHttpResponse(homeBatteryPowerHttpResponse, homeBatteryPowerJsonPattern, homeBatteryPowerXmlPattern, homeBatteryPowerCorrectionFactor).ConfigureAwait(false);
var homeBatteryPowerInversionRequestUrl = _configurationWrapper.HomeBatteryPowerInversionUrl();
if (!string.IsNullOrEmpty(homeBatteryPowerInversionRequestUrl))
{
var homeBatteryPowerInversionHeaders = _configurationWrapper.HomeBatteryPowerInversionHeaders();
//ToDo: implement setting Headers in frontend
var homeBatteryPowerInversionRequest = GenerateHttpRequestMessage(homeBatteryPowerInversionRequestUrl, homeBatteryPowerInversionHeaders);
var homeBatteryPowerInversionHttpResponse = await GetHttpResponse(homeBatteryPowerInversionRequest).ConfigureAwait(false);
var shouldInvertHomeBatteryPowerInt = await GetValueByHttpResponse(homeBatteryPowerInversionHttpResponse, null, null, 1).ConfigureAwait(false);
var shouldInvertHomeBatteryPower = Convert.ToBoolean(shouldInvertHomeBatteryPowerInt);
if (shouldInvertHomeBatteryPower)
{
homeBatteryPower = -homeBatteryPower;
}
}

_settings.HomeBatteryPower = homeBatteryPower;
}
}
Expand Down
2 changes: 2 additions & 0 deletions TeslaSolarCharger/Shared/Contracts/IConfigurationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ public interface IConfigurationWrapper
Task TryAutoFillUrls();
string? SolarMqttUsername();
string? SolarMqttPassword();
string? HomeBatteryPowerInversionUrl();
Dictionary<string, string> HomeBatteryPowerInversionHeaders();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class BaseConfigurationBase
public Dictionary<string, string> HomeBatterySocHeaders { get; set; } = new();
public string? HomeBatteryPowerMqttTopic { get; set; }
public string? HomeBatteryPowerUrl { get; set; }
public string? HomeBatteryPowerInversionUrl { get; set; }
public Dictionary<string, string> HomeBatteryPowerHeaders { get; set; } = new();
public Dictionary<string, string> HomeBatteryPowerInversionHeaders { get; set; } = new();
public bool IsModbusGridUrl { get; set; }
public bool IsModbusHomeBatterySocUrl { get; set; }
public bool IsModbusHomeBatteryPowerUrl { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,21 @@ public Dictionary<string, string> HomeBatterySocHeaders()
return GetBaseConfiguration().HomeBatteryPowerUrl;
}

public string? HomeBatteryPowerInversionUrl()
{
return GetBaseConfiguration().HomeBatteryPowerInversionUrl;
}

public Dictionary<string, string> HomeBatteryPowerHeaders()
{
return GetBaseConfiguration().HomeBatteryPowerHeaders;
}

public Dictionary<string, string> HomeBatteryPowerInversionHeaders()
{
return GetBaseConfiguration().HomeBatteryPowerInversionHeaders;
}

public string? CurrentPowerToGridJsonPattern()
{
return GetBaseConfiguration().CurrentPowerToGridJsonPattern;
Expand Down

0 comments on commit 357da21

Please sign in to comment.