@page "/"
@using Radzen
@using Radzen.Blazor
@using BattSim.Models
@using BattSim.Services
@using BattSim.Pages.Nodes
Wattif
Wattif - Simuleer verbeteringen aan je energiesysteem
@if (FilteredFluviusData.Length > 0)
{
Overzicht: Energie Data & Simulatie
@if (FilteredSimulationData.Length > 0)
{
}
}
@code {
EnergyData[] FluviusDataRaw = [];
EnergyData[] SimulationData = [];
DataFilter.FilterOption SelectedFilterOption = DataFilter.FilterOption.ALL;
EnergyData[] FilteredFluviusData = [];
EnergyData[] FilteredSimulationData = [];
EnergyCostCalculator calculator = new();
double normalCost = 0.0;
double simulatedCost = 0.0;
PipelineStep[] StepData = [
new PipelineStep(), // Step 1
new PipelineStep(), // Step 2
new PipelineStep(), // Step 3
];
string _uploadedFileName = string.Empty;
string? _uploadError = null;
string _timePeriod = "all";
double BatteryCapacity = 7.5;
double Efficiency = 90;
private string GetTimeButtonClass(string period)
{
var baseClass = "time-toggle-button";
if (_timePeriod == period)
{
return $"{baseClass} active";
}
return baseClass;
}
private void SetTimePeriodAll() => SetTimePeriod("all");
private void SetTimePeriodYear() => SetTimePeriod("year");
private void SetTimePeriodMonth() => SetTimePeriod("month");
private void SetTimePeriodWeek() => SetTimePeriod("week");
private void SetTimePeriodDay() => SetTimePeriod("day");
private void ToggleBox(int step)
{
if(step == 1 && FluviusDataRaw.Length == 0) return;
if(step == 2 && SimulationData.Length == 0) return;
StepData[step].Expanded = !StepData[step].Expanded;
StateHasChanged();
}
private async Task OnFileUploaded(InputFileChangeEventArgs e)
{
_uploadError = null;
var file = e.File;
_uploadedFileName = file.Name;
if (file.ContentType != "text/csv")
{
_uploadError = "Alleen CSV bestanden zijn toegestaan!";
StateHasChanged();
return;
}
try
{
StepData[0].isProcessing = true;
StateHasChanged();
FluviusDataRaw = await FluviusDataHandler.LoadAndProcessFile(file);
SetTimePeriod("all");
FilteredSimulationData = [];
StepData[0].isProcessing = false;
StepData[0].Completed = true;
StepData[1].Completed = false;
StepData[2].Completed = false;
SimulationData = [];
StateHasChanged();
}
catch (Exception ex)
{
StepData[0].isProcessing = false;
_uploadError = ex.Message;
StateHasChanged();
}
}
private void SimulateBattery()
{
if (FluviusDataRaw.Length == 0) return;
StepData[1].isProcessing = true;
StateHasChanged();
try
{
SimulationData = BatterySimulator.SimulateBattery(FluviusDataRaw, BatteryCapacity, Efficiency/100);
SetTimePeriod("all");
StepData[1].isProcessing = false;
StepData[1].Completed = true;
StepData[2].Completed = false;
normalCost = 0;
simulatedCost = 0;
StateHasChanged();
}
catch (Exception ex)
{
StepData[1].isProcessing = false;
Console.WriteLine($"Error: {ex.Message}");
}
}
private void Calculate()
{
if (SimulationData.Length == 0) return;
StepData[2].isProcessing = true;
StateHasChanged();
try
{
normalCost = calculator.CalculateCostOfEnergyUsage(FluviusDataRaw);
simulatedCost = calculator.CalculateCostOfEnergyUsage(SimulationData);
StepData[2].isProcessing = false;
StepData[2].Completed = true;
StateHasChanged();
}
catch (Exception ex)
{
StepData[2].isProcessing = false;
Console.WriteLine($"Error: {ex.Message}");
}
}
private void SetTimePeriod(string period)
{
_timePeriod = period;
var filterOption = period switch {
"all" => DataFilter.FilterOption.ALL,
"year" => DataFilter.FilterOption.YEAR,
"month" => DataFilter.FilterOption.MONTH,
"week" => DataFilter.FilterOption.WEEK,
"day" => DataFilter.FilterOption.DAY,
};
// Filter data based on selected period
if(FluviusDataRaw.Length > 0) FilteredFluviusData = DataFilter.FilterData(FluviusDataRaw, filterOption);
if(SimulationData.Length > 0) FilteredSimulationData = DataFilter.FilterData(SimulationData, filterOption);
StateHasChanged();
}
private string FormatObject(object value) {
if(value is double d) return $"{d:0.##} kWh";
if(value is DateTime time) return time.ToString("dd/MM/yyyy");
if(value is DateOnly date) return date.ToString("dd/MM/yyyy");
return string.Empty;
}
}