Skip to content

Commit

Permalink
Merge pull request #94 from pkuehnel/fix/gridpowerFloat
Browse files Browse the repository at this point in the history
Fix/gridpower float
  • Loading branch information
pkuehnel authored Apr 26, 2022
2 parents 019ff6d + 2486893 commit 076f707
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
26 changes: 26 additions & 0 deletions SmartTeslaAmpSetter.Tests/Services/GridService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Xunit;
using Xunit.Abstractions;

namespace SmartTeslaAmpSetter.Tests.Services;

public class GridService : TestBase
{
public GridService(ITestOutputHelper outputHelper)
: base(outputHelper)
{
}

[Theory]
[InlineData("384.8746")]
[InlineData("384")]
[InlineData("384.0")]
[InlineData("384.147")]
public void Can_extract_Integers_From_String(string value)
{
var gridService = Mock.Create<Server.Services.GridService>();
var intValue = gridService.GetIntegerFromString(value);

Assert.Equal(384, intValue);
}

}
29 changes: 21 additions & 8 deletions SmartTeslaAmpSetter/Server/Services/GridService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using System.Globalization;
using Newtonsoft.Json.Linq;
using SmartTeslaAmpSetter.Server.Contracts;

namespace SmartTeslaAmpSetter.Server.Services;
Expand Down Expand Up @@ -43,16 +44,26 @@ public async Task<int> GetCurrentOverage()
throw new InvalidOperationException("Extracted Json Value is null")).Value<string>();
}

if (int.TryParse(result, out var overage))
try
{
var overage = GetIntegerFromString(result);
if (_configuration.GetValue<bool>("CurrentPowerToGridInvertValue"))
{
overage = -overage;
}
return overage;
return overage ;
}
catch (Exception)
{
throw new InvalidCastException($"Could not parse result {result} from uri {requestUri} to integer");
}

}

throw new InvalidCastException($"Could not parse result {result} from uri {requestUri} to integer");
internal int GetIntegerFromString(string? inputString)
{
_logger.LogTrace("{method}({param})", nameof(GetIntegerFromString), inputString);
return (int) double.Parse(inputString ?? throw new ArgumentNullException(nameof(inputString)), CultureInfo.InvariantCulture);
}

public async Task<int?> GetCurrentInverterPower()
Expand All @@ -77,11 +88,13 @@ await _telegramService.SendMessage(
}
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

if (int.TryParse(result, out var overage))
try
{
return overage;
return GetIntegerFromString(result);
}
catch (Exception)
{
throw new InvalidCastException($"Could not parse result {result} from uri {requestUri} to integer");
}

throw new InvalidCastException($"Could not parse result {result} from uri {requestUri} to integer");
}
}

0 comments on commit 076f707

Please sign in to comment.