Skip to content
Snippets Groups Projects
Commit 57d1593c authored by Daniel Lorych's avatar Daniel Lorych :cookie:
Browse files

Add Configuration Service and Controller

parent e1f49035
Branches
No related tags found
No related merge requests found
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();
}
}
}
......@@ -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" />
......
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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment