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,32 +214,47 @@ 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) {
|| !config.TryGetProperty("CurrentPrice", out var currentPrice)) if (line.Length == 0 || line[0] != '{')
return null; return null; // skip "Timestamp;...;" headers and blank lines
return currentPrice.ValueKind switch try
{ {
JsonValueKind.Number => currentPrice.GetDouble(), using var doc = JsonDocument.Parse(line);
JsonValueKind.String when Double.TryParse(currentPrice.GetString(), if (doc.RootElement.ValueKind != JsonValueKind.Object
NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed) => parsed, || !doc.RootElement.TryGetProperty("Config", out var config)
_ => null || !config.TryGetProperty("CurrentPrice", out var currentPrice))
}; return null;
return currentPrice.ValueKind switch
{
JsonValueKind.Number => currentPrice.GetDouble(),
JsonValueKind.String when Double.TryParse(currentPrice.GetString(),
NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed) => parsed,
_ => null
};
}
catch
{
return null; // skip a malformed record line
}
} }
} }