From 57d1593c170861a02ab3526cacf2a3a4a8a2267e Mon Sep 17 00:00:00 2001
From: Dan <daniel2.lorych@live.uwe.ac.uk>
Date: Sun, 1 May 2022 18:51:14 +0100
Subject: [PATCH] Add Configuration Service and Controller

---
 Helios/Controllers/ConfiguratorController.cs |  27 +++++
 Helios/Helios.csproj                         |   2 +
 Helios/Source/ConfiguratorService.cs         | 111 +++++++++++++++++++
 3 files changed, 140 insertions(+)
 create mode 100644 Helios/Controllers/ConfiguratorController.cs
 create mode 100644 Helios/Source/ConfiguratorService.cs

diff --git a/Helios/Controllers/ConfiguratorController.cs b/Helios/Controllers/ConfiguratorController.cs
new file mode 100644
index 0000000..9060e96
--- /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 fc9d274..94eff88 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 0000000..749a519
--- /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;
+		}
+	}
+}
+
-- 
GitLab