fixed chart names and added extra battery properties.
This commit is contained in:
@@ -4,44 +4,31 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Threading.Tasks;
|
||||
using BattSim.Models;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
|
||||
namespace BattSim.Services
|
||||
{
|
||||
public static class DataLoader
|
||||
public static class FluviusDataHandler
|
||||
{
|
||||
public static async Task<List<EnergyData>> LoadAndProcessData(IBrowserFile file)
|
||||
public static async Task<Dictionary<DateTime, EnergyData>> LoadAndProcessFile(IBrowserFile file)
|
||||
{
|
||||
var energyData = new ConcurrentBag<EnergyData>(); // Thread-safe collection
|
||||
await using var stream = file.OpenReadStream(maxAllowedSize: (int)1.0e9);
|
||||
using var reader = new StreamReader(stream, bufferSize: (int)1.0e6);
|
||||
|
||||
// Skip header
|
||||
await reader.ReadLineAsync();
|
||||
|
||||
// Read all lines into memory
|
||||
var lines = new List<string>();
|
||||
string line;
|
||||
while ((line = await reader.ReadLineAsync()) is not null)
|
||||
{
|
||||
lines.Add(line);
|
||||
}
|
||||
|
||||
var lines = await ReadCsvFile(file);
|
||||
var energyData = new ConcurrentDictionary<DateTime,EnergyData>(); // Thread-safe collection
|
||||
// Process lines in parallel
|
||||
Parallel.ForEach(lines, line =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var parts = SplitLine(line);
|
||||
DateTime time = ParseDateTime(parts[0..2]);
|
||||
DateTime time = ParseDateTime(parts[..2]);
|
||||
double volume = ParseVolume(parts[8]);
|
||||
string register = parts[7].Trim();
|
||||
bool dayTarif = register.Contains("Dag");
|
||||
|
||||
// Use ConcurrentBag for thread-safe additions
|
||||
var entry = new EnergyData { Time = time, DayTarif = dayTarif };
|
||||
var entry = new EnergyData { Time = time, DayTariff = dayTarif };
|
||||
if (register.Contains("Afname"))
|
||||
entry.Consumption = volume;
|
||||
else if (register.Contains("Injectie"))
|
||||
@@ -49,28 +36,60 @@ namespace BattSim.Services
|
||||
else
|
||||
throw new Exception("Unknown volume register");
|
||||
|
||||
energyData.Add(entry);
|
||||
energyData.AddOrUpdate(entry.Time, entry, (_, existing) =>
|
||||
{
|
||||
existing.Consumption += entry.Consumption;
|
||||
existing.Production += entry.Production;
|
||||
return existing;
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error parsing line: {line}. Skipping to next line. Exception: {e}");
|
||||
}
|
||||
});
|
||||
return energyData.ToDictionary();
|
||||
}
|
||||
|
||||
// Group by Time and merge entries
|
||||
var groupedData = energyData
|
||||
.GroupBy(e => e.Time)
|
||||
.Select(g =>
|
||||
{
|
||||
var first = g.First();
|
||||
first.Consumption = g.Sum(e => e.Consumption);
|
||||
first.Production = g.Sum(e => e.Production);
|
||||
return first;
|
||||
})
|
||||
.OrderBy(e => e.Time)
|
||||
.ToList();
|
||||
public static Dictionary<DateOnly, EnergyData> GenerateDailyData(Dictionary<DateTime, EnergyData> energyData)
|
||||
{
|
||||
var dailyEnergyData = new ConcurrentDictionary<DateOnly, EnergyData>();
|
||||
Parallel.ForEach(energyData, entry =>
|
||||
{
|
||||
var date = DateOnly.FromDateTime(entry.Key);
|
||||
var energy = entry.Value;
|
||||
|
||||
return groupedData;
|
||||
// Use AddOrUpdate to avoid double lookup
|
||||
dailyEnergyData.AddOrUpdate(
|
||||
date,
|
||||
energy, // If key doesn't exist, add this value
|
||||
(_, existing) =>
|
||||
{
|
||||
// If key exists, aggregate the values
|
||||
existing.Consumption += energy.Consumption;
|
||||
existing.Production += energy.Production;
|
||||
return existing;
|
||||
}
|
||||
);
|
||||
});
|
||||
return dailyEnergyData.ToDictionary();
|
||||
}
|
||||
|
||||
private static async Task<List<string>> ReadCsvFile(IBrowserFile file)
|
||||
{
|
||||
await using var stream = file.OpenReadStream(maxAllowedSize: (int)1.0e9);
|
||||
using var reader = new StreamReader(stream, bufferSize: (int)1.0e6);
|
||||
|
||||
// Skip header
|
||||
await reader.ReadLineAsync();
|
||||
// Read all lines into memory
|
||||
var lines = new List<string>();
|
||||
string line;
|
||||
while ((line = await reader.ReadLineAsync()) is not null)
|
||||
{
|
||||
lines.Add(line);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static string[] SplitLine(string line)
|
||||
Reference in New Issue
Block a user