권주희

implement get currnet weather infromation api

- implement get currnet weather infromation api
- connect the api with frontend
......@@ -4,6 +4,7 @@ var axios = require("axios");
const openAPIKey = require("./secrets.json").openAPIKey;
const googleMapKey = require("./secrets.json").googleAPIKey;
const weatherAPIKey = require("./secrets.json").weatherAPIKey;
axios.create({
// TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
......@@ -25,6 +26,39 @@ router.get("/", async function (req, res, next) {
res.send(airCondition);
});
router.get("/weather", async function (req, res, next) {
console.log("경도:", req.query.latitude);
console.log("경도:", req.query.longitude);
let airCondition = "";
let response = await getEnglishPosition(
req.query.latitude,
req.query.longitude
)
.then((encodedStation) => getWeather(encodedStation))
.then((result) => {
airCondition = result;
});
res.send(airCondition);
});
const getWeather = (encodedStation) => {
return axios
.get(
"https://api.openweathermap.org/data/2.5/weather?q=" +
encodedStation +
"&appid=" +
weatherAPIKey
)
.then(function (response) {
return response["data"];
})
.catch(function (error) {
console.log(error.response);
});
};
const getPosition = (lat, lon) => {
return axios
.get(
......@@ -58,6 +92,29 @@ const getPosition = (lat, lon) => {
console.log(error.response);
});
};
const getEnglishPosition = (lat, lon) => {
return axios
.get(
"https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
lat +
"," +
lon +
"&location_type=ROOFTOP&result_type=street_address&key=" +
googleMapKey +
"&language=en"
)
.then(function (response) {
let stationName =
response["data"].results[0]["address_components"][3]["long_name"];
console.log("STATION : ", stationName);
return (encodedStation = encodeURI(stationName));
})
.catch(function (error) {
console.log(error.response);
});
};
/* GET route airCondition listing. */
router.get("/route", async function (req, res, next) {
console.log("출발지:", req.query.departure);
......
......@@ -31,6 +31,12 @@ export default class Home extends Component {
region: "은평구청",
curAirCondition: null,
routeInformation: null,
temperature: null,
humidity: null,
weather: null,
icon: null,
wind: null,
cloud: null,
};
}
......@@ -303,6 +309,35 @@ export default class Home extends Component {
.finally(function () {
// always executed
});
API.get("airCondition/weather", {
params: {
latitude: position["Ha"],
longitude: position["Ga"],
},
})
.then((response) => {
let resp = response["data"];
console.log(resp);
console.log("현재온도 : " + (resp.main.temp - 273.15));
console.log("현재습도 : " + resp.main.humidity);
console.log("날씨 : " + resp.weather[0].main);
console.log("상세날씨설명 : " + resp.weather[0].description);
console.log("날씨 이미지 : " + resp.weather[0].icon);
console.log("바람 : " + resp.wind.speed);
console.log("구름 : " + resp.clouds.all + "%");
this.setState({
temperature: (resp.main.temp - 273.15).toFixed(3),
humidity: resp.main.humidity,
weather: resp.weather[0].main,
icon: resp.weather[0].icon,
wind: resp.wind.speed,
cloud: resp.clouds.all + "%",
});
})
.catch(function (error) {
console.log(error);
});
};
let setDepart = document.createElement("Button");
setDepart.innerHTML = "출발지로 설정하기";
......@@ -400,7 +435,12 @@ export default class Home extends Component {
<br /> 미세먼지 등급 <br /> {pm10Image} <br />
미세먼지 지수 : {this.state.curAirCondition.pm10Value} <br />
초미세먼지 등급 <br /> {pm25Image} <br />
초미세먼지 지수 : {this.state.curAirCondition.pm25Value} <br />{" "}
초미세먼지 지수 : {this.state.curAirCondition.pm25Value} <br />
현재 온도 : {this.state.temperature} <br />
현재 습도 : {this.state.humidity} <br />
날씨 : {this.state.weather} <br />
바람 : {this.state.wind} <br />
구름 : {this.state.cloud} <br />{" "}
</h5>
);
}
......