added file loader
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using BatteryCostAnalysis.Models;
|
||||
using BattSim.Models;
|
||||
|
||||
namespace BatteryCostAnalysis.Services
|
||||
namespace BattSim.Services
|
||||
{
|
||||
public class BatterySimulator
|
||||
{
|
||||
|
||||
@@ -3,52 +3,71 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using BatteryCostAnalysis.Models;
|
||||
using System.Threading.Tasks;
|
||||
using BattSim.Models;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
|
||||
namespace BatteryCostAnalysis.Services
|
||||
namespace BattSim.Services
|
||||
{
|
||||
public class DataLoader
|
||||
public static class DataLoader
|
||||
{
|
||||
public static List<EnergyData> LoadAndProcessData(string filePath)
|
||||
public static async Task<List<EnergyData>> LoadAndProcessData(IBrowserFile file)
|
||||
{
|
||||
var energyData = new List<EnergyData>();
|
||||
var lines = File.ReadAllLines(filePath).Skip(1); // Skip header
|
||||
|
||||
foreach (var line in lines)
|
||||
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) continue;
|
||||
|
||||
var date = DateTime.Parse(parts[0], new CultureInfo("nl-BE"));
|
||||
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)
|
||||
if (parts.Length < 9)
|
||||
{
|
||||
existing = new EnergyData { Date = date };
|
||||
energyData.Add(existing);
|
||||
Console.WriteLine($"Skipping malformed line: {line}");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (register)
|
||||
|
||||
try
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using BatteryCostAnalysis.Models;
|
||||
using BattSim.Models;
|
||||
|
||||
namespace BatteryCostAnalysis.Services
|
||||
namespace BattSim.Services
|
||||
{
|
||||
public class ReportGenerator
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user