이승규

교통사고 api 추가

No preview for this file type
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').weather_key;
3 +const traffic_key = require('../keys/api_option').traffic_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";
7 +const city = "1300";
8 +const town = "1302";
9 +const year_range = 6;
10 +const year_start = 2012;
6 module.exports = (server, app) => { 11 module.exports = (server, app) => {
7 12
8 const io = require('socket.io')(server, { 13 const io = require('socket.io')(server, {
...@@ -14,6 +19,7 @@ module.exports = (server, app) => { ...@@ -14,6 +19,7 @@ module.exports = (server, app) => {
14 let Heat_index = {}; 19 let Heat_index = {};
15 let Discomport_index = {}; 20 let Discomport_index = {};
16 let Ultra_Violet_index = {}; 21 let Ultra_Violet_index = {};
22 + let Traffic_Accident = {};
17 let sending_to_client_info = {}; 23 let sending_to_client_info = {};
18 let client_send = {}; 24 let client_send = {};
19 let client_name = ""; 25 let client_name = "";
...@@ -44,6 +50,29 @@ module.exports = (server, app) => { ...@@ -44,6 +50,29 @@ module.exports = (server, app) => {
44 }); 50 });
45 }) 51 })
46 } 52 }
53 +
54 + // 교통사고정보를 받아오는 함수
55 + const req_traffic_API = (year) => {
56 + //async await 사용하기 위하여 promise 사용
57 + return new Promise((resolve, reject) => {
58 + requesting.get({
59 + // api를 요청할 주소 -- 시크릿키,위도,경도 입력
60 + url: `http://taas.koroad.or.kr/data/rest/accident/death?authKey=${traffic_key}&searchYear=${year}&siDo=${city}&guGun=${town}&type=json`,
61 + json: true
62 + },
63 + //api에게 응답 받았을때 실행되는 callback function
64 + function (err, api_res, api_body) {
65 + //err 존재시 promise reject 호출
66 + if (err) reject(err);
67 +
68 + // api의 response이 있을경우 promise resolve 호출
69 + if (api_res) {
70 + console.log("calling traffic api");
71 + resolve(api_body);
72 + }
73 + });
74 + })
75 + }
47 const API_bundle = async () => { 76 const API_bundle = async () => {
48 77
49 try { 78 try {
...@@ -53,6 +82,11 @@ module.exports = (server, app) => { ...@@ -53,6 +82,11 @@ module.exports = (server, app) => {
53 Discomport_index = await req_API("index", "th"); //불쾌지수 82 Discomport_index = await req_API("index", "th"); //불쾌지수
54 Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 83 Ultra_Violet_index = await req_API("index", "uv"); //자외선지수
55 84
85 + // 사용자의 생일에 따라서 다른 년도의 교통사고정보를 가져옴.
86 + const year = year_start + (client_birth % (year_range+1));
87 + Traffic_Accident = await req_traffic_API(year); //교통사고정보
88 +
89 +
56 info = { 90 info = {
57 heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 91 heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수
58 sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 92 sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도
...@@ -66,9 +100,11 @@ module.exports = (server, app) => { ...@@ -66,9 +100,11 @@ module.exports = (server, app) => {
66 warning: Current_Weather.common.alertYn, //현재 특보 유무 100 warning: Current_Weather.common.alertYn, //현재 특보 유무
67 typhoon: Current_Weather.common.stormYn, //현재 태풍 101 typhoon: Current_Weather.common.stormYn, //현재 태풍
68 time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 102 time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각
103 + traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수
69 death_prob: 0 //확률 104 death_prob: 0 //확률
70 } 105 }
71 console.log("API INFO \n", info); 106 console.log("API INFO \n", info);
107 + console.log("Traffic count:", info.traffic);
72 108
73 // ------------------------------ death_prob 정의 ------------------------------ 109 // ------------------------------ death_prob 정의 ------------------------------
74 110
...@@ -84,7 +120,7 @@ module.exports = (server, app) => { ...@@ -84,7 +120,7 @@ module.exports = (server, app) => {
84 //죽을 확률 계산(내맘대로 커스텀) 120 //죽을 확률 계산(내맘대로 커스텀)
85 info.death_prob = ( 121 info.death_prob = (
86 (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10) 122 (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10)
87 - + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) 123 + + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) + (info.traffic / 5)
88 ); 124 );
89 125
90 //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 126 //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보
......
...@@ -66,7 +66,8 @@ router.get('/name/:name/birth/:birth', (req,res) => { ...@@ -66,7 +66,8 @@ router.get('/name/:name/birth/:birth', (req,res) => {
66 }); 66 });
67 } 67 }
68 }); 68 });
69 -} ) 69 +});
70 +
70 router.get('/', function(req, res, next) { 71 router.get('/', function(req, res, next) {
71 res.render( 'main' ); 72 res.render( 'main' );
72 }); 73 });
......