Merge branch 'master' into 'master'
교통사고 api 적용 See merge request !3
Showing
11 changed files
with
97 additions
and
18 deletions
... | @@ -39,8 +39,6 @@ build/Release | ... | @@ -39,8 +39,6 @@ build/Release |
39 | # Dependency directories | 39 | # Dependency directories |
40 | node_modules/ | 40 | node_modules/ |
41 | jspm_packages/ | 41 | jspm_packages/ |
42 | -keys/api_option.js | ||
43 | -keys/db_option.js | ||
44 | # TypeScript v1 declaration files | 42 | # TypeScript v1 declaration files |
45 | typings/ | 43 | typings/ |
46 | 44 | ... | ... |
db_traffic.PNG
0 → 100644
56.2 KB
23.9 KB
developImg/2_TrafficAccidentAPI.PNG
0 → 100644
43.5 KB
keys/api_option.js
0 → 100644
keys/db_option.js
0 → 100644
... | @@ -2,5 +2,5 @@ module.exports = { | ... | @@ -2,5 +2,5 @@ module.exports = { |
2 | host: 'localhost', | 2 | host: 'localhost', |
3 | user: 'root', | 3 | user: 'root', |
4 | password: 'abcd', | 4 | password: 'abcd', |
5 | - database: 'db_option_trafficinfo' | 5 | + database: 'db_option_traffic' |
6 | } | 6 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
lib/Examples/trafficAccident_xml.PNG
0 → 100644
55.5 KB
lib/db_traffic.js
0 → 100644
1 | const db = require('./db.js'); | 1 | const db = require('./db.js'); |
2 | +const db_traffic = require('./db_traffic.js'); | ||
2 | const secret_key = require('../keys/api_option').key; | 3 | const secret_key = require('../keys/api_option').key; |
4 | +const secret_key_traffic = require('../keys/api_option_traffic').key; | ||
5 | + | ||
3 | const requesting = require('request'); | 6 | const requesting = require('request'); |
4 | -const lat = "37.239795"; | 7 | + |
5 | -const lon = "127.083240"; | 8 | +const lat = "37.54922931758";//"37.239795"; //weather 위도 |
9 | +const lon = "126.913415969472";//"127.083240"; //weather 경도 | ||
10 | +const sido_num = "11"; //traffic 법정동 시도 코드 | ||
11 | +const gugun_num = "440"; //traffic 법정동 시군구 코드 - 합정동 일대 | ||
12 | +const search_year = "2018074"; //traffic 검색을 원하는 연도 | ||
13 | + | ||
6 | module.exports = (server, app) => { | 14 | module.exports = (server, app) => { |
7 | 15 | ||
8 | const io = require('socket.io')(server, { | 16 | const io = require('socket.io')(server, { |
9 | transports: ['websocket'] // websocket 사용시 polling 사용을 배제하고 안정적인 websocket만 사용함 | 17 | transports: ['websocket'] // websocket 사용시 polling 사용을 배제하고 안정적인 websocket만 사용함 |
10 | }); | 18 | }); |
11 | - //명시적 형 선언 | 19 | + |
20 | + //명시적 형 선언 - Weather, Traffic, Common | ||
12 | let Current_Weather = {}; | 21 | let Current_Weather = {}; |
13 | let Sensible_T = {}; | 22 | let Sensible_T = {}; |
14 | let Heat_index = {}; | 23 | let Heat_index = {}; |
... | @@ -20,9 +29,18 @@ module.exports = (server, app) => { | ... | @@ -20,9 +29,18 @@ module.exports = (server, app) => { |
20 | let client_birth ; | 29 | let client_birth ; |
21 | let Destiny; | 30 | let Destiny; |
22 | let sql; | 31 | let sql; |
23 | - | 32 | + let info = {} //;? |
24 | - let info = {} | 33 | + |
25 | - | 34 | + let Current_TrafficAcc = {}; |
35 | + let sql_traffic; | ||
36 | + //let DeathNum_index = {}; //사상자 | ||
37 | + //let M_HurtNum_index = {}; //중상자 | ||
38 | + //let L_HurtNum_index = {}; //경상자 | ||
39 | + //let LocalName_index = {}; //지역 이름 | ||
40 | + //let OccurNum_index = {}; //발생 건수 | ||
41 | + //afos_fid: 다발지역 FID, bjd_cd: 법정동 코드, spot_cd:지점코드 | ||
42 | + | ||
43 | + //API 호출_Weather | ||
26 | const req_API = (when, what) => { | 44 | const req_API = (when, what) => { |
27 | //async await 사용하기 위하여 promise 사용 | 45 | //async await 사용하기 위하여 promise 사용 |
28 | return new Promise((resolve, reject) => { | 46 | return new Promise((resolve, reject) => { |
... | @@ -38,22 +56,57 @@ module.exports = (server, app) => { | ... | @@ -38,22 +56,57 @@ module.exports = (server, app) => { |
38 | 56 | ||
39 | // api의 response이 있을경우 promise resolve 호출 | 57 | // api의 response이 있을경우 promise resolve 호출 |
40 | if (api_res) { | 58 | if (api_res) { |
41 | - console.log("calling api"); | 59 | + console.log("calling weather api"); |
42 | resolve(api_body); | 60 | resolve(api_body); |
43 | } | 61 | } |
44 | }); | 62 | }); |
45 | }) | 63 | }) |
64 | + } | ||
65 | + //API 호출_Traffic | ||
66 | + const req_API_traffic = (cate1, cate2) => { | ||
67 | + //async await 사용하기 위하여 promise 사용 | ||
68 | + return new Promise((resolve, reject) => { | ||
69 | + requesting.get({ | ||
70 | + // api를 요청할 주소 -- 시크릿키,위도,경도 입력 | ||
71 | + 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`, | ||
72 | + //나중에 삭제 http://apis.data.go.kr/B552061/frequentzoneLg/getRestFrequentzoneLg?ServiceKey=XOLqI9tY4gVCdk1i2BoOM3EYmSUYqt8WlXUfIDSfD4RnNBrFwwthbNAtGOxoskP540JbUAzcnuABsydoE2U2dA%3D%3D&searchYearCd=2017&siDo=11&guGun=200&type=json&numOfRows=10&pageNo=1 | ||
73 | + //나중에 삭제 http://apis.data.go.kr/B552061/frequentzoneLg/getRestFrequentzoneLg?ServiceKey=XOLqI9tY4gVCdk1i2BoOM3EYmSUYqt8WlXUfIDSfD4RnNBrFwwthbNAtGOxoskP540JbUAzcnuABsydoE2U2dA%3D%3D&searchYearCd=2018074&siDo=11&guGun=440&type=json&numOfRows=10&pageNo=1 | ||
74 | + //sido = 11 gugun = 440 (마포구 합정동, 도화동, 도화동) | ||
75 | + //cate1 = frequentzoneLg | ||
76 | + //cate2 = getRestFrequentzoneLg | ||
77 | + //xml: true | ||
78 | + json: true | ||
79 | + }, | ||
80 | + //api에게 응답 받았을때 실행되는 callback function | ||
81 | + function (err, api_res, api_body) { | ||
82 | + //err 존재시 promise reject 호출 | ||
83 | + if (err) reject(err); | ||
46 | 84 | ||
85 | + // api의 response이 있을경우 promise resolve 호출 | ||
86 | + if (api_res) { | ||
87 | + console.log("calling traffic api"); | ||
88 | + resolve(api_body); | ||
89 | + } | ||
90 | + }); | ||
91 | + }) | ||
47 | } | 92 | } |
93 | + | ||
48 | const API_bundle = async () => { | 94 | const API_bundle = async () => { |
49 | 95 | ||
50 | try { | 96 | try { |
51 | - Current_Weather = await req_API("current", "minutely"); //현재날씨 (분별) | 97 | + Current_Weather = await req_API("current", "minutely"); //현재날씨 (분별) //when, what |
52 | Sensible_T = await req_API("index", "wct"); //체감온도 | 98 | Sensible_T = await req_API("index", "wct"); //체감온도 |
53 | Heat_index = await req_API("index", "heat"); //열지수 | 99 | Heat_index = await req_API("index", "heat"); //열지수 |
54 | Discomport_index = await req_API("index", "th"); //불쾌지수 | 100 | Discomport_index = await req_API("index", "th"); //불쾌지수 |
55 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 | 101 | Ultra_Violet_index = await req_API("index", "uv"); //자외선지수 |
56 | 102 | ||
103 | + Current_TrafficAcc = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); //cate1, cate2 | ||
104 | + /*DeathNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); | ||
105 | + M_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); | ||
106 | + L_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); | ||
107 | + LocalName_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg"); | ||
108 | + OccurNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");*/ | ||
109 | + | ||
57 | info = { | 110 | info = { |
58 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 | 111 | heat: Heat_index.weather.wIndex.heatIndex[0].current.index, //열지수 |
59 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 | 112 | sensible_temperature: Sensible_T.weather.wIndex.wctIndex[0].current.index, //체감온도 |
... | @@ -67,7 +120,12 @@ module.exports = (server, app) => { | ... | @@ -67,7 +120,12 @@ module.exports = (server, app) => { |
67 | warning: Current_Weather.common.alertYn, //현재 특보 유무 | 120 | warning: Current_Weather.common.alertYn, //현재 특보 유무 |
68 | typhoon: Current_Weather.common.stormYn, //현재 태풍 | 121 | typhoon: Current_Weather.common.stormYn, //현재 태풍 |
69 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 | 122 | time: Current_Weather.weather.minutely[0].timeObservation, // 불러온 시각 |
70 | - death_prob: 0 //확률 | 123 | + death_prob: 0, //확률 |
124 | + death_number: Current_TrafficAcc.items.item[0].caslt_cnt, //사상자 수 | ||
125 | + midhurt_number: Current_TrafficAcc.items.item[0].se_dnv_cnt, //중상자 수 | ||
126 | + lighthurt_number: Current_TrafficAcc.items.item[0].sl_dnv_cnt, //경상자 수 | ||
127 | + occur_number: Current_TrafficAcc.items.item[0].occrrnc_cnt //발생건수 | ||
128 | + //"spot_cd": "11440001" | ||
71 | } | 129 | } |
72 | console.log("API INFO \n", info); | 130 | console.log("API INFO \n", info); |
73 | 131 | ||
... | @@ -88,13 +146,17 @@ module.exports = (server, app) => { | ... | @@ -88,13 +146,17 @@ module.exports = (server, app) => { |
88 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) | 146 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) |
89 | ); | 147 | ); |
90 | 148 | ||
91 | - //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 | 149 | + //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보, 홈페이지 그래프에 나타날 정보 |
92 | client_send = { | 150 | client_send = { |
93 | time: info.time, | 151 | time: info.time, |
94 | wind: info.windspd, | 152 | wind: info.windspd, |
95 | temperature: info.current_temperature, | 153 | temperature: info.current_temperature, |
96 | rain: info.rain, | 154 | rain: info.rain, |
97 | - death: info.death_prob | 155 | + death: info.death_prob, |
156 | + trafficdeath: info.death_number, | ||
157 | + mhurt: info.midhurt_number, | ||
158 | + lhurt: info.lighthurt_number, | ||
159 | + occurence: info.occur_number | ||
98 | }; | 160 | }; |
99 | function getRandom_add_prob(min, max) { | 161 | function getRandom_add_prob(min, max) { |
100 | return Math.random() * (max - min) + min; | 162 | return Math.random() * (max - min) + min; |
... | @@ -120,7 +182,7 @@ module.exports = (server, app) => { | ... | @@ -120,7 +182,7 @@ module.exports = (server, app) => { |
120 | 182 | ||
121 | console.log("client send data \n",client_send) | 183 | console.log("client send data \n",client_send) |
122 | 184 | ||
123 | - app.get("socket").emit("weatherInfo_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산 | 185 | + app.get("socket").emit("weather_and_traffic_Info_minutely_send_to_client", client_send); // 클라이언트에게 정보 담아서 이벤트 발산 |
124 | console.log("emit"); | 186 | console.log("emit"); |
125 | 187 | ||
126 | //db에 저장 | 188 | //db에 저장 |
... | @@ -128,6 +190,11 @@ module.exports = (server, app) => { | ... | @@ -128,6 +190,11 @@ module.exports = (server, app) => { |
128 | db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death], (err, result) => { | 190 | db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death], (err, result) => { |
129 | if (err) console.log(err); | 191 | if (err) console.log(err); |
130 | }) | 192 | }) |
193 | + | ||
194 | + sql_traffic = "INSERT INTO trafficInfo (tdeath,mhurt,lhurt,occurence,prob) VALUES (?,?,?,?,?)"; | ||
195 | + db_traffic.query(sql_traffic, [client_send.trafficdeath, client_send.mhurt, client_send.lhurt, client_send.occurence, client_send.death], (err, result) => { | ||
196 | + if (err) console.log(err); | ||
197 | + }) | ||
131 | } catch (err) { //promise err or try err catch | 198 | } catch (err) { //promise err or try err catch |
132 | console.log("================Error Occured !!================\n", err); | 199 | console.log("================Error Occured !!================\n", err); |
133 | } | 200 | } |
... | @@ -145,8 +212,8 @@ module.exports = (server, app) => { | ... | @@ -145,8 +212,8 @@ module.exports = (server, app) => { |
145 | app.set("socket", socket); | 212 | app.set("socket", socket); |
146 | socket.on("connection", (client_data) => { | 213 | socket.on("connection", (client_data) => { |
147 | console.log("SOCKET CONNECTED"); | 214 | console.log("SOCKET CONNECTED"); |
148 | - client_name = client_data.name; | 215 | + client_name = client_data.name; //이름 입력 |
149 | - client_birth = client_data.birth; | 216 | + client_birth = client_data.birth; //생일 입력 |
150 | Start_Interval(60, API_bundle); //소켓 연결후 interval 활성화하여 1분마다 API 호출 | 217 | Start_Interval(60, API_bundle); //소켓 연결후 interval 활성화하여 1분마다 API 호출 |
151 | 218 | ||
152 | }); | 219 | }); | ... | ... |
-
Please register or login to post a comment