diff --git a/server/package.json b/server/package.json index 6467f04e0ea014232b929a0585935e7c420410bf..e8083ebb3bd635478e7fb703d123c4dc5051c37a 100644 --- a/server/package.json +++ b/server/package.json @@ -26,6 +26,7 @@ "express": "^4.17.1", "helmet": "^4.1.1", "morgan": "^1.10.0", + "node-fetch": "^2.6.1", "pug": "^3.0.0" }, "devDependencies": { diff --git a/server/src/controllers/dataController.js b/server/src/controllers/dataController.js index 6d3b4ca88cf89fdc684df3b79d3845a1f393090f..65a1e1c0227448cbe02a1def564197038bf4f8bf 100644 --- a/server/src/controllers/dataController.js +++ b/server/src/controllers/dataController.js @@ -1,6 +1,32 @@ import routes from "../routes"; +import fetch from "node-fetch"; export const getDataInput = (req, res) => { console.log(req.query); + if (req.query.type === "Out") { + // 외부 데이터 수집기 + const { + query: { id, type, lat, lng }, + } = req; + // OpenWeatherAPI로 부터 지역의 날씨 정보획득 + fetch( + `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${process.env.OPENWEATHERMAP_API_KEY}` + ) + .then((response) => response.json()) + .then((json) => { + const temp = json.main.temp; + const humi = json.main.humidity; + const press = json.main.pressure; + const wind_speed = json.wind.speed; + console.log(id, type, lat, lng, temp, humi, press, wind_speed); + }); + } else { + // 내부 데이터 수집기 동작 + const { + query: { id, type, temp, humi, lights }, + } = req; + console.log(id, type, temp, humi, lights); + } + res.status(200).send("

OK

"); };