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.
private static Double? ExtractCurrentPrice(String json)
// data.json is a line-based dump (same format the frontend parseChunkJson consumes):
// 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);
if (doc.RootElement.ValueKind != JsonValueKind.Object)
return null;
var found = false;
var record = default(JsonElement);
foreach (var prop in doc.RootElement.EnumerateObject())
Double? last = null;
foreach (var line in dataJson.Split('\n'))
{
record = prop.Value;
found = true;
var price = TryReadLinePrice(line.Trim());
if (price.HasValue)
last = price;
}
return last;
}
if (!found
|| !record.TryGetProperty("Config", out var config)
|| !config.TryGetProperty("CurrentPrice", out var currentPrice))
return null;
private static Double? TryReadLinePrice(String line)
{
if (line.Length == 0 || line[0] != '{')
return null; // skip "Timestamp;...;" headers and blank lines
return currentPrice.ValueKind switch
try
{
JsonValueKind.Number => currentPrice.GetDouble(),
JsonValueKind.String when Double.TryParse(currentPrice.GetString(),
NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed) => parsed,
_ => null
};
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))
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
}
}
}