Showing
1 changed file
with
34 additions
and
2 deletions
1 | const db = require('./db.js'); | 1 | const db = require('./db.js'); |
2 | const secret_key = require('../keys/api_option').key; | 2 | const secret_key = require('../keys/api_option').key; |
3 | +const secret_D_key = require('../keys/api_option').D_key;//사망교통사고 키 | ||
3 | const requesting = require('request'); | 4 | const requesting = require('request'); |
4 | const lat = "37.239795"; | 5 | const lat = "37.239795"; |
5 | const lon = "127.083240"; | 6 | const lon = "127.083240"; |
... | @@ -23,6 +24,9 @@ module.exports = (server, app) => { | ... | @@ -23,6 +24,9 @@ module.exports = (server, app) => { |
23 | 24 | ||
24 | let info = {} | 25 | let info = {} |
25 | 26 | ||
27 | + | ||
28 | + | ||
29 | + | ||
26 | const req_API = (when, what) => { | 30 | const req_API = (when, what) => { |
27 | //async await 사용하기 위하여 promise 사용 | 31 | //async await 사용하기 위하여 promise 사용 |
28 | return new Promise((resolve, reject) => { | 32 | return new Promise((resolve, reject) => { |
... | @@ -44,6 +48,30 @@ module.exports = (server, app) => { | ... | @@ -44,6 +48,30 @@ module.exports = (server, app) => { |
44 | }); | 48 | }); |
45 | }) | 49 | }) |
46 | } | 50 | } |
51 | + | ||
52 | + const req_D_API = () =>{ | ||
53 | + | ||
54 | + return new Promise((resolve, reject) => { | ||
55 | + requesting.get({ | ||
56 | + // api를 요청할 주소 -- 시크릿키 입력, 경기도 수원시 설정 | ||
57 | + url: `http://taas.koroad.or.kr/data/rest/accident/death?authKey=${secret_D_key}searchYear=2018&sido=1300&guGun=1302&type=json`, | ||
58 | + json: true | ||
59 | + }, | ||
60 | + //api에게 응답 받았을때 실행되는 callback function | ||
61 | + function (err, api_res, api_body) { | ||
62 | + //err 존재시 promise reject 호출 | ||
63 | + if (err) reject(err); | ||
64 | + | ||
65 | + // api의 response이 있을경우 promise resolve 호출 | ||
66 | + if (api_res) { | ||
67 | + console.log("calling api"); | ||
68 | + resolve(api_body); | ||
69 | + } | ||
70 | + }); | ||
71 | + }) | ||
72 | + } | ||
73 | + | ||
74 | + | ||
47 | const API_bundle = async () => { | 75 | const API_bundle = async () => { |
48 | 76 | ||
49 | try { | 77 | try { |
... | @@ -52,7 +80,8 @@ module.exports = (server, app) => { | ... | @@ -52,7 +80,8 @@ module.exports = (server, app) => { |
52 | Heat_index = await req_API("index", "heat"); //열지수 | 80 | Heat_index = await req_API("index", "heat"); //열지수 |
53 | Discomport_index = await req_API("index", "th"); //불쾌지수 | 81 | Discomport_index = await req_API("index", "th"); //불쾌지수 |
54 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 | 82 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 |
55 | - | 83 | + let kts = await req_D_API(); |
84 | + | ||
56 | info = { | 85 | info = { |
57 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 | 86 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 |
58 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 | 87 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 |
... | @@ -66,10 +95,13 @@ module.exports = (server, app) => { | ... | @@ -66,10 +95,13 @@ module.exports = (server, app) => { |
66 | warning: Current_Weather.common.alertYn, //현재 특보 유무 | 95 | warning: Current_Weather.common.alertYn, //현재 특보 유무 |
67 | typhoon: Current_Weather.common.stormYn, //현재 태풍 | 96 | typhoon: Current_Weather.common.stormYn, //현재 태풍 |
68 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 | 97 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 |
69 | - death_prob: 0 //확률 | 98 | + death_prob: 0, //확률 |
99 | + | ||
70 | } | 100 | } |
71 | console.log("API INFO \n", info); | 101 | console.log("API INFO \n", info); |
72 | 102 | ||
103 | + | ||
104 | + | ||
73 | // ------------------------------ death_prob 정의 ------------------------------ | 105 | // ------------------------------ death_prob 정의 ------------------------------ |
74 | 106 | ||
75 | info.death_prob += info.sky.substr(5) * 1 //하늘 상태에 따라 확률 증가 | 107 | info.death_prob += info.sky.substr(5) * 1 //하늘 상태에 따라 확률 증가 | ... | ... |
-
Please register or login to post a comment