diff --git a/Helios/Controllers/ConfiguratorController.cs b/Helios/Controllers/ConfiguratorController.cs new file mode 100644 index 0000000000000000000000000000000000000000..9060e96608d18bba3dfc204a06545f9424459c40 --- /dev/null +++ b/Helios/Controllers/ConfiguratorController.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Helios.Source; +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace Helios.Controllers +{ + public class ConfiguratorController : Controller + { + private readonly ConfiguratorService _service; + + public ConfiguratorController(ConfiguratorService service) + { + _service = service; + } + + public IActionResult Index() + { + return View(); + } + } +} + diff --git a/Helios/Helios.csproj b/Helios/Helios.csproj index fc9d27436ca1afc4e77c5d53f73a0b8fe23f2f93..94eff889a551ff3407508f9987e16dae6a3f74ac 100644 --- a/Helios/Helios.csproj +++ b/Helios/Helios.csproj @@ -12,10 +12,12 @@ <None Remove="Microsoft.VisualStudio.Web.CodeGeneration.Design" /> <None Remove="Swashbuckle.AspNetCore" /> <None Remove="Views\Login\" /> + <None Remove="Views\Configurator\" /> </ItemGroup> <ItemGroup> <Folder Include="Source\" /> <Folder Include="Views\Login\" /> + <Folder Include="Views\Configurator\" /> </ItemGroup> <ItemGroup> <PackageReference Include="Npgsql" Version="6.0.4" /> diff --git a/Helios/Source/ConfiguratorService.cs b/Helios/Source/ConfiguratorService.cs new file mode 100644 index 0000000000000000000000000000000000000000..749a519de39674cf935d56c4dd9d4bc6444292ff --- /dev/null +++ b/Helios/Source/ConfiguratorService.cs @@ -0,0 +1,111 @@ +using System; +using Helios.Models; +using Npgsql; + +namespace Helios.Source +{ + public class ConfiguratorService + { + private readonly NpgsqlConnection _connection; + private readonly List<SolarPanel> _solarPanels; + private readonly SunElevation _maxElevation; + + public ConfiguratorService(string connectionString) + { + _connection = new NpgsqlConnection(connectionString); + + this._solarPanels = GetSolarPanels(); + this._maxElevation = GetSunElevation(); + } + + public List<ConfiguratorResult> GetResults(List<Roof> roofs, int requiredPower, int maxPower, int budget) + { + List<ConfiguratorResult> results = new List<ConfiguratorResult>(); + + foreach (var roof in roofs) + { + foreach (SolarPanel panel in _solarPanels) + { + ConfiguratorResult result = new ConfiguratorResult(); + + result.RoofArea = roof.Area; + result.RoofAngle = roof.Elevation; + result.PanelEfficiency = panel.Efficiency; + result.InstallationCost = panel.GetInstallationCost(roof.Area); + result.PowerGeneratedSummer = panel.GetPowerOutput(roof.Area, _maxElevation.SummerElevation); + result.PowerGeneratedWinter = panel.GetPowerOutput(roof.Area, _maxElevation.WinterElevation); + result.GeneratesRequiredPower = GeneratesRequiredPower(requiredPower, result.PowerGeneratedWinter); + result.ExceedsMaximumPower = ExceedsPowerLimit(maxPower, result.PowerGeneratedWinter); + result.WithinBudget = WithinBudget(budget, result.InstallationCost); + + results.Add(result); + } + } + + return results; + } + + private bool WithinBudget(int budget, double installationCost) + { + return installationCost > budget; + } + + private bool GeneratesRequiredPower(int requiredPower, double powerGenerated) + { + return powerGenerated >= requiredPower; + } + + private bool ExceedsPowerLimit(int maxPower, double powerGenerated) + { + return powerGenerated >= maxPower; + } + + + private List<SolarPanel> GetSolarPanels() + { + List<SolarPanel> solarPanels = new List<SolarPanel>(); + + _connection.Open(); + + NpgsqlCommand cmd = new NpgsqlCommand("select efficiency, area_cost from solar_panel", _connection); + + using var reader = cmd.ExecuteReader(); + + while (reader.Read()) + { + solarPanels.Add(new SolarPanel + { + Efficiency = double.Parse(reader["efficiency"].ToString()), + CostPerMetreSquared = double.Parse(reader["area_cost"].ToString()) + }); + } + + _connection.Close(); + + return solarPanels; + } + + private SunElevation GetSunElevation() + { + SunElevation elevation = new SunElevation(); + + _connection.Open(); + + NpgsqlCommand cmd = new NpgsqlCommand("select \"time\", summer_eleveation, winter_eleveation from sun_elevation where \"time\" = '12:00'", _connection); + + using var reader = cmd.ExecuteReader(); + + if (reader.Read()) + { + elevation.Time = DateTime.Parse(reader["time"].ToString()); + elevation.SummerElevation = int.Parse(reader["summer_eleveation"].ToString()); + elevation.WinterElevation = int.Parse(reader["winter_eleveation"].ToString()); + } + + _connection.Close(); + + return elevation; + } + } +} +