73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BattSim.Models;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
|
|
namespace BattSim.Services
|
|
{
|
|
public static class DataLoader
|
|
{
|
|
public static async Task<List<EnergyData>> LoadAndProcessData(IBrowserFile file)
|
|
{
|
|
var energyData = new List<EnergyData>();
|
|
await using var stream = file.OpenReadStream();
|
|
using var reader = new StreamReader(stream);
|
|
|
|
// Skip header
|
|
await reader.ReadLineAsync();
|
|
|
|
string line;
|
|
while ((line = await reader.ReadLineAsync()) != null)
|
|
{
|
|
var parts = line.Split(';');
|
|
if (parts.Length < 9)
|
|
{
|
|
Console.WriteLine($"Skipping malformed line: {line}");
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
var date = DateOnly.ParseExact(parts[0].Length == 10 ? parts[0] : "0"+parts[0], "dd/MM/yyyy", CultureInfo.InvariantCulture);
|
|
var register = parts[7].Trim();
|
|
var volumeStr = parts[8].Trim();
|
|
var volume = string.IsNullOrEmpty(volumeStr)
|
|
? 0
|
|
: double.Parse(volumeStr.Replace(",", "."), CultureInfo.InvariantCulture);
|
|
|
|
var existing = energyData.FirstOrDefault(e => e.Date == date);
|
|
if (existing == null)
|
|
{
|
|
existing = new EnergyData { Date = date };
|
|
energyData.Add(existing);
|
|
}
|
|
|
|
switch (register)
|
|
{
|
|
case "Afname Dag":
|
|
existing.DayConsumption = volume;
|
|
break;
|
|
case "Afname Nacht":
|
|
existing.NightConsumption = volume;
|
|
break;
|
|
case "Injectie Dag":
|
|
existing.DayProduction = volume;
|
|
break;
|
|
case "Injectie Nacht":
|
|
existing.NightProduction = volume;
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error parsing line: {line}. Exception: {ex.Message}");
|
|
}
|
|
}
|
|
return energyData;
|
|
}
|
|
}
|
|
} |