diff --git a/csharp/App/Backend/Services/CurrentPriceHistoryService.cs b/csharp/App/Backend/Services/CurrentPriceHistoryService.cs index d95f2de26..af3d45c2b 100644 --- a/csharp/App/Backend/Services/CurrentPriceHistoryService.cs +++ b/csharp/App/Backend/Services/CurrentPriceHistoryService.cs @@ -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;; + // {"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 + } } }