Skip to content

Commit

Permalink
Fix issues when customer has data with null cost (#3)
Browse files Browse the repository at this point in the history
* Add dump of json

* Tweak filename format

* Update readme

* bump version

* Handle missing cost data and tidy up

* Update NuGet packages

* Flip switch
  • Loading branch information
MikeWilliams-UK authored Nov 14, 2024
1 parent 958fc74 commit e3edbdd
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 21 deletions.
3 changes: 2 additions & 1 deletion AppSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"AccountsUri": "https://smartpaymapi.ovoenergy.com/first-login/api/bootstrap/v2/",
"MonthlyUri": "https://smartpaymapi.ovoenergy.com/usage/api/monthly/{0}?date={1}",
"DailyUri": "https://smartpaymapi.ovoenergy.com/usage/api/daily/{0}?date={1}",
"HalfHourlyUri": "https://smartpaymapi.ovoenergy.com/usage/api/half-hourly/{0}?date={1}"
"HalfHourlyUri": "https://smartpaymapi.ovoenergy.com/usage/api/half-hourly/{0}?date={1}",
"DumpData": "false"
}
Binary file added Assets/FolderPermissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Configuration;
using System;

namespace OvoData.Helpers;

public static class ConfigHelper
{
public static bool GetBoolean(IConfigurationRoot config, string key, bool defaultValue)
{
var result = defaultValue;

if (config != null)
{
try
{
var value = config[key];
if (bool.TryParse(value, out result))
{
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

return result;
}
}
12 changes: 12 additions & 0 deletions Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public static MonthlyResponse GetMonthlyUsage(IConfigurationRoot config, string
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
if (ConfigHelper.GetBoolean(config, "DumpData", false))
{
Logger.DumpJson($"{nameof(GetMonthlyUsage)}-{year}", content);
}
result = JsonSerializer.Deserialize<MonthlyResponse>(content, _options);
}
}
Expand All @@ -109,6 +113,10 @@ public static DailyResponse GetDailyUsage(IConfigurationRoot config, string acco
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
if (ConfigHelper.GetBoolean(config, "DumpData", false))
{
Logger.DumpJson($"{nameof(GetDailyUsage)}-{year}-{month:D2}", content);
}
result = JsonSerializer.Deserialize<DailyResponse>(content, _options);
}
}
Expand All @@ -135,6 +143,10 @@ public static HalfHourlyResponse GetHalfHourlyUsage(IConfigurationRoot config, s
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
if (ConfigHelper.GetBoolean(config, "DumpData", false))
{
Logger.DumpJson($"{nameof(GetHalfHourlyUsage)}-{year}-{month:D2}-{day:D2}", content);
}
result = JsonSerializer.Deserialize<HalfHourlyResponse>(content, _options);
}
}
Expand Down
14 changes: 14 additions & 0 deletions Helpers/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ private static string GetFileName()

return fileName;
}

public static void DumpJson(string responseType, string json)
{
var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Constants.ApplicationName);

if (!Directory.Exists(Path.Combine(folder, "Dump")))
{
Directory.CreateDirectory(Path.Combine(folder, "Dump"));
}

var fileName = Path.Combine(folder, "Dump", $"{DateTime.Now:yyyy-MM-dd HH-mm-ss.fff} {responseType}.json");

