-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
128 lines (96 loc) · 3.39 KB
/
server.js
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
// Air quality index
let apikey=config.API_KEY;
// Fetch air quality index
const fetchaqi = async (city,state,country) =>{
await fetch("https://api.airvisual.com/v2/city?city="+city+"&state="+state+"&country="+country+"&key="+apikey)
.then((res) => res.json())
.then((res) => display(res.data));
}
// display data
const display = (data) =>{
const {aqius}=data.current.pollution;
console.log(aqius);
document.querySelector('.aq').innerHTML=aqius;
if (0<=aqius && aqius<=50)
{
document.getElementById('display').style.backgroundColor="green";
document.querySelector('.level').innerHTML="Good";
}
else if(51<=aqius && aqius<=100)
{
document.getElementById('display').style.backgroundColor="yellow";
document.querySelector('.level').innerHTML="Moderate";
}
else if(101<=aqius && aqius<=150)
{
document.getElementById('display').style.backgroundColor="orange";
document.querySelector('.level').innerHTML="Unhealthy and Sensitive Groups";
}
else if(151<=aqius && aqius<=200)
{
document.getElementById('display').style.backgroundColor="red";
document.querySelector('.level').innerHTML="Unhealthy";
}
else if(201<=aqius && aqius<=300)
{
document.getElementById('.display').style.backgroundColor="purple";
document.querySelector('.level').innerHTML="Very Unhealthy";
}
else if(aqius>=301)
{
document.getElementById('.display').style.backgroundColor="maroon";
document.querySelector('.level').innerHTML="Hazardous";
}
}
// search data
const search = () =>{
fetchaqi(document.querySelector(".city").value,document.querySelector(".state").value,document.querySelector(".country").value);
}
document.querySelector('.search-btn').addEventListener("click",function (){
document.querySelector('.aq').style.display="block";
search();
})
// Major Pollutants
var latval;
var lonval;
var apiid=config.API_ID;
// getting user location
const getUserLocation =()=>{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onPositionGather,onPositionError);
} else {
onPositionError({message:"Can't access location."})
}
}
const onPositionGather = (pos)=>{
let lat=pos.coords.latitude.toFixed(4);
let lon=pos.coords.longitude.toFixed(4);
latval=lat;
lonval=lon;
getAirquality(lat,lon);
}
const getAirquality = async (lat,lon)=>{
await fetch("https://api.openweathermap.org/data/2.5/air_pollution/forecast?lat="+lat+"&lon="+lon+"&appid="+apiid)
.then((res)=>res.json())
.then((data)=>setvalue(data))
}
const setvalue = (data)=>{
const {co}=data.list[0].components;
const {nh3}=data.list[0].components;
const {no}=data.list[0].components;
const {no2}=data.list[0].components;
const {o3}=data.list[0].components;
const {pm2_5}=data.list[0].components;
const {pm10}=data.list[0].components;
const {so2}=data.list[0].components;
document.querySelector('.o3').innerHTML=o3+" μg/mᶟ";
document.querySelector('.co').innerHTML=co+" μg/mᶟ";
document.querySelector('.no2').innerHTML=no2+" μg/mᶟ";
document.querySelector('.pm2').innerHTML=pm2_5+" μg/mᶟ";
document.querySelector('.so2').innerHTML=so2+" μg/mᶟ";
}
const onPositionError = (e)=>{
errorLabel.innerHTML=e.message
}
getUserLocation();