Toggle navigation
Toggle navigation
This project
Loading...
Sign in
조인화
/
Probability Death
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
김주희
2019-05-31 21:22:33 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
84fa0d50256f334e82de49ddddbe43bf46c353ec
84fa0d50
2 parents
baf07756
882720f1
Merge branch 'trafficSocket' to master
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
16 deletions
keys/api_option.js
lib/Examples/trafficAccident_xml.PNG
lib/db_traffic.js
lib/socketio.js
keys/api_option.js
View file @
84fa0d5
module
.
exports
=
{
key
:
"
2ef7cebf-d079-40c0-bdb2-174e0e02912c
"
key
:
"
aaaa
"
}
//Server Key
\ No newline at end of file
//개인 Server Key 받아서 사용해야함, Browser key 안됨
\ No newline at end of file
...
...
lib/Examples/trafficAccident_xml.PNG
0 → 100644
View file @
84fa0d5
55.5 KB
lib/db_traffic.js
0 → 100644
View file @
84fa0d5
const
mysql
=
require
(
'mysql'
);
const
DB_option_traffic
=
require
(
'../keys/db_option_traffic'
);
module
.
exports
=
mysql
.
createConnection
(
DB_option_traffic
);
lib/socketio.js
View file @
84fa0d5
const
db
=
require
(
'./db.js'
);
const
db_traffic
=
require
(
'./db_traffic.js'
);
const
secret_key
=
require
(
'../keys/api_option'
).
key
;
const
secret_key_traffic
=
require
(
'../keys/api_option_traffic'
).
key
;
const
requesting
=
require
(
'request'
);
const
lat
=
"37.239795"
;
const
lon
=
"127.083240"
;
const
lat
=
"37.54922931758"
;
//"37.239795"; //weather 위도
const
lon
=
"126.913415969472"
;
//"127.083240"; //weather 경도
const
sido_num
=
"11"
;
//traffic 법정동 시도 코드
const
gugun_num
=
"440"
;
//traffic 법정동 시군구 코드 - 합정동 일대
const
search_year
=
"2018074"
;
//traffic 검색을 원하는 연도
module
.
exports
=
(
server
,
app
)
=>
{
const
io
=
require
(
'socket.io'
)(
server
,
{
transports
:
[
'websocket'
]
// websocket 사용시 polling 사용을 배제하고 안정적인 websocket만 사용함
});
//명시적 형 선언
//명시적 형 선언 - Weather, Traffic, Common
let
Current_Weather
=
{};
let
Sensible_T
=
{};
let
Heat_index
=
{};
...
...
@@ -20,9 +29,18 @@ module.exports = (server, app) => {
let
client_birth
;
let
Destiny
;
let
sql
;
let
info
=
{}
let
info
=
{}
//;?
let
Current_TrafficAcc
=
{};
let
sql_traffic
;
//let DeathNum_index = {}; //사상자
//let M_HurtNum_index = {}; //중상자
//let L_HurtNum_index = {}; //경상자
//let LocalName_index = {}; //지역 이름
//let OccurNum_index = {}; //발생 건수
//afos_fid: 다발지역 FID, bjd_cd: 법정동 코드, spot_cd:지점코드
//API 호출_Weather
const
req_API
=
(
when
,
what
)
=>
{
//async await 사용하기 위하여 promise 사용
return
new
Promise
((
resolve
,
reject
)
=>
{
...
...
@@ -38,22 +56,57 @@ module.exports = (server, app) => {
// api의 response이 있을경우 promise resolve 호출
if
(
api_res
)
{
console
.
log
(
"calling api"
);
console
.
log
(
"calling
weather
api"
);
resolve
(
api_body
);
}
});
})
}
//API 호출_Traffic
const
req_API_traffic
=
(
cate1
,
cate2
)
=>
{
//async await 사용하기 위하여 promise 사용
return
new
Promise
((
resolve
,
reject
)
=>
{
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
function
(
err
,
api_res
,
api_body
)
{
//err 존재시 promise reject 호출
if
(
err
)
reject
(
err
);
// api의 response이 있을경우 promise resolve 호출
if
(
api_res
)
{
console
.
log
(
"calling traffic api"
);
resolve
(
api_body
);
}
});
})
}
const
API_bundle
=
async
()
=>
{
try
{
Current_Weather
=
await
req_API
(
"current"
,
"minutely"
);
//현재날씨 (분별)
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
/*DeathNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
M_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
L_HurtNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
LocalName_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");
OccurNum_index = await req_API_traffic("frequentzoneLg", "getRestFrequentzoneLg");*/
info
=
{
heat
:
Heat_index
.
weather
.
wIndex
.
heatIndex
[
0
].
current
.
index
,
//열지수
sensible_temperature
:
Sensible_T
.
weather
.
wIndex
.
wctIndex
[
0
].
current
.
index
,
//체감온도
...
...
@@ -67,7 +120,12 @@ module.exports = (server, app) => {
warning
:
Current_Weather
.
common
.
alertYn
,
//현재 특보 유무
typhoon
:
Current_Weather
.
common
.
stormYn
,
//현재 태풍
time
:
Current_Weather
.
weather
.
minutely
[
0
].
timeObservation
,
// 불러온 시각
death_prob
:
0
//확률
death_prob
:
0
,
//확률
death_number
:
Current_TrafficAcc
.
items
.
item
[
0
].
caslt_cnt
,
//사상자 수
midhurt_number
:
Current_TrafficAcc
.
items
.
item
[
0
].
se_dnv_cnt
,
//중상자 수
lighthurt_number
:
Current_TrafficAcc
.
items
.
item
[
0
].
sl_dnv_cnt
,
//경상자 수
occur_number
:
Current_TrafficAcc
.
items
.
item
[
0
].
occrrnc_cnt
//발생건수
//"spot_cd": "11440001"
}
console
.
log
(
"API INFO \n"
,
info
);
...
...
@@ -88,13 +146,17 @@ module.exports = (server, app) => {
+
info
.
windspd
*
1
+
(
info
.
rain
/
10
)
+
(
Math
.
abs
(
info
.
current_temperature
-
15
)
/
10
)
);
//이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보
//이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보
, 홈페이지 그래프에 나타날 정보
client_send
=
{
time
:
info
.
time
,
wind
:
info
.
windspd
,
temperature
:
info
.
current_temperature
,
rain
:
info
.
rain
,
death
:
info
.
death_prob
death
:
info
.
death_prob
,
trafficdeath
:
info
.
death_number
,
mhurt
:
info
.
midhurt_number
,
lhurt
:
info
.
lighthurt_number
,
occurence
:
info
.
occur_number
};
function
getRandom_add_prob
(
min
,
max
)
{
return
Math
.
random
()
*
(
max
-
min
)
+
min
;
...
...
@@ -120,7 +182,7 @@ module.exports = (server, app) => {
console
.
log
(
"client send data \n"
,
client_send
)
app
.
get
(
"socket"
).
emit
(
"weatherInfo_minutely_send_to_client"
,
client_send
);
// 클라이언트에게 정보 담아서 이벤트 발산
app
.
get
(
"socket"
).
emit
(
"weather
_and_traffic_
Info_minutely_send_to_client"
,
client_send
);
// 클라이언트에게 정보 담아서 이벤트 발산
console
.
log
(
"emit"
);
//db에 저장
...
...
@@ -128,6 +190,11 @@ module.exports = (server, app) => {
db
.
query
(
sql
,
[
client_send
.
time
,
client_send
.
wind
,
client_send
.
temperature
,
client_send
.
rain
,
client_send
.
death
],
(
err
,
result
)
=>
{
if
(
err
)
console
.
log
(
err
);
})
sql_traffic
=
"INSERT INTO trafficInfo (tdeath,mhurt,lhurt,occurence,prob) VALUES (?,?,?,?,?)"
;
db_traffic
.
query
(
sql_traffic
,
[
client_send
.
trafficdeath
,
client_send
.
mhurt
,
client_send
.
lhurt
,
client_send
.
occurence
,
client_send
.
death
],
(
err
,
result
)
=>
{
if
(
err
)
console
.
log
(
err
);
})
}
catch
(
err
)
{
//promise err or try err catch
console
.
log
(
"================Error Occured !!================\n"
,
err
);
}
...
...
@@ -145,8 +212,8 @@ module.exports = (server, app) => {
app
.
set
(
"socket"
,
socket
);
socket
.
on
(
"connection"
,
(
client_data
)
=>
{
console
.
log
(
"SOCKET CONNECTED"
);
client_name
=
client_data
.
name
;
client_birth
=
client_data
.
birth
;
client_name
=
client_data
.
name
;
//이름 입력
client_birth
=
client_data
.
birth
;
//생일 입력
Start_Interval
(
60
,
API_bundle
);
//소켓 연결후 interval 활성화하여 1분마다 API 호출
});
...
...
Please
register
or
login
to post a comment