From 113b7c32340cf7818986e0a77986f88dd7f818d4 Mon Sep 17 00:00:00 2001 From: douwe Date: Sat, 28 Mar 2026 02:01:00 +0100 Subject: [PATCH] added file loader --- Models/BatteryDayResult.cs | 4 +- Models/EnergyData.cs | 4 +- Pages/Home.razor | 126 +++++++++++------------------------ Pages/Test.razor | 96 ++++++++++++++++++++++++++ Program.cs | 4 ++ Services/BatterySimulator.cs | 4 +- Services/DataLoader.cs | 87 ++++++++++++++---------- Services/ReportGenerator.cs | 4 +- 8 files changed, 201 insertions(+), 128 deletions(-) create mode 100644 Pages/Test.razor diff --git a/Models/BatteryDayResult.cs b/Models/BatteryDayResult.cs index e6f91d8..d2c033f 100644 --- a/Models/BatteryDayResult.cs +++ b/Models/BatteryDayResult.cs @@ -1,10 +1,10 @@ using System; -namespace BatteryCostAnalysis.Models +namespace BattSim.Models { public class BatteryDayResult { - public DateTime Date { get; set; } + public DateOnly Date { get; set; } public double ChargedEnergy { get; set; } public double UsedEnergy { get; set; } public double RemainingEnergy { get; set; } diff --git a/Models/EnergyData.cs b/Models/EnergyData.cs index e609c27..c93ac7f 100644 --- a/Models/EnergyData.cs +++ b/Models/EnergyData.cs @@ -1,10 +1,10 @@ using System; -namespace BatteryCostAnalysis.Models +namespace BattSim.Models { public class EnergyData { - public DateTime Date { get; set; } + public DateOnly Date { get; set; } public double DayConsumption { get; set; } public double NightConsumption { get; set; } public double DayProduction { get; set; } diff --git a/Pages/Home.razor b/Pages/Home.razor index 2b05933..00302e4 100644 --- a/Pages/Home.razor +++ b/Pages/Home.razor @@ -1,96 +1,50 @@ @page "/" -@using Radzen -@using Radzen.Blazor -@using System.Globalization +@using BattSim.Models +@using BattSim.Services +BattSim -Home - -

Hello, world!

- -Welcome to your new app. - -

A Radzen chart:

- - - - - - - - - - - - - - +

BattSim

+

Upload your fluvius daily csv file here.

+ +@if (_isLoading){

Loading...

} @code { - bool showDataLabels = false; + EnergyData[] EnergyData = []; + (int, BatteryDayResult[])[] SimulationData = []; - void OnSeriesClick(SeriesClickEventArgs args) + bool _isLoading = false; + + private async Task LoadCsvFile(InputFileChangeEventArgs e) { - + _isLoading = true; + StateHasChanged(); + + var file = e.File; + if (file.ContentType != "text/csv") + { + Console.WriteLine("Only CSV files are allowed!"); + return; + } + + try + { + var loadingTask = DataLoader.LoadAndProcessData(file); + var energyData = await loadingTask; + + EnergyData = energyData.ToArray(); + _isLoading = false; + StateHasChanged(); + + foreach (var data in EnergyData) + { + Console.WriteLine(data.Date.ToString()); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error loading file: {ex.Message}"); + } } - - class DataItem - { - public string Quarter { get; set; } - public double Revenue { get; set; } - } - - string FormatAsUSD(object value) - { - return ((double)value).ToString("C0", CultureInfo.CreateSpecificCulture("en-US")); - } - - DataItem[] revenue2023 = new DataItem[] - { - new DataItem - { - Quarter = "Q1", - Revenue = 234000 - }, - new DataItem - { - Quarter = "Q2", - Revenue = 284000 - }, - new DataItem - { - Quarter = "Q3", - Revenue = 274000 - }, - new DataItem - { - Quarter = "Q4", - Revenue = 294000 - }, - }; - - DataItem[] revenue2024 = new DataItem[] { - new DataItem - { - Quarter = "Q1", - Revenue = 254000 - }, - new DataItem - { - Quarter = "Q2", - Revenue = 324000 - }, - new DataItem - { - Quarter = "Q3", - Revenue = 354000 - }, - new DataItem - { - Quarter = "Q4", - Revenue = 394000 - }, - - }; } \ No newline at end of file diff --git a/Pages/Test.razor b/Pages/Test.razor new file mode 100644 index 0000000..b0eca29 --- /dev/null +++ b/Pages/Test.razor @@ -0,0 +1,96 @@ +@page "/Test" +@using Radzen +@using Radzen.Blazor +@using System.Globalization + + +Home + +

Hello, world!

+ +Welcome to your new app. + +

A Radzen chart:

+ + + + + + + + + + + + + + + + +@code { + bool showDataLabels = false; + + void OnSeriesClick(SeriesClickEventArgs args) + { + + } + + class DataItem + { + public string Quarter { get; set; } + public double Revenue { get; set; } + } + + string FormatAsUSD(object value) + { + return ((double)value).ToString("C0", CultureInfo.CreateSpecificCulture("en-US")); + } + + DataItem[] revenue2023 = new DataItem[] + { + new DataItem + { + Quarter = "Q1", + Revenue = 234000 + }, + new DataItem + { + Quarter = "Q2", + Revenue = 284000 + }, + new DataItem + { + Quarter = "Q3", + Revenue = 274000 + }, + new DataItem + { + Quarter = "Q4", + Revenue = 294000 + }, + }; + + DataItem[] revenue2024 = new DataItem[] { + new DataItem + { + Quarter = "Q1", + Revenue = 254000 + }, + new DataItem + { + Quarter = "Q2", + Revenue = 324000 + }, + new DataItem + { + Quarter = "Q3", + Revenue = 354000 + }, + new DataItem + { + Quarter = "Q4", + Revenue = 394000 + }, + + }; +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 1efa159..4a31976 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,12 @@ using BattSim; +using BattSim.Models; using Radzen; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + + +// Setup Frontend var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); builder.RootComponents.Add("head::after"); diff --git a/Services/BatterySimulator.cs b/Services/BatterySimulator.cs index 96441fc..0247fcd 100644 --- a/Services/BatterySimulator.cs +++ b/Services/BatterySimulator.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; -using BatteryCostAnalysis.Models; +using BattSim.Models; -namespace BatteryCostAnalysis.Services +namespace BattSim.Services { public class BatterySimulator { diff --git a/Services/DataLoader.cs b/Services/DataLoader.cs index 0e757f6..41c9f2b 100644 --- a/Services/DataLoader.cs +++ b/Services/DataLoader.cs @@ -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 LoadAndProcessData(string filePath) + public static async Task> LoadAndProcessData(IBrowserFile file) { var energyData = new List(); - 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; } - } +} } \ No newline at end of file diff --git a/Services/ReportGenerator.cs b/Services/ReportGenerator.cs index abffff9..2e8d174 100644 --- a/Services/ReportGenerator.cs +++ b/Services/ReportGenerator.cs @@ -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 {