fixed chart names and added extra battery properties.
This commit is contained in:
@@ -5,7 +5,7 @@ namespace BattSim.Models
|
||||
public class EnergyData
|
||||
{
|
||||
public DateTime Time { get; set; }
|
||||
public bool DayTarif { get; set; }
|
||||
public bool DayTariff { get; set; }
|
||||
public double Consumption { get; set; }
|
||||
public double Production { get; set; }
|
||||
}
|
||||
|
||||
106
Pages/Home.razor
106
Pages/Home.razor
@@ -9,32 +9,48 @@
|
||||
<h1>BattSim</h1>
|
||||
|
||||
<h2>Input Data</h2>
|
||||
<p>Upload your fluvius daily csv file here.</p>
|
||||
<InputFile OnChange="LoadCsvFile" accept=".csv"/>
|
||||
@if (_isLoadingFile){ <p>Loading...</p> }
|
||||
@if (EnergyData.Length != 0){
|
||||
<RadzenChart>
|
||||
<RadzenAreaSeries Smooth=true Data="@EnergyData" CategoryProperty="Time" Title="Consumption" ValueProperty="Consumption">
|
||||
<RadzenChartTooltipOptions Visible="true" />
|
||||
</RadzenAreaSeries>
|
||||
<RadzenAreaSeries Smooth=true Data="@EnergyData" CategoryProperty="Time" Title="Production" ValueProperty="Production">
|
||||
<RadzenChartTooltipOptions Visible="true" />
|
||||
</RadzenAreaSeries>
|
||||
<RadzenCategoryAxis Formatter="@FormatObject" Padding="20" LabelAutoRotation="-45">
|
||||
<RadzenAxisTitle Text="Time" />
|
||||
</RadzenCategoryAxis>
|
||||
<RadzenValueAxis Formatter="@FormatObject">
|
||||
<RadzenGridLines Visible="true" />
|
||||
<RadzenAxisTitle Text="Energy" />
|
||||
</RadzenValueAxis>
|
||||
</RadzenChart>
|
||||
}
|
||||
<div>
|
||||
<p>Upload your Fluvius quarterly csv file here. The longer the timeframe the longer it takes to process.</p>
|
||||
<InputFile OnChange="OnFileUploaded" accept=".csv"/>
|
||||
@if (_isLoadingFile)
|
||||
{
|
||||
<p>Loading...</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (FluviusDataRaw.Count != 0)
|
||||
{
|
||||
<p>@(FluviusDataRaw.Count) entries read.</p>
|
||||
}
|
||||
|
||||
@if (FluviusDataDaily.Count != 0)
|
||||
{
|
||||
<RadzenChart>
|
||||
<RadzenAreaSeries Smooth=true Data="@FluviusDataDaily.Values" CategoryProperty="Time" Title="Consumption" ValueProperty="Consumption">
|
||||
<RadzenChartTooltipOptions Visible="true"/>
|
||||
</RadzenAreaSeries>
|
||||
<RadzenAreaSeries Smooth=true Data="@FluviusDataDaily.Values" CategoryProperty="Time" Title="Production" ValueProperty="Production">
|
||||
<RadzenChartTooltipOptions Visible="true"/>
|
||||
</RadzenAreaSeries>
|
||||
<RadzenCategoryAxis Formatter="@FormatObject" Padding="20" LabelAutoRotation="-45">
|
||||
<RadzenGridLines Visible="true"/>
|
||||
<RadzenAxisTitle Text="Time"/>
|
||||
</RadzenCategoryAxis>
|
||||
<RadzenValueAxis Formatter="@FormatObject">
|
||||
<RadzenGridLines Visible="true"/>
|
||||
<RadzenAxisTitle Text="Energy"/>
|
||||
</RadzenValueAxis>
|
||||
</RadzenChart>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
|
||||
@code{
|
||||
EnergyData[] EnergyData = [];
|
||||
Dictionary<DateTime, EnergyData> FluviusDataRaw = [];
|
||||
Dictionary<DateOnly, EnergyData> FluviusDataDaily = [];
|
||||
bool _isLoadingFile = false;
|
||||
|
||||
private async Task LoadCsvFile(InputFileChangeEventArgs e)
|
||||
private async Task OnFileUploaded(InputFileChangeEventArgs e)
|
||||
{
|
||||
var file = e.File;
|
||||
if (file.ContentType != "text/csv")
|
||||
@@ -48,11 +64,8 @@
|
||||
Console.WriteLine("Reading csv file...");
|
||||
_isLoadingFile = true;
|
||||
StateHasChanged();
|
||||
|
||||
var loadingTask = DataLoader.LoadAndProcessData(file);
|
||||
var energyData = await loadingTask;
|
||||
EnergyData = energyData.ToArray();
|
||||
|
||||
FluviusDataRaw = await FluviusDataHandler.LoadAndProcessFile(file);
|
||||
FluviusDataDaily = FluviusDataHandler.GenerateDailyData(FluviusDataRaw);
|
||||
_isLoadingFile = false;
|
||||
StateHasChanged();
|
||||
Console.WriteLine("Done reading csv file!");
|
||||
@@ -65,27 +78,44 @@
|
||||
|
||||
private void OnSeriesClick(){}
|
||||
private string FormatObject(object value) {
|
||||
if(value is double d) return $"{value:0.##} kWh";
|
||||
if(value is DateOnly date) return (date.Day == 1) ? date.ToString("MM/yyyy") : string.Empty;
|
||||
else return string.Empty;
|
||||
if(value is double d) return $"{d:0.##} kWh";
|
||||
if(value is DateTime date) return date.ToString("dd/MM/yyyy");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
<h2>Simulate Battery</h2>
|
||||
<p>Set the battery capacity</p>
|
||||
<InputNumber @bind-value="BatteryCapacity"/>
|
||||
<Button @onclick="SimulateBattery">Simulate</Button>
|
||||
|
||||
<h2>Calculate Cost</h2>
|
||||
|
||||
<div>
|
||||
<p>Generate adjusted energy data simulating the effect of a battery with properties as configured here:</p>
|
||||
<ul>
|
||||
<li>
|
||||
<p>Capacity: <InputNumber @bind-value="BatteryCapacity"/> kWh</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Discharge Rate: <InputNumber @bind-value="DischargeRate"/> kW</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Charge Rate: <InputNumber @bind-value="ChargeRate"/> kW</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Round-trip Efficiency: <InputNumber @bind-value="Efficiency"/> %</p>
|
||||
</li>
|
||||
</ul>
|
||||
<Button @onclick="SimulateBattery">Simulate</Button>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
double BatteryCapacity = 0.0;
|
||||
double BatteryCapacity = 7.5;
|
||||
double DischargeRate = 3;
|
||||
double ChargeRate = 3;
|
||||
double Efficiency = 90;
|
||||
BatteryDayResult[] SimulationData = [];
|
||||
|
||||
private async Task SimulateBattery(){
|
||||
Console.WriteLine("Simulating...");
|
||||
SimulationData = BatterySimulator.SimulateBattery(EnergyData, BatteryCapacity).ToArray();
|
||||
// SimulationData = BatterySimulator.SimulateBattery(EnergyData, BatteryCapacity).ToArray();
|
||||
Console.WriteLine("Done simulating!");
|
||||
}
|
||||
}
|
||||
|
||||
<h2>Calculate Cost</h2>
|
||||
|
||||
@@ -12,5 +12,4 @@ builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
builder.Services.AddRadzenComponents();
|
||||
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
@@ -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)
|
||||
28025
data/Verbruikshistoriek_kwartiertotalen_all.csv
Normal file
28025
data/Verbruikshistoriek_kwartiertotalen_all.csv
Normal file
File diff suppressed because it is too large
Load Diff
5569
data/Verbruikshistoriek_kwartiertotalen_month.csv
Normal file
5569
data/Verbruikshistoriek_kwartiertotalen_month.csv
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user