weather.js
3.13 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
const axios = require("axios").default;
const getWeather = async (user) => {
if (!user) {
return;
}
try {
const response = await axios.post("http://localhost:8080/api/weather");
const data = response.data;
const formattedData = {
meta: {
country: data.sys.country,
city: data.name,
timezone: data.timezone / 60 / 60,
sunrise: data.sys.sunrise,
sunset: data.sys.sunset,
},
description: data.weather[0].description,
temp: {
realCelcius: (data.main.temp - 273.15).toFixed(2),
feelCelcius: (data.main.feels_like - 273.15).toFixed(2),
realFer: ((data.main.temp - 273.15) * 9) / 5 + 32,
feelFer: ((data.main.feels_like - 273.15) * 9) / 5 + 32,
},
types: {
wind: data.wind.speed,
clouds: data.clouds.all,
rain: data.rain ? data.rain : null,
snow: data.snow ? data.snow : null,
},
};
return formattedData;
} catch (err) {
console.log(err);
}
};
const getForecaset = async (user) => {
if (!user) {
return;
}
try {
const response = await axios.post(
"http://localhost:8080/api/weather/forecast"
);
const data = response.data;
const forecast = [];
for (let i = 0; i < data.list.length; i++) {
const singleData = {};
let singleItem = data.list[i];
singleData["dateTime"] = singleItem.dt_txt;
singleData["description"] = singleItem.weather[0].description;
singleData["temp"] = {
realCelcius: (singleItem.main.temp - 273.15).toFixed(2),
feelCelcius: (singleItem.main.feels_like - 273.15).toFixed(2),
realFer: ((singleItem.main.temp - 273.15) * 9) / 5 + 32,
feelFer: ((singleItem.main.feels_like - 273.15) * 9) / 5 + 32,
};
singleData["types"] = {
wind: singleItem.wind.speed,
clouds: singleItem.clouds.all,
};
forecast.push(singleData);
}
const formattedData = {
meta: {
country: data.city.country,
city: data.city.name,
timezone: data.city.timezone / 60 / 60,
sunrise: data.city.sunrise,
sunset: data.city.sunset,
},
forecast,
};
return formattedData;
} catch (err) {
console.log(err);
}
};
const getAirPollution = async (user) => {
if (!user) {
return;
}
try {
const response = await axios.post(
"http://localhost:8080/api/weather/airpollution"
);
const dataObject = response.data;
const airData = dataObject.airPollutionData.list[0].components;
const formattedData = {
meta: {
country: dataObject.country,
state: dataObject.state ? dataObject.state : null,
coordinates: dataObject.airPollutionData.coord,
},
airData: {
co: airData.co,
nh3: airData.nh3,
no: airData.no,
no2: airData.no2,
o3: airData.o3,
pm2_5: airData.pm2_5,
pm10: airData.pm10,
so2: airData.so2,
},
};
return formattedData;
} catch (err) {
console.log(err);
}
};
const weatherService = {
getWeather,
getForecaset,
getAirPollution,
};
export default weatherService;