Showing
3 changed files
with
39 additions
and
6 deletions
FatalTrafficAccidents_CodeList.xlsx
0 → 100644
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 secret_D_key = require('../keys/api_option').D_key;//사망교통사고 키 | 3 | +const traffic_key = require('../keys/api_option').traffic_key; |
| 4 | const requesting = require('request'); | 4 | const requesting = require('request'); |
| 5 | const lat = "37.239795"; | 5 | const lat = "37.239795"; |
| 6 | 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; | ||
| 7 | module.exports = (server, app) => { | 11 | module.exports = (server, app) => { |
| 8 | 12 | ||
| 9 | const io = require('socket.io')(server, { | 13 | const io = require('socket.io')(server, { |
| ... | @@ -15,6 +19,7 @@ module.exports = (server, app) => { | ... | @@ -15,6 +19,7 @@ module.exports = (server, app) => { |
| 15 | let Heat_index = {}; | 19 | let Heat_index = {}; |
| 16 | let Discomport_index = {}; | 20 | let Discomport_index = {}; |
| 17 | let Ultra_Violet_index = {}; | 21 | let Ultra_Violet_index = {}; |
| 22 | + let Traffic_Accident = {}; | ||
| 18 | let sending_to_client_info = {}; | 23 | let sending_to_client_info = {}; |
| 19 | let client_send = {}; | 24 | let client_send = {}; |
| 20 | let client_name = ""; | 25 | let client_name = ""; |
| ... | @@ -50,9 +55,28 @@ module.exports = (server, app) => { | ... | @@ -50,9 +55,28 @@ module.exports = (server, app) => { |
| 50 | }) | 55 | }) |
| 51 | } | 56 | } |
| 52 | 57 | ||
| 53 | - | 58 | + // 교통사고정보를 받아오는 함수 |
| 59 | + const req_traffic_API = (year) => { | ||
| 60 | + //async await 사용하기 위하여 promise 사용 | ||
| 61 | + return new Promise((resolve, reject) => { | ||
| 62 | + requesting.get({ | ||
| 63 | + // api를 요청할 주소 -- 시크릿키,위도,경도 입력 | ||
| 64 | + url: `http://taas.koroad.or.kr/data/rest/accident/death?authKey=${traffic_key}&searchYear=${year}&siDo=${city}&guGun=${town}&type=json`, | ||
| 65 | + json: true | ||
| 66 | + }, | ||
| 67 | + //api에게 응답 받았을때 실행되는 callback function | ||
| 68 | + function (err, api_res, api_body) { | ||
| 69 | + //err 존재시 promise reject 호출 | ||
| 70 | + if (err) reject(err); | ||
| 54 | 71 | ||
| 55 | - | 72 | + // api의 response이 있을경우 promise resolve 호출 |
| 73 | + if (api_res) { | ||
| 74 | + console.log("calling traffic api"); | ||
| 75 | + resolve(api_body); | ||
| 76 | + } | ||
| 77 | + }); | ||
| 78 | + }) | ||
| 79 | + } | ||
| 56 | const API_bundle = async () => { | 80 | const API_bundle = async () => { |
| 57 | 81 | ||
| 58 | try { | 82 | try { |
| ... | @@ -63,6 +87,12 @@ module.exports = (server, app) => { | ... | @@ -63,6 +87,12 @@ module.exports = (server, app) => { |
| 63 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 | 87 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 |
| 64 | //let kts = await req_D_API(); | 88 | //let kts = await req_D_API(); |
| 65 | Day3_Weather = await req_API("forecast","3days"); //3일예보(단기예보) | 89 | Day3_Weather = await req_API("forecast","3days"); //3일예보(단기예보) |
| 90 | + | ||
| 91 | + // 사용자의 생일에 따라서 다른 년도의 교통사고정보를 가져옴. | ||
| 92 | + const year = year_start + (client_birth % (year_range+1)); | ||
| 93 | + Traffic_Accident = await req_traffic_API(year); //교통사고정보 | ||
| 94 | + | ||
| 95 | + | ||
| 66 | info = { | 96 | info = { |
| 67 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 | 97 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 |
| 68 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 | 98 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 |
| ... | @@ -78,9 +108,11 @@ module.exports = (server, app) => { | ... | @@ -78,9 +108,11 @@ module.exports = (server, app) => { |
| 78 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 | 108 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 |
| 79 | 109 | ||
| 80 | Forecast_3D: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd64hour, | 110 | Forecast_3D: Day3_Weather.weather.forecast3days[0].fcst3hour.wind.wspd64hour, |
| 111 | + traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수 | ||
| 81 | death_prob: 0 //확률 | 112 | death_prob: 0 //확률 |
| 82 | } | 113 | } |
| 83 | console.log("API INFO \n", info); | 114 | console.log("API INFO \n", info); |
| 115 | + console.log("Traffic count:", info.traffic); | ||
| 84 | 116 | ||
| 85 | 117 | ||
| 86 | 118 | ||
| ... | @@ -98,7 +130,7 @@ module.exports = (server, app) => { | ... | @@ -98,7 +130,7 @@ module.exports = (server, app) => { |
| 98 | //죽을 확률 계산(내맘대로 커스텀) | 130 | //죽을 확률 계산(내맘대로 커스텀) |
| 99 | info.death_prob = ( | 131 | info.death_prob = ( |
| 100 | (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10) | 132 | (info.heat / 50) + (Math.abs(info.sensible_temperature - 15) / 10) + (info.discomport / 10) + (info.UV / 10) |
| 101 | - + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) | 133 | + + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) + (info.traffic / 5) |
| 102 | ); | 134 | ); |
| 103 | 135 | ||
| 104 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 | 136 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 | ... | ... |
| ... | @@ -132,7 +132,8 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { | ... | @@ -132,7 +132,8 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { |
| 132 | }); | 132 | }); |
| 133 | } | 133 | } |
| 134 | }); | 134 | }); |
| 135 | -} ) | 135 | +}); |
| 136 | + | ||
| 136 | router.get('/', function(req, res, next) { | 137 | router.get('/', function(req, res, next) { |
| 137 | res.render( 'main' ); | 138 | res.render( 'main' ); |
| 138 | }); | 139 | }); | ... | ... |
-
Please register or login to post a comment