Skip to content

Commit

Permalink
fix: infinitly inscrease total
Browse files Browse the repository at this point in the history
  • Loading branch information
Dim145 committed May 29, 2024
1 parent 22e8dbc commit 48bcfb2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
22 changes: 17 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@
if (File.Exists(fileName))
{
Console.WriteLine("Read total consumption from file...");

var filedata = await File.ReadAllTextAsync(fileName);
var filedataSplitted = filedata.Split('\n');

totalConsumption = float.Parse(await File.ReadAllTextAsync(fileName), CultureInfo.InvariantCulture);
totalConsumption = float.Parse(filedataSplitted[0], CultureInfo.InvariantCulture);

var lastData = waterData.GetConso().ToList().MaxBy(e => e.Key);

totalConsumption += waterData.GetConso().ToList().MaxBy(e => e.Key).Value;
var fileDataDate = DateTime.Parse(filedataSplitted[1], CultureInfo.InvariantCulture);

await File.WriteAllTextAsync(fileName, totalConsumption.ToString(CultureInfo.InvariantCulture));
if(fileDataDate < lastData.Key)
{
totalConsumption += lastData.Value;

await File.WriteAllTextAsync(fileName, $"{totalConsumption.ToString(CultureInfo.InvariantCulture)}\n{lastData.Key.ToString(CultureInfo.InvariantCulture)}");

await mqttClient.PublishTotalConsuption(numContrat, totalConsumption);
await mqttClient.PublishTotalConsuption(numContrat, totalConsumption);
}
}
else
{
Expand Down Expand Up @@ -116,8 +126,10 @@

if (!Directory.Exists(Path.GetDirectoryName(fileName)))
Directory.CreateDirectory(Path.GetDirectoryName(fileName)!);

var lastDataDate = waterData.GetConso().ToList().MaxBy(e => e.Key).Key;

await File.WriteAllTextAsync(fileName, totalConsumption.ToString(CultureInfo.InvariantCulture));
await File.WriteAllTextAsync(fileName, $"{totalConsumption.ToString(CultureInfo.InvariantCulture)}\n{lastDataDate.ToString(CultureInfo.InvariantCulture)}");
}

Console.WriteLine($"Total consumption: {totalConsumption} m3");
Expand Down
2 changes: 1 addition & 1 deletion addon/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ CMD ["/entrypoint.sh"]
############

LABEL \
io.hass.version="0.2.0" \
io.hass.version="0.2.1" \
io.hass.type="addon" \
io.hass.arch="amd64|arm64"
2 changes: 1 addition & 1 deletion addon/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "Hariane 2 MQTT"
description: "Extracts Hariane water data into MQTT a.o."
version: 0.2.0
version: 0.2.1
slug: "hariane_2_mqtt"
url: https://github.com/Dim145/Hariane2Mqtt
init: true
Expand Down

0 comments on commit 48bcfb2

Please sign in to comment.