Skip to content

Commit

Permalink
Feature branch1 (#7)
Browse files Browse the repository at this point in the history
* trying to fix background

* styling

* last edit of the day

* including left out files

* small edit

* updated functions

* added checkboxes

* added button to refresh data(CRUD delete)

* moved button

* added image

* added background

* updated image

* updated logo

* fixed image

* updated checkboxes

* added html

* added new Logo

* changed image

* added Plan drawing

* fixed checkbox box

* updated buttons

* added font style

* added delete button

* complete app

* added logo

* updatet css

* added shadow to text

* updated bergen

---------

Co-authored-by: bax082024 <bax082024@gmail.com>
  • Loading branch information
Amaliebra and bax082024 authored Oct 14, 2024
1 parent 6b23d7b commit aa56cd3
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/obj
/bin
wwwroot/



14 changes: 10 additions & 4 deletions Services/WeatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public async Task<IActionResult> GetWeatherForBergenAsync()
try
{
// API request
string requestUri = "https://api.open-meteo.com/v1/forecast?latitude=60.393&longitude=5.3242&hourly=temperature_2m,precipitation,rain,showers,weather_code,wind_speed_10m";
string requestUri = "https://api.open-meteo.com/v1/forecast?latitude=60.393&longitude=5.3242&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,rain,showers,snowfall,snow_depth,weather_code,surface_pressure,cloud_cover,cloud_cover_low,cloud_cover_mid,cloud_cover_high,visibility,wind_speed_10m,uv_index,is_day,sunshine_duration&forecast_days=1";

var response = await _httpClient.GetAsync(requestUri);

if (response.IsSuccessStatusCode)
Expand All @@ -31,16 +32,22 @@ public async Task<IActionResult> GetWeatherForBergenAsync()

if (weatherData?.Hourly != null)
{
// Check if the first element in the lists exist and use them, otherwise return "undefined"
// Return only fields that are working
float? temperature = weatherData.Hourly.Temperature2M?.Count > 0 ? weatherData.Hourly.Temperature2M[0] : (float?)null;
float? windspeed = weatherData.Hourly.WindSpeed10M?.Count > 0 ? weatherData.Hourly.WindSpeed10M[0] : (float?)null;
int? weatherCode = weatherData.Hourly.WeatherCode?.Count > 0 ? weatherData.Hourly.WeatherCode[0] : (int?)null;
float? visibility = weatherData.Hourly.Visibility?.Count > 0 ? weatherData.Hourly.Visibility[0] : (float?)null;
string humidity = weatherData.Hourly.RelativeHumidity2M?.Count > 0 ? $"{weatherData.Hourly.RelativeHumidity2M[0]}%" : "undefined";
bool? isDay = weatherData.Hourly.IsDay?.Count > 0 ? (weatherData.Hourly.IsDay[0] == 1) : (bool?)null;

return Ok(new
{
temperature = temperature.HasValue ? $"{temperature}°C" : "undefined",
windspeed = windspeed.HasValue ? $"{windspeed} km/h" : "undefined",
condition = weatherCode.HasValue && weatherCode.Value == 3 ? "Cloudy" : "Clear"
condition = weatherCode.HasValue && weatherCode.Value == 3 ? "Cloudy" : "Clear",
visibility = visibility.HasValue ? $"{visibility} km" : "undefined",
humidity = humidity,
isDay = isDay.HasValue ? (isDay.Value ? "Day" : "Night") : "undefined"
});
}
else
Expand All @@ -61,4 +68,3 @@ public async Task<IActionResult> GetWeatherForBergenAsync()
}
}
}

52 changes: 47 additions & 5 deletions classes/WeatherData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,61 @@
public class WeatherData
{
[JsonPropertyName("hourly")]
public HourlyData Hourly { get; set; }
public HourlyData? Hourly { get; set; }
}

public class HourlyData
{
[JsonPropertyName("temperature_2m")]
public List<float> Temperature2M { get; set; }
public List<float>? Temperature2M { get; set; }

[JsonPropertyName("wind_speed_10m")]
public List<float> WindSpeed10M { get; set; }
public List<float>? WindSpeed10M { get; set; }

[JsonPropertyName("weather_code")]
public List<int> WeatherCode { get; set; }
public List<int>? WeatherCode { get; set; }

// Add other fields like precipitation, rain, etc. if needed
[JsonPropertyName("uv_index")]
public List<float>? UVIndex { get; set; }

[JsonPropertyName("sunshine_duration")]
public List<float>? SunshineDuration { get; set; }

[JsonPropertyName("is_day")]
public List<int>? IsDay { get; set; }

[JsonPropertyName("visibility")]
public List<float>? Visibility { get; set; }

[JsonPropertyName("relative_humidity_2m")]
public List<float>? RelativeHumidity2M { get; set; }

[JsonPropertyName("apparent_temperature")]
public List<float>? ApparentTemperature { get; set; }

[JsonPropertyName("wind_gusts_10m")]
public List<float>? WindGusts10M { get; set; }

[JsonPropertyName("rain")]
public List<float>? Rain { get; set; }

[JsonPropertyName("showers")]
public List<float>? Showers { get; set; }

[JsonPropertyName("surface_pressure")]
public List<float>? SurfacePressure { get; set; }

[JsonPropertyName("cloud_cover")]
public List<float>? CloudCover { get; set; }

[JsonPropertyName("cloud_cover_low")]
public List<float>? CloudCoverLow { get; set; }

[JsonPropertyName("cloud_cover_mid")]
public List<float>? CloudCoverMid { get; set; }

[JsonPropertyName("cloud_cover_high")]
public List<float>? CloudCoverHigh { get; set; }

// Add other fields like snowfall, snow_depth if needed
}
Binary file added documentation/WeatherApp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

**Navigate to files**

|[HTML](/wwwroot/index.html)|[CSS](/wwwroot/styles.css)|[Figma](documentation/Figma-design.pdf)
|[HTML](/wwwroot/index.html)|[CSS](/wwwroot/styles.css)|[Figma](documentation/Figma-design.pdf)[Plan](documentation\WeatherApp.png)
|[Program.cs](/Program.cs)|[WheatherData.cs](/classes/WeatherData.cs)
|[WeatherController.cs](/Controller/WeatherController.cs)|[WheatherService.cs](/Services/WeatherService.cs)
|[WeatherController.cs](/Controller/WeatherController.cs)|[WheatherService.cs](/Services/WeatherService.cs)|
Binary file added wwwroot/backround.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wwwroot/images/BaxAma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wwwroot/images/BaxAma2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wwwroot/images/bergen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 43 additions & 43 deletions wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bax Weather Network</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
<h1 class="logo">Bax Weather Network</h1>
<p class="local-only">Local weather ONLY</p>
</header>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bax Weather Network</title>
<link rel="stylesheet" href="styles.css" />
<script defer src="script.js"></script>


<div class="divider-line"></div>
<main>

</head>
<body>
<header>
<div class="logo">
<img src="images/BaxAma2.png" alt="BaxAma">
</div>
</header>

<main>
<div class="content-wrapper">
<div class="weather-container">
<h1 class="bergen">Bergen</h1>
<p>Current weather in Bergen, Norway.</p>
<p>_____________________________</p>
<hr class="divider">
<p id="temperature">Temperature: Loading...</p>
<p id="windspeed">Windspeed: Loading...</p>
<p id="condition">Condition: Loading...</p>
</div>
</main>

<script>
async function fetchWeather() {
try {
const response = await fetch("/api/weather");
if (!response.ok) {
throw new Error("Failed to fetch weather data");
}
<p id="is-day">Day or Night: Loading...</p>
<p id="visibility">Visibility: Loading...</p>
<p id="humidity">Humidity: Loading...</p>
<p id="windspeed">Windspeed: Loading...</p>

const weatherData = await response.json();
console.log("Received Weather Data: ", weatherData);
<button id="refreshButton">Refresh Weather Data</button>
</div>

// Update the HTML elements with the fetched data
document.getElementById("temperature").innerText = `Temperature: ${weatherData.temperature}`;
document.getElementById("windspeed").innerText = `Windspeed: ${weatherData.windspeed}`;
document.getElementById("condition").innerText = `Condition: ${weatherData.condition}`;

} catch (error) {
console.error("Error: ", error.message);
document.getElementById("temperature").innerText = "Temperature: Error";
document.getElementById("windspeed").innerText = "Windspeed: Error";
document.getElementById("condition").innerText = "Condition: Error";
}
}
<div class="checkbox-container">
<div class="checkbox-list minimized">
<p class="choose-condition">Choose conditions</p>
<label><input type="checkbox" id="toggle-temperature" checked> Temperature</label><br>
<label><input type="checkbox" id="toggle-windspeed" checked> Windspeed</label><br>
<label><input type="checkbox" id="toggle-condition" checked> Condition</label><br>
<label><input type="checkbox" id="toggle-is-day" checked> Day or Night</label><br>
<label><input type="checkbox" id="toggle-visibility" checked> Visibility</label><br>
<label><input type="checkbox" id="toggle-humidity" checked> Humidity</label><br>

