곽태식

예보 api 추가 완료

const db = require('./db.js');
const secret_key = require('../keys/api_option').weather_key;
//const traffic_key = require('../keys/api_option').traffic_key;
const traffic_key = require('../keys/api_option').traffic_key;
const requesting = require('request');
const lat = "37.239795";
const lon = "127.083240";
......@@ -56,27 +56,27 @@ module.exports = (server, app) => {
}
// 교통사고정보를 받아오는 함수
// const req_traffic_API = (year) => {
const req_traffic_API = (year) => {
//async await 사용하기 위하여 promise 사용
// return new Promise((resolve, reject) => {
// requesting.get({
// // api를 요청할 주소 -- 시크릿키,위도,경도 입력
// url: `http://taas.koroad.or.kr/data/rest/accident/death?authKey=${traffic_key}&searchYear=${year}&siDo=${city}&guGun=${town}&type=json`,
// json: true
// },
// //api에게 응답 받았을때 실행되는 callback function
// function (err, api_res, api_body) {
// //err 존재시 promise reject 호출
// if (err) reject(err);
//
// // api의 response이 있을경우 promise resolve 호출
// if (api_res) {
// console.log("calling traffic api");
// resolve(api_body);
// }
// });
// })
// }
return new Promise((resolve, reject) => {
requesting.get({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
url: `http://taas.koroad.or.kr/data/rest/accident/death?authKey=${traffic_key}&searchYear=${year}&siDo=${city}&guGun=${town}&type=json`,
json: true
},
//api에게 응답 받았을때 실행되는 callback function
function (err, api_res, api_body) {
//err 존재시 promise reject 호출
if (err) reject(err);
// api의 response이 있을경우 promise resolve 호출
if (api_res) {
console.log("calling traffic api");
resolve(api_body);
}
});
})
}
const API_bundle = async () => {
try {
......@@ -89,8 +89,8 @@ module.exports = (server, app) => {
Day3_Weather = await req_API("forecast","3days"); //3일예보(단기예보)
// 사용자의 생일에 따라서 다른 년도의 교통사고정보를 가져옴.
// const year = year_start + (client_birth % (year_range+1));
// Traffic_Accident = await req_traffic_API(year); //교통사고정보
const year = year_start + (client_birth % (year_range+1));
Traffic_Accident = await req_traffic_API(year); //교통사고정보
info = {
......@@ -107,22 +107,40 @@ module.exports = (server, app) => {
typhoon: Current_Weather.common.stormYn, //현재 태풍
time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각
Forecast_64W: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd61hour,
Forecast_25W: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd25hour,
Forecast_46W: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd46hour,
Forecast_1dayHeat: Heat_index.weather.wIndex.heatIndex[0].forecast.index25hour, //열지수 예보
Forecast_2dayHeat: Heat_index.weather.wIndex.heatIndex[0].forecast.index49hour,
Forecast_laterHeat: Heat_index.weather.wIndex.heatIndex[0].forecast.index52hour,
Forecast_1daySensible: Sensible_T.weather.wIndex.wctIndex[0].forecast.index25hour,// 체감온도 예보
Forecast_2daySensible: Sensible_T.weather.wIndex.wctIndex[0].forecast.index49hour,
Forecast_laterSensible: Sensible_T.weather.wIndex.wctIndex[0].forecast.index52hour,
Forecast_25R: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob25hour,
Forecast_46R: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob46hour,
Forecast_64R: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob64hour,
Forecast_1dayDiscomport: Discomport_index.weather.wIndex.thIndex[0].forecast.index25hour, //불쾌지수
Forecast_2dayDiscomport: Discomport_index.weather.wIndex.thIndex[0].forecast.index49hour,
Forecast_laterDiscomport: Discomport_index.weather.wIndex.thIndex[0].forecast.index52hour,
Forecast_warning: Day3_Weather.common.alertYn, //특보 예보
Forecast_typhoon: Day3_Weather.common.stormYn, //태풍 예보
Forecast_1dayW: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd25hour,//바람속도 예보
Forecast_2dayW: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd49hour,
Forecast_laterW: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd52hour,
Forecast_1dayR: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob25hour, //강수량 예보
Forecast_2dayR: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob49hour,
Forecast_laterR: Day3_Weather.weather.forecast3days[0].fcst3hour.precipitation.prob52hour,
Forecast_25S: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code25hour,
Forecast_46S: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code46hour,
Forecast_64S: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code64hour,
// traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수
Forecast_1dayS: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code25hour, //하늘상태 예보
Forecast_2dayS: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code46hour,
Forecast_laterS: Day3_Weather.weather.forecast3days[0].fcst3hour.sky.code64hour,
traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수
death_prob: 0 //확률
}
console.log("API INFO \n", info);
// console.log("Traffic count:", info.traffic);
console.log("Traffic count:", info.traffic);
......