File.WriteAllText(fileName, json);
}
}
8 changes: 6 additions & 2 deletions Helpers/SqliteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ public void UpsertMonthly(string fuelType, List<MonthlyDataItem> items)
{
var stringBuilder = new StringBuilder();

double.TryParse(item.Cost.Amount, out var safeCost);

stringBuilder.AppendLine($"INSERT INTO Monthly{fuelType}");
stringBuilder.AppendLine("VALUES");
stringBuilder.AppendLine($"('{item.Year}-{item.Month:D2}', '{item.Mpxn}', {item.Consumption}, {item.Cost.Amount})");
stringBuilder.AppendLine($"('{item.Year}-{item.Month:D2}', '{item.Mpxn}', {item.Consumption}, {safeCost})");
stringBuilder.AppendLine("ON CONFLICT (Month)");
stringBuilder.AppendLine("DO UPDATE SET Mxpn = excluded.Mxpn, Consumption = excluded.Consumption, Cost = excluded.Cost");

Expand Down Expand Up @@ -191,9 +193,11 @@ public void UpsertDaily(string fuelType, List<DailyDataItem> items)

var day = item.Interval.Start.ToString("yyyy-MM-dd");

double.TryParse(item.Cost.Amount, out var safeCost);

stringBuilder.AppendLine($"INSERT INTO Daily{fuelType}");
stringBuilder.AppendLine("VALUES");
stringBuilder.AppendLine($"('{day}', {item.Consumption}, {item.Cost.Amount}, {item.Rates.Standing}, {item.Rates.AnyTime}, {item.Rates.Peak}, {item.Rates.OffPeak}, {item.HasHhData})");
stringBuilder.AppendLine($"('{day}', {item.Consumption}, {safeCost}, {item.Rates.Standing}, {item.Rates.AnyTime}, {item.Rates.Peak}, {item.Rates.OffPeak}, {item.HasHhData})");
stringBuilder.AppendLine("ON CONFLICT (Day)");
stringBuilder.AppendLine("DO UPDATE SET Consumption = excluded.Consumption, Cost = excluded.Cost, Standing = excluded.Standing, AnyTime = excluded.AnyTime, Peak = excluded.Peak, OffPeak = excluded.OffPeak, HasHhData = excluded.HasHhData");

Expand Down
4 changes: 2 additions & 2 deletions Models/OvoApi/AccountsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace OvoData.Models.OvoApi;

public class AccountsResponse
{
public string CustomerId { get; set; }
public string CustomerId { get; set; } = string.Empty;
public bool IsFirstLogin { get; set; }
public List<string> AccountIds { get; set; }
public List<string> AccountIds { get; set; } = new();
}
4 changes: 2 additions & 2 deletions Models/OvoApi/Cost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class Cost
{
public string CurrencyUnit { get; set; }
public string Amount { get; set; }
public string CurrencyUnit { get; set; } = string.Empty;
public string Amount { get; set; } = string.Empty;
}
6 changes: 3 additions & 3 deletions Models/OvoApi/DailyDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class DailyDataItem
public bool HasHhData { get; set; }
public double Consumption { get; set; }

public Interval Interval { get; set; }
public Interval Interval { get; set; } = new();

public Cost Cost { get; set; }
public Rates Rates { get; set; }
public Cost Cost { get; set; } = new();
public Rates Rates { get; set; } = new();
}
4 changes: 2 additions & 2 deletions Models/OvoApi/HalfHourlyDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class HalfHourlyDataItem
{
public double Consumption { get; set; }
public Interval Interval { get; set; }
public string Unit { get; set; }
public Interval Interval { get; set; } = new();
public string Unit { get; set; } = string.Empty;
}
4 changes: 2 additions & 2 deletions Models/OvoApi/LoginRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class LoginRequest
{
public string Username { get; set; }
public string Password { get; set; }
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public bool RememberMe { get; set; }
}
4 changes: 2 additions & 2 deletions Models/OvoApi/MonthlyDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class MonthlyDataItem
{
public string Mpxn { get; set; }
public string Mpxn { get; set; } = string.Empty;
public int Month { get; set; }
public int Year { get; set; }
public double Consumption { get; set; }
public Cost Cost { get; set; }
public Cost Cost { get; set; } = new();
}
10 changes: 5 additions & 5 deletions OvoData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SatelliteResourceLanguages>en-US;en</SatelliteResourceLanguages>
<ApplicationIcon>Ovo.ico</ApplicationIcon>
<AssemblyVersion>1.0.2</AssemblyVersion>
<FileVersion>1.0.2.2</FileVersion>
<AssemblyVersion>1.0.3</AssemblyVersion>
<FileVersion>1.0.3.3</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -33,12 +33,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageReference Include="OpenSpreadsheet" Version="1.2.3" />
<PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.118" />
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Get your energy usage data from OVO via it's API.

There have been reports that some Windows 11 users require this to be run elevated (as admin).
This is probably due to permissions to the folder C:\ProgramData.

If this affects you please create the above folder and assign "Full control" to the Users Group

<img src="Assets/FolderPermissions.png">

If you like my work, please consider donating at https://www.buymeacoffee.com/mikewilliamsuk

<img src="Assets/BuyMeACoffee-QR.png" width=200 height=200>
Expand Down

0 comments on commit e3edbdd

Please sign in to comment.