fix of backend to get 15-min data

This commit is contained in:
Yinyin Liu 2026-06-12 13:28:50 +02:00
parent 807882b960
commit b09cf00d78
1 changed files with 36 additions and 21 deletions

View File

@ -214,23 +214,33 @@ public static class CurrentPriceHistoryService
} }
} }
// data.json is an object keyed by timestamp(s); read Config.CurrentPrice of the last record. // data.json is a line-based dump (same format the frontend parseChunkJson consumes):
private static Double? ExtractCurrentPrice(String json) // Timestamp;<unix>;
// {"Config":{"CurrentPrice":0.19,...},"InverterRecord":{...}}
// i.e. a header line then a JSON-record line, possibly repeated. It is NOT a single
// JSON object, so parse line by line and return CurrentPrice from the last record.
private static Double? ExtractCurrentPrice(String dataJson)
{ {
using var doc = JsonDocument.Parse(json); Double? last = null;
if (doc.RootElement.ValueKind != JsonValueKind.Object) foreach (var line in dataJson.Split('\n'))
return null;
var found = false;
var record = default(JsonElement);
foreach (var prop in doc.RootElement.EnumerateObject())
{ {
record = prop.Value; var price = TryReadLinePrice(line.Trim());
found = true; if (price.HasValue)
last = price;
}
return last;
} }
if (!found private static Double? TryReadLinePrice(String line)
|| !record.TryGetProperty("Config", out var config) {
if (line.Length == 0 || line[0] != '{')
return null; // skip "Timestamp;...;" headers and blank lines
try
{
using var doc = JsonDocument.Parse(line);
if (doc.RootElement.ValueKind != JsonValueKind.Object
|| !doc.RootElement.TryGetProperty("Config", out var config)
|| !config.TryGetProperty("CurrentPrice", out var currentPrice)) || !config.TryGetProperty("CurrentPrice", out var currentPrice))
return null; return null;
@ -242,4 +252,9 @@ public static class CurrentPriceHistoryService
_ => null _ => null
}; };
} }
catch
{
return null; // skip a malformed record line
}
}
} }