Toggle navigation
Toggle navigation
This project
Loading...
Sign in
조민지
/
2018_2_project
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
DESKTOP-PC6TBNS\강환석씨
2018-12-11 15:01:00 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
04199e6fc83c5ba925b3621018489282d4535edc
04199e6f
1 parent
f3174baf
리팩토링 => socket 부분 간소화
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
55 deletions
lib/socketio.js
lib/socketio.js
View file @
04199e6
...
...
@@ -5,12 +5,23 @@ const lat = "37.239795";
const
lon
=
"127.083240"
;
module
.
exports
=
(
server
,
app
)
=>
{
const
io
=
require
(
'socket.io'
,
)(
server
,
{
transports
:
[
'websocket'
]
const
io
=
require
(
'socket.io'
)(
server
,
{
transports
:
[
'websocket'
]
// websocket 사용시 polling 사용을 배제하고 안정적인 websocket만 사용함
});
//명시적 형 선언
let
Current_Weather
=
{};
let
Sensible_T
=
{};
let
Heat_index
=
{};
let
Discomport_index
=
{};
let
Ultra_Violet_index
=
{};
let
sending_to_client_info
=
{};
let
client_send
=
{};
let
sql
;
let
info
=
{}
const
CALL
=
(
when
,
what
)
=>
{
const
req_API
=
(
when
,
what
)
=>
{
//async await 사용하기 위하여 promise 사용
return
new
Promise
((
resolve
,
reject
)
=>
{
requesting
.
get
({
// api를 요청할 주소 -- 시크릿키,위도,경도 입력
...
...
@@ -24,38 +35,21 @@ module.exports = (server, app) => {
// api의 response이 있을경우 promise resolve 호출
if
(
api_res
)
{
console
.
log
(
"call"
);
console
.
log
(
"call
ing api
"
);
resolve
(
api_body
);
}
});
})
}
io
.
on
(
'connection'
,
(
socket
)
=>
{
//웹 페이지 연결시 루프 동작
let
API_CALL
;
//명시적으로 객체임을 선언
let
Current_Weather
=
{};
let
Sensible_T
=
{};
let
Heat_index
=
{};
let
Discomport_index
=
{};
let
Ultra_Violet_index
=
{};
let
sending_to_client_info
=
{};
let
client_send
=
{};
let
sql
;
socket
.
on
(
"connection"
,
()
=>
{
console
.
log
(
"lala"
);
// API_CALL = setInterval(() => {
console
.
log
(
"lala"
);
}
const
API_bundle
=
async
()
=>
{
try
{
Current_Weather
=
await
CALL
(
"current"
,
"minutely"
);
//현재날씨 (분별)
Sensible_T
=
await
CALL
(
"index"
,
"wct"
);
//체감온도
Heat_index
=
await
CALL
(
"index"
,
"heat"
);
//열지수
Discomport_index
=
await
CALL
(
"index"
,
"th"
);
//불쾌지수
Ultra_Violet_index
=
await
CALL
(
"index"
,
"uv"
);
//자외선지수
Current_Weather
=
await
req_API
(
"current"
,
"minutely"
);
//현재날씨 (분별)
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"
);
//자외선지수
console
.
log
(
"bundle"
);
info
=
{
...
...
@@ -78,53 +72,78 @@ module.exports = (server, app) => {
// ------------------------------ death_prob 정의 ------------------------------
info
.
death_prob
+=
info
.
sky
.
substr
(
5
)
*
1
//하늘 상태에 따라 확률 증가
info
.
death_prob
+=
info
.
sky
.
substr
(
5
)
*
1
//하늘 상태에 따라 확률 증가
if
(
info
.
lightning
===
1
)
//낙뢰시에 확률 증가
if
(
info
.
lightning
===
1
)
//낙뢰시에 확률 증가
info
.
death_prob
+=
10
;
if
(
info
.
typhoon
===
"Y"
)
//태풍시에 확률 증가
if
(
info
.
typhoon
===
"Y"
)
//태풍시에 확률 증가
info
.
death_prob
+=
10
;
if
(
info
.
warning
===
"Y"
)
// 특보 발령시 확률 증가
if
(
info
.
warning
===
"Y"
)
// 특보 발령시 확률 증가
info
.
death_prob
+=
5
//죽을 확률 계산(내맘대로 커스텀)
info
.
death_prob
=
(
(
info
.
heat
/
8
)
+
(
Math
.
abs
(
info
.
sensible_temperature
-
15
)
/
2
)
+
(
info
.
discomport
/
10
)
+
(
info
.
UV
/
5
)
+
(
info
.
windspd
*
3
)
+
(
info
.
rain
/
10
)
+
(
Math
.
abs
(
info
.
current_temperature
-
15
)
/
2
)
info
.
death_prob
=
(
(
info
.
heat
/
8
)
+
(
Math
.
abs
(
info
.
sensible_temperature
-
15
)
/
2
)
+
(
info
.
discomport
/
10
)
+
(
info
.
UV
/
5
)
+
(
info
.
windspd
*
3
)
+
(
info
.
rain
/
10
)
+
(
Math
.
abs
(
info
.
current_temperature
-
15
)
/
2
)
);
//이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보
client_send
=
{
time
:
info
.
time
,
wind
:
info
.
windspd
,
temperature
:
info
.
current_temperature
,
rain
:
info
.
rain
,
death
:
info
.
death_prob
client_send
=
{
time
:
info
.
time
,
wind
:
info
.
windspd
,
temperature
:
info
.
current_temperature
,
rain
:
info
.
rain
,
death
:
info
.
death_prob
};
function
getRandom_add_prob
(
min
,
max
)
{
return
Math
.
random
()
*
(
max
-
min
)
+
min
;
}
// 심장마비로 갑자기 확률 증가 할 수 있음
Math
.
random
()
*
2
>=
1
?
client_send
.
death
+=
getRandom_add_prob
(
0
,
10
)
:
client_send
.
death
-=
getRandom_add_prob
(
0
,
10
)
;
//만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리
if
(
client_send
.
death
>=
100
)
{
client_send
.
death
=
100
;
if
(
client_send
.
death
>=
100
)
{
client_send
.
death
=
100
;
}
console
.
log
(
client_send
)
app
.
get
(
"socket"
).
emit
(
"weatherInfo_minutely_send_to_client"
,
client_send
);
// 클라이언트에게 정보 담아서 이벤트 발산
console
.
log
(
"emit"
);
//db에 저장
sql
=
"INSERT INTO weatherInfo (time,wind,temperature,rain,prob) VALUES (?,?,?,?,?)"
;
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
);
})
}
catch
(
err
)
{
//promise err or try err catch
console
.
log
(
"================Error Occured !!================\n"
,
err
);
console
.
log
(
"================Error Occured !!================\n"
,
err
);
}
}
API_bundle
();
// //db에 저장
// sql="INSERT INTO weatherInfo (time,wind,temperature,rain,prob) VALUES (?,?,?,?,?)";
// 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);
// socket.emit("weatherInfo_minutely_send_to_client",client_send); // 클라이언트에게 정보 담아서 이벤트 발산
// })
// }, 1* 1000); //1분마다 호출
let
call_interval
;
const
Start_Interval
=
(
second
,
CALL
)
=>
{
CALL
();
//처음 불러올때 한번 호출하고
call_interval
=
setInterval
(
CALL
,
second
*
1000
);
//그 후에 1분마다 호출
}
io
.
on
(
'connection'
,
(
socket
)
=>
{
//프론트와 소켓 연결시 이벤트 루프 동작
app
.
set
(
"socket"
,
socket
);
socket
.
on
(
"connection"
,
()
=>
{
console
.
log
(
"SOCKET CONNECTED"
);
Start_Interval
(
60
,
API_bundle
);
//소켓 연결후 interval 활성화하여 1분마다 API 호출
});
socket
.
on
(
'disconnecting'
,
(
reason
)
=>
{
clearInterval
(
API_CALL
);
//연결 종료시 해제
socket
.
on
(
'disconnect'
,
(
reason
)
=>
{
console
.
log
(
"disconnected"
);
clearInterval
(
call_interval
);
//연결 종료시 interval 해제
})
})
}
}
...
...
Please
register
or
login
to post a comment