김주희

비동기 함수 순서 수정

......@@ -4,8 +4,8 @@ const secret_key_traffic = require('../keys/api_option_traffic').key;
const requesting = require('request');
const lat = "37.54922931758";//"37.239795"; //weather 위도
const lon = "126.913415969472";//"127.083240"; //weather 경도
const lat = "37.54922931758"; //weather 위도
const lon = "126.913415969472"; //weather 경도
const sido_num = "11"; //traffic 법정동 시도 코드
const gugun_num = "440"; //traffic 법정동 시군구 코드 - 합정동 일대
const search_year = "2018074"; //traffic 검색을 원하는 연도
......@@ -42,17 +42,18 @@ module.exports = (server, app) => {
json: true
},
//api에게 응답 받았을때 실행되는 callback function
function (err, api_res, api_body) {
function (err1, api_res1, api_body1) {
//err 존재시 promise reject 호출
if (err) reject(err);
if (err1) reject(err1);
// api의 response이 있을경우 promise resolve 호출
if (api_res) {
if (api_res1) {
console.log("calling weather api");
resolve(api_body);
resolve(api_body1);
}
});
})
}) //Promise
}
......@@ -63,12 +64,6 @@ module.exports = (server, app) => {
requesting.get({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
url: `http://apis.data.go.kr/B552061/${cate1}/${cate2}?ServiceKey=${secret_key_traffic}&searchYearCd=${search_year}&siDo=${sido_num}&guGun=${gugun_num}&type=json&numOfRows=1&pageNo=1`,
//나중에 삭제 http://apis.data.go.kr/B552061/frequentzoneLg/getRestFrequentzoneLg?ServiceKey=XOLqI9tY4gVCdk1i2BoOM3EYmSUYqt8WlXUfIDSfD4RnNBrFwwthbNAtGOxoskP540JbUAzcnuABsydoE2U2dA%3D%3D&searchYearCd=2017&siDo=11&guGun=200&type=json&numOfRows=10&pageNo=1
//나중에 삭제 http://apis.data.go.kr/B552061/frequentzoneLg/getRestFrequentzoneLg?ServiceKey=XOLqI9tY4gVCdk1i2BoOM3EYmSUYqt8WlXUfIDSfD4RnNBrFwwthbNAtGOxoskP540JbUAzcnuABsydoE2U2dA%3D%3D&searchYearCd=2018074&siDo=11&guGun=440&type=json&numOfRows=10&pageNo=1
//sido = 11 gugun = 440 (마포구 합정동, 도화동, 도화동)
//cate1 = frequentzoneLg
//cate2 = getRestFrequentzoneLg
//xml: true
json: true
},
//api에게 응답 받았을때 실행되는 callback function
......@@ -82,20 +77,26 @@ module.exports = (server, app) => {
resolve(api_body);
}
});
})
}) //Promise
}
const API_bundle = async () => {
try {
var order = 0;
while (order < 2) {
switch (order) {
case 0:
console.log("APIWait - WAITING FOR ALL API");
Current_Weather = await req_API("current", "minutely"); //현재날씨 (분별) //when, what
Sensible_T = await req_API("index", "wct"); //체감온도
Heat_index = await req_API("index", "heat"); //열지수
Discomport_index = await req_API("index", "th"); //불쾌지수
Ultra_Violet_index = await req_API("index", "uv"); //자외선지수
Current_TrafficAcc = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); //cate1, cate2
order = 1;
break;
case 1:
console.log("APIDataProcess - CALLING INFO");
info = {
heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수
sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도
......@@ -180,6 +181,13 @@ module.exports = (server, app) => {
db_total.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death, client_send.trafficdeath, client_send.mhurt, client_send.lhurt, client_send.occurence], (err, result) => {
if (err) console.log(err);
})
order = 2;
break;
default:
order = 3;
break;
}//switch
}; //while
} catch (err) { //promise err or try err catch
console.log("================Error Occured !!================\n", err);
......