import React, { useState, useEffect } from 'react';
import './EnergySimulation.css';
// Mock data for demonstration purposes
const mockEnergyData = {
solarGeneration: {
current: 85,
capacity: 150,
dailyForecast: [
{ hour: '06:00', value: 0 },
{ hour: '07:00', value: 15 },
{ hour: '08:00', value: 45 },
{ hour: '09:00', value: 75 },
{ hour: '10:00', value: 95 },
{ hour: '11:00', value: 120 },
{ hour: '12:00', value: 140 },
{ hour: '13:00', value: 150 },
{ hour: '14:00', value: 145 },
{ hour: '15:00', value: 130 },
{ hour: '16:00', value: 100 },
{ hour: '17:00', value: 70 },
{ hour: '18:00', value: 40 },
{ hour: '19:00', value: 10 },
{ hour: '20:00', value: 0 },
]
},
batteryStorage: {
current: 72,
capacity: 500,
chargingRate: 45,
dischargingRate: 60,
history: [
{ time: '00:00', level: 65 },
{ time: '01:00', level: 68 },
{ time: '02:00', level: 70 },
{ time: '03:00', level: 72 },
{ time: '04:00', level: 74 },
{ time: '05:00', level: 76 },
{ time: '06:00', level: 78 },
{ time: '07:00', level: 80 },
{ time: '08:00', level: 82 },
{ time: '09:00', level: 80 },
{ time: '10:00', level: 78 },
{ time: '11:00', level: 75 },
{ time: '12:00', level: 72 },
]
},
gridConsumption: {
current: 120,
peak: 250,
average: 180,
history: [
{ time: '00:00', value: 100 },
{ time: '01:00', value: 90 },
{ time: '02:00', value: 85 },
{ time: '03:00', value: 80 },
{ time: '04:00', value: 90 },
{ time: '05:00', value: 110 },
{ time: '06:00', value: 130 },
{ time: '07:00', value: 150 },
{ time: '08:00', value: 180 },
{ time: '09:00', value: 210 },
{ time: '10:00', value: 230 },
{ time: '11:00', value: 250 },
{ time: '12:00', value: 240 },
]
},
chargingDemand: {
current: 210,
activeChargers: 6,
totalChargers: 12,
byType: [
{ type: 'Level 2', count: 2, power: 20 },
{ type: 'DC Fast', count: 3, power: 150 },
{ type: 'Ultra Fast', count: 1, power: 350 }
],
forecast: [
{ time: '13:00', value: 220 },
{ time: '14:00', value: 240 },
{ time: '15:00', value: 260 },
{ time: '16:00', value: 280 },
{ time: '17:00', value: 300 },
{ time: '18:00', value: 280 },
{ time: '19:00', value: 250 },
{ time: '20:00', value: 220 },
{ time: '21:00', value: 180 },
{ time: '22:00', value: 150 },
{ time: '23:00', value: 120 },
]
}
};
function EnergySimulation() {
const [activeTab, setActiveTab] = useState('overview');
const [loading, setLoading] = useState(true);
const [simulationSpeed, setSimulationSpeed] = useState(1);
const [simulationRunning, setSimulationRunning] = useState(false);
const [simulationTime, setSimulationTime] = useState('12:00');
const [energyData, setEnergyData] = useState(mockEnergyData);
const [optimizationMode, setOptimizationMode] = useState('balanced');
useEffect(() => {
// Simulate loading
const timer = setTimeout(() => {
setLoading(false);
}, 1500);
return () => clearTimeout(timer);
}, []);
useEffect(() => {
let interval;
if (simulationRunning) {
interval = setInterval(() => {
// Update simulation time
setSimulationTime(prevTime => {
const [hours, minutes] = prevTime.split(':').map(Number);
let newMinutes = minutes + 15;
let newHours = hours;
if (newMinutes >= 60) {
newMinutes = 0;
newHours = (newHours + 1) % 24;
}
return `${newHours.toString().padStart(2, '0')}:${newMinutes.toString().padStart(2, '0')}`;
});
// Update energy data based on simulation
setEnergyData(prevData => {
// This would be a more complex algorithm in a real implementation
// For demo purposes, we're just making small random adjustments
const solarVariation = Math.random() * 10 - 5;
const batteryVariation = Math.random() * 3 - 1;
const gridVariation = Math.random() * 15 - 7;
const demandVariation = Math.random() * 20 - 10;
return {
solarGeneration: {
...prevData.solarGeneration,
current: Math.max(0, Math.min(prevData.solarGeneration.capacity, prevData.solarGeneration.current + solarVariation))
},
batteryStorage: {
...prevData.batteryStorage,
current: Math.max(0, Math.min(100, prevData.batteryStorage.current + batteryVariation))
},
gridConsumption: {
...prevData.gridConsumption,
current: Math.max(0, prevData.gridConsumption.current + gridVariation)
},
chargingDemand: {
...prevData.chargingDemand,
current: Math.max(0, prevData.chargingDemand.current + demandVariation)
}
};
});
}, 1000 / simulationSpeed);
}
return () => clearInterval(interval);
}, [simulationRunning, simulationSpeed]);
const toggleSimulation = () => {
setSimulationRunning(!simulationRunning);
};
const resetSimulation = () => {
setSimulationRunning(false);
setSimulationTime('12:00');
setEnergyData(mockEnergyData);
};
const handleOptimizationChange = (mode) => {
setOptimizationMode(mode);
// In a real implementation, this would adjust the simulation parameters
// For demo purposes, we'll just show different messages
let message;
switch(mode) {
case 'cost':
message = "Optimizing for lowest operational cost";
break;
case 'renewable':
message = "Maximizing renewable energy usage";
break;
case 'demand':
message = "Prioritizing charging demand fulfillment";
break;
default:
message = "Balanced optimization mode";
}
// This would show a notification in a real implementation
console.log(message);
};
// Helper function to render a gauge chart
const renderGauge = (value, max, label, color) => {
const percentage = (value / max) * 100;
const angle = (percentage / 100) * 180;
return (
);
};
// Helper function to render a simple bar chart
const renderBarChart = (data, valueKey, labelKey, color) => {
const maxValue = Math.max(...data.map(item => item[valueKey]));
return (
{data.map((item, index) => (
))}
);
};
// Helper function to render the energy flow diagram
const renderEnergyFlow = () => {
const { solarGeneration, batteryStorage, gridConsumption, chargingDemand } = energyData;
// Calculate flow widths based on energy values
const solarWidth = Math.min(100, (solarGeneration.current / 200) * 100);
const gridWidth = Math.min(100, (gridConsumption.current / 300) * 100);
const batteryWidth = Math.min(100, (batteryStorage.chargingRate / 100) * 100);
const demandWidth = Math.min(100, (chargingDemand.current / 400) * 100);
return (
☀️
Solar Array
{solarGeneration.current} kW
⚡
Grid Power
{gridConsumption.current} kW
🔋
Battery Storage
{batteryStorage.current}%
{batteryStorage.capacity} kWh
🚗
Charging Demand
{chargingDemand.current} kW
{chargingDemand.activeChargers}/{chargingDemand.totalChargers} Chargers Active
);
};
if (loading) {
return (
Initializing Energy Simulation...
);
}
return (
{activeTab === 'overview' && (
Current Energy Balance
Total Input:
{energyData.solarGeneration.current + energyData.gridConsumption.current} kW
Total Demand:
{energyData.chargingDemand.current} kW
Battery Change:
energyData.batteryStorage.dischargingRate ? 'positive' : 'negative'}`}>
{energyData.batteryStorage.chargingRate > energyData.batteryStorage.dischargingRate ? '+' : '-'}
{Math.abs(energyData.batteryStorage.chargingRate - energyData.batteryStorage.dischargingRate)} kW
Renewable Ratio:
{Math.round((energyData.solarGeneration.current / (energyData.solarGeneration.current + energyData.gridConsumption.current)) * 100)}%
{renderEnergyFlow()}
Optimization Mode: {optimizationMode.charAt(0).toUpperCase() + optimizationMode.slice(1)}
)}
{activeTab === 'solar' && (
{renderGauge(
energyData.solarGeneration.current,
energyData.solarGeneration.capacity,
'kW',
'#FFD700'
)}
Current Solar Generation
Daily Production Forecast
{renderBarChart(
energyData.solarGeneration.dailyForecast,
'value',
'hour',
'#FFD700'
)}
Solar Array Details
Array Capacity:
{energyData.solarGeneration.capacity} kW
Panel Type:
Monocrystalline
Panel Count:
375
Array Area:
750 m²
Efficiency:
22.5%
Installation Date:
March 2025
)}
{activeTab === 'battery' && (
{renderGauge(
energyData.batteryStorage.current,
100,
'%',
'#00E0FF'
)}
Current Battery Level
Battery Status
Capacity:
{energyData.batteryStorage.capacity} kWh
Charging Rate:
{energyData.batteryStorage.chargingRate} kW
Discharging Rate:
{energyData.batteryStorage.dischargingRate} kW
Net Flow:
energyData.batteryStorage.dischargingRate ? 'positive' : 'negative'}`}>
{energyData.batteryStorage.chargingRate > energyData.batteryStorage.dischargingRate ? '+' : '-'}
{Math.abs(energyData.batteryStorage.chargingRate - energyData.batteryStorage.dischargingRate)} kW
Estimated Runtime:
{Math.round((energyData.batteryStorage.current / 100) * energyData.batteryStorage.capacity / energyData.batteryStorage.dischargingRate)} hours
Health:
98%
Battery Level History
{renderBarChart(
energyData.batteryStorage.history,
'level',
'time',
'#00E0FF'
)}
Battery Management Controls
Priority Mode:
)}
{activeTab === 'grid' && (
{renderGauge(
energyData.gridConsumption.current,
energyData.gridConsumption.peak,
'kW',
'#FF5555'
)}
Current Grid Consumption
Grid Connection Status
Connection:
Active
Peak Capacity:
{energyData.gridConsumption.peak} kW
Average Load:
{energyData.gridConsumption.average} kW
Current Rate:
$0.12/kWh
Peak Hours:
2:00 PM - 7:00 PM
Grid Stability:
Stable
Grid Consumption History
{renderBarChart(
energyData.gridConsumption.history,
'value',
'time',
'#FF5555'
)}
Grid Integration Controls
)}
{activeTab === 'demand' && (
{renderGauge(
energyData.chargingDemand.current,
energyData.chargingDemand.byType.reduce((sum, item) => sum + item.count * item.power, 0),
'kW',
'#35F2DB'
)}
Current Charging Demand
Charger Status
Active Chargers:
{energyData.chargingDemand.activeChargers}/{energyData.chargingDemand.totalChargers}
Utilization:
{Math.round((energyData.chargingDemand.activeChargers / energyData.chargingDemand.totalChargers) * 100)}%
Average Session:
42 minutes
Queue Length:
3 vehicles
Wait Time:
15 minutes
Charger Types and Usage
{energyData.chargingDemand.byType.map((type, index) => (
{type.type}
{type.power} kW
{type.count}
Chargers
{Math.round((type.count / energyData.chargingDemand.totalChargers) * 100)}% of Total
sum + item.count * item.power, 0)) * 100}%`,
background: `linear-gradient(90deg, #00E0FF, #00E0FFCC)`
}}
>
{Math.round((type.count * type.power / energyData.chargingDemand.byType.reduce((sum, item) => sum + item.count * item.power, 0)) * 100)}% of Power
))}
Demand Forecast
{renderBarChart(
energyData.chargingDemand.forecast,
'value',
'time',
'#35F2DB'
)}
)}
{activeTab === 'optimization' && (
Optimization Mode Selection
handleOptimizationChange('balanced')}
>
⚖️
Balanced
Optimize for balanced performance across all metrics
- Moderate cost efficiency
- Good renewable utilization
- Reliable charging availability
handleOptimizationChange('cost')}
>
💰
Cost Optimization
Minimize operational costs and maximize revenue
- Reduced grid consumption during peak hours
- Strategic battery usage
- Dynamic pricing implementation
handleOptimizationChange('renewable')}
>
🌱
Renewable Maximization
Prioritize usage of renewable energy sources
- Highest solar utilization
- Reduced carbon footprint
- Battery storage optimization
handleOptimizationChange('demand')}
>
⚡
Demand Response
Focus on meeting charging demand and reducing wait times
- Maximum charger availability
- Reduced wait times
- Optimized charging speeds
Projected Optimization Results
Daily Operational Cost
Current:
$480
Optimized:
${optimizationMode === 'cost' ? 380 : optimizationMode === 'balanced' ? 420 : optimizationMode === 'renewable' ? 450 : 460}
Savings:
${optimizationMode === 'cost' ? 100 : optimizationMode === 'balanced' ? 60 : optimizationMode === 'renewable' ? 30 : 20}
({optimizationMode === 'cost' ? 21 : optimizationMode === 'balanced' ? 13 : optimizationMode === 'renewable' ? 6 : 4}%)
Renewable Energy Usage
Current:
42%
Optimized:
{optimizationMode === 'renewable' ? 68 : optimizationMode === 'balanced' ? 55 : optimizationMode === 'cost' ? 48 : 45}%
Improvement:
+{optimizationMode === 'renewable' ? 26 : optimizationMode === 'balanced' ? 13 : optimizationMode === 'cost' ? 6 : 3}%
Average Wait Time
Current:
15 min
Optimized:
{optimizationMode === 'demand' ? 5 : optimizationMode === 'balanced' ? 8 : optimizationMode === 'cost' ? 12 : 10} min
Reduction:
-{optimizationMode === 'demand' ? 10 : optimizationMode === 'balanced' ? 7 : optimizationMode === 'cost' ? 3 : 5} min
({optimizationMode === 'demand' ? 67 : optimizationMode === 'balanced' ? 47 : optimizationMode === 'cost' ? 20 : 33}%)
Carbon Footprint
Current:
320 kg CO₂/day
Optimized:
{optimizationMode === 'renewable' ? 180 : optimizationMode === 'balanced' ? 240 : optimizationMode === 'cost' ? 280 : 300} kg CO₂/day
Reduction:
-{optimizationMode === 'renewable' ? 140 : optimizationMode === 'balanced' ? 80 : optimizationMode === 'cost' ? 40 : 20} kg
({optimizationMode === 'renewable' ? 44 : optimizationMode === 'balanced' ? 25 : optimizationMode === 'cost' ? 13 : 6}%)
)}
);
}
export default EnergySimulation;