-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
161 lines (126 loc) · 4.94 KB
/
Copy pathapp.js
File metadata and controls
161 lines (126 loc) · 4.94 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* ===================
API KEYS
=================== */
const WEATHER_KEY = "585a33a12f169018b1cd5f2e8e6e6aea";
const TOMTOM_KEY = "RcbWRRwjfiSofAi8ScuEi0mhKgshA9zw";
let useCelsius = true;
/* HELPERS */
const $ = s => document.querySelector(s);
function safeFetch(url, timeout=9000){
return new Promise((resolve, reject)=>{
const timer = setTimeout(()=>reject(new Error("timeout")), timeout);
fetch(url)
.then(r=>{
clearTimeout(timer);
if(!r.ok) return r.text().then(t=>reject(new Error(r.status+" "+t)));
return r.json().then(resolve);
})
.catch(err=>{ clearTimeout(timer); reject(err); });
});
}
function showLoading(){
$("#weatherCity").textContent = "Loading...";
$("#tempValue").textContent = "--°";
}
/* FORMATTERS */
function formatTemp(t){
return useCelsius ? Math.round(t)+"°C" : Math.round(t*9/5+32)+"°F";
}
function aqiLabel(pm){
if(pm<=30) return {label:"Good", color:"#16a34a"};
if(pm<=80) return {label:"Moderate", color:"#f59e0b"};
if(pm<=150) return {label:"Unhealthy", color:"#f97316"};
return {label:"Very Unhealthy", color:"#ef4444"};
}
/* API WRAPPERS */
const getWeather = city => safeFetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${WEATHER_KEY}&units=metric`);
const getForecast = city => safeFetch(`https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${WEATHER_KEY}&units=metric`);
const getCoordinates = city => safeFetch(`https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=${WEATHER_KEY}`);
const reverseGeo = (lat,lon) => safeFetch(`https://api.openweathermap.org/geo/1.0/reverse?lat=${lat}&lon=${lon}&limit=1&appid=${WEATHER_KEY}`);
const getAQI = (lat,lon) => safeFetch(`https://api.openweathermap.org/data/2.5/air_pollution?lat=${lat}&lon=${lon}&appid=${WEATHER_KEY}`);
const getTraffic = (lat,lon) => safeFetch(`https://api.tomtom.com/traffic/services/4/flowSegmentData/absolute/10/json?key=${TOMTOM_KEY}&point=${lat},${lon}`);
/* UI UPDATES */
function updateWeatherUI(data){
$("#weatherCity").textContent = `${data.name}, ${data.sys.country}`;
$("#weatherSummary").textContent = data.weather[0].description;
$("#tempValue").textContent = formatTemp(data.main.temp);
$("#tempRange").textContent = `H: ${formatTemp(data.main.temp_max)} L: ${formatTemp(data.main.temp_min)}`;
$("#humidityVal").textContent = data.main.humidity + "%";
$("#windVal").textContent = data.wind.speed + " m/s";
$("#pressVal").textContent = data.main.pressure + " hPa";
const icon = data.weather[0].icon;
$("#weatherIcon").innerHTML = `<img src="https://openweathermap.org/img/wn/${icon}@2x.png" style="width:48px">`;
}
function updateAQIUI(pm){
$("#pm25Val").textContent = pm;
const s = aqiLabel(pm);
$("#aqiStatus").textContent = s.label;
$("#aqiStatus").style.color = s.color;
}
/* MAIN DATA FETCHER */
async function fetchCityData(city){
if(!city) return alert("Enter a city");
showLoading();
if(!tempChart) initCharts();
try{
const coords = await getCoordinates(city).then(r=>({lat:r[0].lat, lon:r[0].lon}));
const [weather, forecast, aqi] = await Promise.all([
getWeather(city),
getForecast(city),
getAQI(coords.lat, coords.lon)
]);
// Update UI
updateWeatherUI(weather);
updateAQIUI(aqi.list[0].components.pm2_5);
// Fill forecast charts
const list = forecast.list.slice(0, 6);
list.forEach((d, i)=>{
const label = i===0 ? "Now" : `+${i*3}h`;
labels.push(label);
tempData.push(d.main.temp);
humidityData.push(d.main.humidity);
rainData.push((d.rain && (d.rain["3h"]||d.rain["1h"])) || 0);
windData.push(d.wind.speed);
pmData.push(aqi.list[0].components.pm2_5);
trimData();
});
updateCharts();
renderForecastChart(
list.map((d,i)=>({t:d.main.temp, label:i===0?"Now":`+${i*3}h`}))
);
renderBarChart(weather.main.temp, aqi.list[0].components.pm2_5);
$("#refreshBtn").textContent =
"Last refresh: " + new Date().toLocaleTimeString();
} catch(err){
alert("Error: " + err.message);
}
}
/* EVENTS */
$("#searchBtn").onclick = () => {
const city = $("#cityInput").value || $("#citySelect").value;
fetchCityData(city);
};
$("#citySelect").onchange = function(){
$("#cityInput").value = this.value;
};
$("#unitsToggle").onclick = () => {
useCelsius = !useCelsius;
const city = $("#cityInput").value || $("#citySelect").value;
if(city) fetchCityData(city);
};
$("#refreshBtn").onclick = () => {
const city = $("#cityInput").value || "Bengaluru";
fetchCityData(city);
};
$("#geoBtn").onclick = () => {
navigator.geolocation.getCurrentPosition(async pos=>{
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
const cityRes = await reverseGeo(lat,lon);
const name = cityRes[0].name;
$("#cityInput").value = name;
fetchCityData(name);
}, err=>alert(err.message));
};
// Auto-load Bengaluru
fetchCityData("Bengaluru");