fix of backend to get 15-min data
This commit is contained in:
parent
807882b960
commit
b09cf00d78
|
|
@ -214,23 +214,33 @@ 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)
|
||||
private static Double? TryReadLinePrice(String line)
|
||||
{
|
||||
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))
|
||||
return null;
|
||||
|
||||
|
|
@ -242,4 +252,9 @@ public static class CurrentPriceHistoryService
|
|||
_ => null
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null; // skip a malformed record line
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue