-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile.js
More file actions
49 lines (42 loc) · 3.53 KB
/
Copy pathmobile.js
File metadata and controls
49 lines (42 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const ambientApiUrl = 'https://api.ambientweather.net/v1/devices?applicationKey=40b33f6a63754b5fb70a4d5fe557c64efcdd693597924c21986b47e71e1e68eb&apiKey=c5cc20bfdc0446aaaddd4543eb04c64c4852dcd72d1f4d5d8c7f207c1d21036a'; // Updated Ambient Weather API endpoint
async function fetchWeatherData() {
try {
// Fetch weather data from Ambient Weather API
const ambientResponse = await fetch(ambientApiUrl); // Updated fetch URL without location
if (!ambientResponse.ok) {
throw new Error('Network response was not ok');
}
const ambientData = await ambientResponse.json(); // Fixed variable name from ambkentData to ambientData
// Check if ambientData is defined and has entries
if (!ambientData || ambientData.length === 0) {
throw new Error('No weather data available');
}
updateWeatherUI(ambientData);
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
function updateWeatherUI(ambientData) {
const currentWeather = ambientData[0].lastData; // Accessing lastData for current weather
const locationInfo = ambientData[0].info; // Accessing location info
const useMetric = localStorage.getItem('useMetric') === 'true';
document.querySelector('#current-temp').textContent = `${currentWeather.tempf !== undefined ? (useMetric ? fahrenheitToCelsius(currentWeather.tempf) : currentWeather.tempf).toFixed(1) : 'N/A'}${useMetric ? '°C' : '°F'}`;
document.querySelector('#feels-like').textContent = `${currentWeather.feelsLike !== undefined ? (useMetric ? fahrenheitToCelsius(currentWeather.feelsLike) : currentWeather.feelsLike).toFixed(1) : 'N/A'}${useMetric ? '°C' : '°F'}`;
document.querySelector('#humidity').textContent = `${currentWeather.humidity !== undefined ? currentWeather.humidity : 'N/A'}%`;
document.querySelector('#wind').textContent = `${currentWeather.windspeedmph !== undefined ? (useMetric ? mphToKmh(currentWeather.windspeedmph) : currentWeather.windspeedmph).toFixed(1) : 'N/A'} ${useMetric ? 'km/h' : 'mph'}`;
document.querySelector('#pressure').textContent = `${currentWeather.baromrelin !== undefined ? (useMetric ? inHgToHpa(currentWeather.baromrelin) : currentWeather.baromrelin).toFixed(2) : 'N/A'} ${useMetric ? 'hPa' : 'inHg'}`;
document.querySelector('#dew-point').textContent = `${currentWeather.dewPoint !== undefined ? (useMetric ? fahrenheitToCelsius(currentWeather.dewPoint) : currentWeather.dewPoint).toFixed(1) : 'N/A'}${useMetric ? '°C' : '°F'}`;
document.querySelector('#rain-today').textContent = `${currentWeather.dailyrainin !== undefined ? (useMetric ? inchesToMm(currentWeather.dailyrainin) : currentWeather.dailyrainin).toFixed(2) : 'N/A'} ${useMetric ? 'mm' : 'in'}`;
document.querySelector('#weather-icon').innerHTML = `<img src="${currentWeather.icon || './NA.jpg'}" alt="${currentWeather.weather || 'No weather data'}">`;
document.querySelector('#last-update').textContent = `Last updated: ${new Date(currentWeather.dateutc).toLocaleString() || 'Unknown time'}`;
}
// Ensure the DOM is fully loaded before adding event listeners
document.addEventListener('DOMContentLoaded', () => {
// Event listener for reload button
const reloadButton = document.getElementById('reload-weather');
if (reloadButton) {
reloadButton.addEventListener('click', fetchWeatherData); // Call fetchWeatherData directly
}
// Initial fetch
fetchWeatherData(); // Call fetchWeatherData directly
});