Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
RIT_Project1
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박권수
2021-05-11 02:39:26 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
757f464898019af10ddcf3a41a1aafdfe6a8cf2d
757f4648
1 parent
ebff0cb1
feat. logic and fix error, cookies alive 7days -> 30days
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
17 deletions
server/src/lib/DataProcess.js
server/src/lib/MqttModule.js
server/src/lib/jwtMiddleWare.js
server/src/lib/DataProcess.js
View file @
757f464
const
Bottle
=
require
(
'../models/bottle'
);
//message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수
exports
.
dataPublish
=
async
(
topic
,
message
)
=>
{
//client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴
const
data
=
await
factoring
(
topic
,
message
);
const
{
bottleId
}
=
data
;
//가공된 데이터를 bottleId의 약병에 업데이트
await
bottleInfoUpdate
(
data
);
//가공된 데이터를 메시지로 만들어 topic과 message 리턴
const
result
=
await
transPublishingTopicAndMessage
(
bottleId
);
return
result
;
};
//Hub topic : bottle/bottleId
//Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서
exports
.
factoring
=
(
topic
,
message
)
=>
{
const
factoring
=
(
topic
,
message
)
=>
{
const
bottleId
=
parseInt
(
topic
.
split
(
'/'
)[
1
]);
const
data
=
message
.
split
(
'/'
);
const
[
isOpen
,
temperature
,
humidity
,
balance
]
=
data
;
...
...
@@ -20,7 +34,7 @@ exports.factoring = (topic, message) => {
}
//bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다.
exports
.
bottleInfoUpdate
=
async
(
data
)
=>
{
const
bottleInfoUpdate
=
async
(
data
)
=>
{
const
{
bottleId
,
isOpen
,
openDate
,
temperature
,
humidity
,
balance
}
=
data
;
if
(
isOpen
===
'1'
)
{
await
Bottle
.
findOneAndUpdate
({
...
...
@@ -30,7 +44,7 @@ exports.bottleInfoUpdate = async(data) => {
});
}
await
Bottle
.
find
ById
AndUpdate
({
await
Bottle
.
find
One
AndUpdate
({
bottleId
},
{
temperature
,
...
...
@@ -39,18 +53,19 @@ exports.bottleInfoUpdate = async(data) => {
},
{
new
:
true
});
}
//해당 MQTT Broker(client)에 bottleId의 정보에 관한
message를 발행
한다.
exports
.
dataPublishing
=
async
(
client
,
bottleId
)
=>
{
const
topic
=
'bottle/'
.
concat
(
bottleId
);
//해당 MQTT Broker(client)에 bottleId의 정보에 관한
topic과 message를 리턴
한다.
const
transPublishingTopicAndMessage
=
async
(
bottleId
)
=>
{
const
topic
=
'bottle/'
.
concat
(
bottleId
)
+
'/stb'
;
const
bottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
const
recentOpen
=
await
bottle
.
getRecentOpenDate
();
const
message
=
await
transDate
(
recentOpen
);
client
.
publish
(
topic
,
message
,
()
=>
{
console
.
log
(
'topic : '
,
topic
,
'message : '
,
message
);
})
return
{
topic
,
message
}
}
//날짜를 yymmdd로 변환해주는 함수
...
...
server/src/lib/MqttModule.js
View file @
757f464
...
...
@@ -21,14 +21,24 @@ exports.mqttOn = async (hosting) => {
};
};
exports
.
mqttSubscribe
=
(
client
,
topic
)
=>
{
exports
.
mqttSubscribe
=
(
client
,
topic
,
func
)
=>
{
client
.
subscribe
(
topic
);
client
.
on
(
'message'
,
(
topic
,
message
,
packet
)
=>
{
cons
ole
.
log
(
'\x1b[1;37mtopic : '
,
topic
,
'\x1b[0m'
);
console
.
log
(
'\x1b[1;37mmessage : '
,
message
.
toString
(),
'\x1b[0m'
,
'\n'
);
client
.
on
(
'message'
,
async
(
topic
,
message
,
packet
)
=>
{
cons
t
result
=
await
func
(
topic
,
message
.
toString
()
);
this
.
mqttPublishMessage
(
client
,
result
);
});
};
exports
.
mqttPublishMessage
=
(
client
,
topic
,
message
)
=>
{
exports
.
mqttPublishMessage
=
(
client
,
{
topic
,
message
}
)
=>
{
client
.
publish
(
topic
,
message
,
()
=>
{});
};
\ No newline at end of file
};
exports
.
mqttOff
=
(
hosting
)
=>
{
const
filterIndex
=
clientList
.
findIndex
(
client
=>
{
return
(
client
.
options
.
clientId
===
hosting
.
clientId
&&
client
.
options
.
host
===
hosting
.
host
&&
client
.
options
.
port
===
hosting
.
port
)
});
clientList
[
filterIndex
].
end
();
clientList
.
splice
(
filterIndex
,
1
);
}
\ No newline at end of file
...
...
server/src/lib/jwtMiddleWare.js
View file @
757f464
...
...
@@ -14,13 +14,13 @@ const jwtMiddleware = async (ctx, next) => {
userId
:
decoded
.
userId
};
const
now
=
Math
.
floor
(
Date
.
now
()
/
1000
);
if
(
decoded
.
exp
-
now
<
60
*
60
*
24
*
3.5
)
{
if
(
decoded
.
exp
-
now
<
60
*
60
*
24
*
7
)
{
const
user
=
await
User
.
findById
(
decoded
.
_id
);
const
token
=
user
.
generateToken
();
ctx
.
cookies
.
set
(
'access_token'
,
token
,
{
httpOnly
:
true
,
maxAge
:
1000
*
60
*
60
*
24
*
7
maxAge
:
1000
*
60
*
60
*
24
*
30
})
}
...
...
Please
register
or
login
to post a comment