dataController.js 2.5 KB
Newer Older
1
import fetch from "node-fetch";
2
import { serverMSG, statusCode } from "../serverinfo";
3
import db from "../db/index";
4
5
6
import dotenv from "dotenv";

dotenv.config();
7

8
// 외부 수집기로 부터 들어온 정보 처리
9
const handleOutData = async (locCode, date, lat, lng) => {
10
  // OpenWeatherAPI로 부터 지역의 날씨 정보획득을 위해 지역의 경도와 위도, API Key, 단위 기준 metric 전달
11
  const response = await fetch(
12
    `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${process.env.OPENWEATHERMAP_API_KEY}&units=metric`
13
14
15
16
17
18
19
20
  );
  const json = await response.json();

  const temp = json["main"]["temp"];
  const humi = json["main"]["humidity"];
  const press = json["main"]["pressure"];
  const wind_speed = json["wind"]["speed"];

21
22
23
24
25
26
27
28
29
30
31
32
33
  await db.Weather_out.create(
    {
      loc_code: Number(locCode),
      collected_at: date,
      temp: temp,
      humi: humi,
      press: press,
      wind_speed: wind_speed,
    },
    {
      logging: false,
    }
  );
34
35
36
};

// 내부 수집기로 부터 들어온 정보 처리
37
38
39
40
41
42
43
44
45
46
47
48
49
const handleInData = async (id, date, temp, humi, lights) => {
  db.Weather_in.create(
    {
      host: id,
      collected_at: date,
      temp: temp,
      humi: humi,
      lights: lights,
    },
    {
      logging: false,
    }
  );
50
51
52
};

// 데이터 수신 처리
53
export const getDataInput = (req, res) => {
54
  try {
55
    if (req.query.type === "OUT") {
56
57
      // 외부 데이터 수집기
      const {
58
        query: { locCode, date, lat, lng },
59
      } = req;
60

61
62
      console.log(locCode, date, lat, lng);
      // handleOutData(locCode, date, lat, lng);
63
64
65
    } else {
      // 내부 데이터 수집기 동작
      const {
66
        query: { id, locCode, date, temp, humi, lights },
67
68
      } = req;

69
      console.log(id, locCode, date, temp, humi, lights);
70
      // handleInData(id, date, temp, humi, lights);
71
72
    }

73
    res.status(statusCode.ok).send(serverMSG.server_ok);
74
75
  } catch (error) {
    console.log(error);
76
    res.status(statusCode.err).send(serverMSG.server_err);
77
  }
KangMin An's avatar
KangMin An committed
78
};
79
80

// 사용자의 데이터 가져오기 및 예측 값 전송
81
export const getUserWeatherData = (req, res) => {
82
  const {
83
    params: { email },
84
85
  } = req;

86
87
88
89
90
91
92
93
94
  /* 사용자 email에 따른 사용자 날씨 데이터 가져오기 */

  res.status(statusCode.ok).send(serverMSG.server_ok);
};

// 지역 코드 요청 처리
export const getLocCode = (req, res) => {
  /* 통합 지역 코드 및 이름 json으로 생성 및 전송 */

95
96
  res.status(statusCode.ok).send(serverMSG.server_ok);
};