Skip to content

Commit

Permalink
Merge pull request #146 from COS301-SE-2023/development
Browse files Browse the repository at this point in the history
Updating Deployment
  • Loading branch information
jason-dutton authored Oct 22, 2023
2 parents 6b47be0 + 3bfe0b7 commit 3864561
Show file tree
Hide file tree
Showing 13 changed files with 798 additions and 390 deletions.
29 changes: 27 additions & 2 deletions src/apps/api/Repository/BusinessBestSolarPanelsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public class BusinessBestSolarPanelsRepository
private SharedUtils.locationDataClass locationDataClass = new SharedUtils.locationDataClass();
private DataHandlers.SolarDataHandler solarCalculator = new DataHandlers.SolarDataHandler();
private DataHandlers.RooftopDataHandler rooftopDataHandler = new DataHandlers.RooftopDataHandler();
private SharedUtils.otherDataClass otherDataClass = new SharedUtils.otherDataClass();
private string locationName = "";
private string? mapboxAccessToken = Environment.GetEnvironmentVariable("MAP_BOX_API_KEY");


public async Task<string> GetProcessedDataAsync(BestSolarPanelsInput bestSolarPanelsInput)
{
Expand All @@ -32,7 +33,7 @@ public async Task<string> GetProcessedDataAsync(BestSolarPanelsInput bestSolarPa
}

LocationDataModel? locationData = await locationDataClass.GetLocationData(latitude, longitude);
locationName = await otherDataClass.GetLocationNameFromCoordinates(latitude, longitude);
locationName = await GetLocationNameFromCoordinates(latitude, longitude);
if (locationData == null)
{
await locationDataClass.CreateLocationData(latitude, longitude, locationName);
Expand Down Expand Up @@ -82,4 +83,28 @@ private async Task<LocationDataModel> GetLocationDataModel(double latitude, doub
}
return locationData!;
}
public async Task<string> GetLocationNameFromCoordinates(double latitude, double longitude)
{
string baseUrl = "https://api.mapbox.com/geocoding/v5/mapbox.places/";
string requestUrl =
$"{baseUrl}{longitude.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")}.json?&access_token={mapboxAccessToken}";

try
{
HttpClient httpClient = new HttpClient();
var mapResponse = await httpClient.GetFromJsonAsync<GeocodingResponse>(requestUrl);
return mapResponse?.Features[0].Place_Name ?? "";
}
catch (Exception ex)
{
// Handle any errors or exceptions
Console.WriteLine(ex.Message);
return "";
}

}
private class GeocodingResponse
{
public List<LocationSuggestion> Features { get; set; } = new List<LocationSuggestion>();
}
}
163 changes: 153 additions & 10 deletions src/apps/api/Repository/BusinessRequestDataRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Newtonsoft.Json;
using System.Text.Json;
using System.Net.Http.Json;
using System.Net;

namespace Api.Repository;

Expand All @@ -12,14 +15,11 @@ private struct DataType {
public const string SUNLIGHT_HOURS = "sunlight hours";
}

private SharedUtils.locationDataClass locationDataClass = new SharedUtils.locationDataClass();
private string? mapboxAccessToken = Environment.GetEnvironmentVariable("MAP_BOX_API_KEY");
private DataHandlers.SolarDataHandler solarCalculator = new DataHandlers.SolarDataHandler();
private DataHandlers.RooftopDataHandler rooftopDataHandler = new DataHandlers.RooftopDataHandler();
private SharedUtils.otherDataClass otherDataClass = new SharedUtils.otherDataClass();
private DataHandlers.SolarDataHandler solarDataHandler = new DataHandlers.SolarDataHandler();
// private DataHandlers.RooftopDataHandler rooftopDataHandler = new DataHandlers.RooftopDataHandler();
private string locationName = "";

private string express = "http://localhost:3333";
private string? API_PORT = Environment.GetEnvironmentVariable("API_PORT");
public BusinessRequestDataRepository()
Expand All @@ -44,11 +44,11 @@ public async Task<string> GetProcessedDataAsync(BusinessRequestData requestData)
var client = new HttpClient();
var dataTypeResponse = new HttpResponseMessage();

LocationDataModel? locationData = await locationDataClass.GetLocationData(latitude, longitude);
locationName = await otherDataClass.GetLocationNameFromCoordinates(latitude, longitude);
LocationDataModel? locationData = await GetLocationData(latitude, longitude);
locationName = await GetLocationNameFromCoordinates(latitude, longitude);
if (locationData == null)
{
await locationDataClass.CreateLocationData(latitude, longitude, locationName);
await CreateLocationData(latitude, longitude, locationName);
}