// Call the function when the page loads
window.onload = fetchWeather;
</script>
</body>
</html>
<button id="resetButton" onclick="resetSelection()">Delete</button>
</div>

</div>
</div>
</main>
</body>
</html>
89 changes: 89 additions & 0 deletions wwwroot/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Function to uncheck all checkboxes
function resetSelection() {
const checkboxes = document.querySelectorAll('.checkbox-list input[type="checkbox"]');

checkboxes.forEach(checkbox => {
checkbox.checked = false;
});

toggleElements();
}

document.getElementById("resetButton").addEventListener("click", resetSelection);

window.onload = function () {
fetchWeather();
toggleElements();
document.getElementById("refreshButton").addEventListener("click", refreshWeather); // Add the event listener for the refresh button here
};

// Function to toggle visibility of weather elements based on checkbox states
function toggleElements() {
const temperatureEl = document.getElementById("temperature");
const windspeedEl = document.getElementById("windspeed");
const conditionEl = document.getElementById("condition");
const isDayEl = document.getElementById("is-day");
const visibilityEl = document.getElementById("visibility");
const humidityEl = document.getElementById("humidity");

if (temperatureEl && windspeedEl && conditionEl && isDayEl && visibilityEl && humidityEl) {
temperatureEl.style.display = document.getElementById("toggle-temperature").checked ? "block" : "none";
windspeedEl.style.display = document.getElementById("toggle-windspeed").checked ? "block" : "none";
conditionEl.style.display = document.getElementById("toggle-condition").checked ? "block" : "none";
isDayEl.style.display = document.getElementById("toggle-is-day").checked ? "block" : "none";
visibilityEl.style.display = document.getElementById("toggle-visibility").checked ? "block" : "none";
humidityEl.style.display = document.getElementById("toggle-humidity").checked ? "block" : "none";
}
}

document.querySelectorAll('input[type="checkbox"]').forEach((checkbox) => {
checkbox.addEventListener('change', toggleElements);
});

// Fetch weather data function
async function fetchWeather() {
try {
const response = await fetch("/api/weather");
if (!response.ok) {
throw new Error("Failed to fetch weather data");
}

const weatherData = await response.json();
console.log("Received Weather Data: ", weatherData); // Added to check that Api data is received

// Ensure weatherData contains the required fields
if (weatherData) {
// Update the HTML elements with the fetched data
document.getElementById("temperature").innerText = `Temperature: ${weatherData.temperature || 'N/A'}`;
document.getElementById("windspeed").innerText = `Windspeed: ${weatherData.windspeed || 'N/A'}`;
document.getElementById("condition").innerText = `Condition: ${weatherData.condition || 'N/A'}`;
document.getElementById("is-day").innerText = `Day or Night: ${weatherData.isDay || 'N/A'}`;
document.getElementById("visibility").innerText = `Visibility: ${weatherData.visibility || 'N/A'}`;
document.getElementById("humidity").innerText = `Humidity: ${weatherData.humidity || 'N/A'}`;
}
} catch (error) {
console.error("Error fetching weather data: ", error.message);

// Handle errors by showing "Error" in the text fields
document.getElementById("temperature").innerText = "Temperature: Error";
document.getElementById("windspeed").innerText = "Windspeed: Error";
document.getElementById("condition").innerText = "Condition: Error";
document.getElementById("is-day").innerText = "Day or Night: Error";
document.getElementById("visibility").innerText = "Visibility: Error";
document.getElementById("humidity").innerText = "Humidity: Error";
}
}

// Function to refresh weather data
function refreshWeather() {

document.getElementById("temperature").innerText = "Temperature: Loading...";
document.getElementById("windspeed").innerText = "Windspeed: Loading...";
document.getElementById("condition").innerText = "Condition: Loading...";
document.getElementById("is-day").innerText = "Day or Night: Loading...";
document.getElementById("visibility").innerText = "Visibility: Loading...";
document.getElementById("humidity").innerText = "Humidity: Loading...";


fetchWeather();
}
Loading

0 comments on commit aa56cd3

Please sign in to comment.