dataController.js 3.34 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
  /* 사용자 email에 따른 사용자 날씨 데이터 가져오기 */

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

// 지역 코드 요청 처리
92
export const getLocCode = async (req, res) => {
93
  /* 통합 지역 코드 및 이름 json으로 생성 및 전송 */
94
  let locCodes = [];
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
124
125
126
127
128
129
130
131
132
  const does = await db.Doe.findAll({ logging: false });
  const sggs = await db.Sgg.findAll({ logging: false });
  const emds = await db.Emd.findAll({ logging: false });

  let doe_sgg = [];
  let sgg_emd = [];

  does.map((info_doe) => {
    let temp = {
      name_doe: info_doe["name_doe"],
      code_doe: info_doe["code_doe"],
    };
    temp.sgg = sggs.filter(
      (info_sgg) => info_sgg["code_doe"] === info_doe["code_doe"]
    );
    doe_sgg.push(temp);
  });

  sggs.map((info_sgg) => {
    let temp = {
      code_doe: info_sgg["code_doe"],
      name_sgg: info_sgg["name_sgg"],
      code_sgg: info_sgg["code_sgg"],
    };
    temp.emd = emds.filter(
      (info_emd) => info_emd["code_sgg"] === info_sgg["code_sgg"]
    );
    sgg_emd.push(temp);
  });

  res.status(statusCode.ok).json({
    locCodes: {
      DOE: does,
      SGG: doe_sgg,
      EMD: sgg_emd,
    },
  });
133
};