typeOfData = data!;
Expand Down Expand Up @@ -127,13 +127,156 @@ private Address getAddress(){

private async Task<LocationDataModel> GetLocationDataModel(double latitude, double longitude)
{
LocationDataModel? locationData = await locationDataClass.GetLocationData(latitude, longitude);
LocationDataModel? locationData = await GetLocationData(latitude, longitude);
if(locationData==null){
locationData = await locationDataClass.CreateLocationData(latitude, longitude, locationName);
await CreateLocationData(latitude, longitude, locationName);
locationData = await GetLocationData(latitude, longitude);
}
return locationData!;
}


public async Task<LocationDataModel> GetLocationData(double latitude, double longitude)
{
try
{
var client = new HttpClient();
var request = new HttpRequestMessage(
HttpMethod.Get,
express + "/api/locationData/" + latitude + "/" + longitude
);
Console.WriteLine("Getting LocationData for " + latitude + ", " + longitude);
var response = await client.SendAsync(request);

if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
LocationDataModelTemp locationDataTemp = System.Text.Json.JsonSerializer.Deserialize<LocationDataModelTemp>(data)!;
LocationDataModel locationData = new LocationDataModel()
{
latitude = locationDataTemp.latitude,
longitude = locationDataTemp.longitude,
locationName = locationDataTemp.locationName,
solarPanelsData = System.Text.Json.JsonSerializer.Deserialize<RooftopInformationModel>(locationDataTemp.solarPanelsData!),
satteliteImageData = Convert.FromBase64String(locationDataTemp.satteliteImageData!),
annualFluxData = Convert.FromBase64String(locationDataTemp.annualFluxData!),
monthlyFluxData = Convert.FromBase64String(locationDataTemp.monthlyFluxData!),
maskData = Convert.FromBase64String(locationDataTemp.maskData!),
dateCreated = locationDataTemp.dateCreated,
horisonElevationData = locationDataTemp.horisonElevationData
};
return locationData!;
}else if (response.StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine(".NET LocationData not found");
return null!;
}
else
{
Console.WriteLine(".NET Error getting LocationData");
throw new Exception("Error getting LocationData");
}

}
catch (System.Exception)
{
throw new Exception("Could not get LocationData");
}
}

public async Task<string> CreateLocationData(
double latitude,
double longitude,
string locationName

)
{


LocationDataModel? locationData = await GetLocationDataModel(latitude, longitude);
if(locationData!=null){
return "LocationData could not be created";
}
string solarPanelsData = locationData!.solarPanelsData!.ToString()!;
byte[] satteliteImageData = locationData!.satteliteImageData!;
byte[] annualFluxData = locationData!.annualFluxData!;
byte[] monthlyFluxData = locationData!.monthlyFluxData!;
byte[] maskData = locationData!.maskData!;
string horisonElevationData = locationData!.horisonElevationData!;
try
{
var client = new HttpClient();
var request = new HttpRequestMessage(
HttpMethod.Post,
express + "/api/locationData/create"
);
string satteliteImageDataBase64 = Convert.ToBase64String(satteliteImageData);
string annualFluxDataBase64 = Convert.ToBase64String(annualFluxData);
string monthlyFluxDataBase64 = Convert.ToBase64String(monthlyFluxData);
string maskDataBase64 = Convert.ToBase64String(maskData);

var postData = new
{
latitude = latitude.ToString(),
longitude = longitude.ToString(),
locationName = locationName,
solarPanelsData = solarPanelsData,
satteliteImageData = satteliteImageDataBase64,
annualFluxData = annualFluxDataBase64,
monthlyFluxData = monthlyFluxDataBase64,
maskData = maskDataBase64,
horisonElevationData = horisonElevationData
};

var json = System.Text.Json.JsonSerializer.Serialize(postData);
request.Content = new StringContent(json, null, "application/json");
// Console.WriteLine(await request.Content.ReadAsStringAsync());
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(".NET LocationData created successfully");
return "LocationData created successfully";
}
else if (response.StatusCode == HttpStatusCode.BadRequest)
{
Console.WriteLine(".NET Bad Request When Creating LocationData");
// Get error
string data = response.Content.ReadAsStringAsync().Result;
return data;
}
else
{
Console.WriteLine(".NET Error creating LocationData");
throw new Exception("Error creating LocationData");
}

}
catch (System.Exception)
{
throw new Exception("Could not create locationData");
}
}

public async Task<string> GetLocationNameFromCoordinates(double latitude, double longitude)
{
string baseUrl = "https://api.mapbox.com/geocoding/v5/mapbox.places/";
string requestUrl =
$"{baseUrl}{longitude.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")}.json?&access_token={mapboxAccessToken}";

try
{
HttpClient httpClient = new HttpClient();
var mapResponse = await httpClient.GetFromJsonAsync<GeocodingResponse>(requestUrl);
return mapResponse?.Features[0].Place_Name ?? "";
}
catch (Exception ex)
{
// Handle any errors or exceptions
Console.WriteLine(ex.Message);
return "";
}
}
private class GeocodingResponse
{
public List<LocationSuggestion> Features { get; set; } = new List<LocationSuggestion>();
}
}
19 changes: 5 additions & 14 deletions src/apps/blazor-app/Components/Advanced/BuildYourHome.razor
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,11 @@
</div>
<DaylightChart hours="@dlh" daylightHours="@daylight" />
<div class="flex w-full justify-center absolute top-1/2 left-0 items-center gap-1 mt-5">
@{
if (dlh == (24 - daylight))
{
<p class="text-5xl text-primary-900 font-semibold">@dlh+</p>
}
else
{
<p class="text-5xl text-primary-900 font-semibold">@dlh</p>
}
}
@if(float.IsInfinity(dlh)) {
<p class="text-5xl text-primary-900 font-semibold">12+</p>
} else {
<p class="text-5xl text-primary-900 font-semibold">@dlh</p>
}
<p class="text-5xl text-gray-400 font-bold">h</p>
</div>
</div>
Expand Down Expand Up @@ -769,10 +764,6 @@

double runningHours = chargingLimit / appliancePowerUsage;
double runningHoursPercentage = (runningHours / daylight) * 100;
if (runningHoursPercentage > 100)
{
runningHoursPercentage = 100;
}
dlh = (float)Math.Round((24 - daylight) * (runningHoursPercentage / 100), 2);
}

Expand Down
Loading

0 comments on commit 3864561

Please sign in to comment.