From 937e73fbd027d09db4f9f4cc7af64ad4040c71ed Mon Sep 17 00:00:00 2001 From: Kangmin An Date: Mon, 1 Mar 2021 20:25:52 +0900 Subject: [PATCH] Arduino DataSending & Server Receiving & Get API. --- server/package.json | 1 + server/src/controllers/dataController.js | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/server/package.json b/server/package.json index 6467f04..e8083eb 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 6d3b4ca..65a1e1c 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

"); }; -- GitLab