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 23:11:03 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
1228fd4396b78753eeb00e500d939afd3a9e5cf1
1228fd43
1 parent
35e88303
feat. http status and authorization add
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
104 additions
and
18 deletions
server/src/api/auth/auth.ctrl.js
server/src/api/bottle/bottle.ctrl.js
server/src/api/hub/hub.ctrl.js
server/src/api/medicine/medicine.ctrl.js
server/src/api/auth/auth.ctrl.js
View file @
1228fd4
...
...
@@ -30,7 +30,7 @@ exports.register = async(ctx) => {
await
user
.
setPassword
(
password
);
await
user
.
save
();
ctx
.
status
=
20
0
;
ctx
.
status
=
20
1
;
};
...
...
@@ -66,7 +66,7 @@ exports.login = async(ctx) => {
maxAge
:
1000
*
60
*
60
*
24
*
30
});
ctx
.
status
=
20
1
;
ctx
.
status
=
20
0
;
ctx
.
body
=
{
userId
};
...
...
@@ -80,5 +80,4 @@ exports.logout = async(ctx) => {
});
ctx
.
status
=
204
;
ctx
.
body
=
null
;
};
\ No newline at end of file
...
...
server/src/api/bottle/bottle.ctrl.js
View file @
1228fd4
...
...
@@ -3,14 +3,18 @@ const Bottle = require('../../models/bottle');
const
Hub
=
require
(
'../../models/hub'
);
const
Medicine
=
require
(
'../../models/medicine'
);
const
Mqtt
=
require
(
'../../lib/MqttModule'
);
const
jwt
=
require
(
'jsonwebtoken'
);
//약병 등록
exports
.
bottleConnect
=
async
(
ctx
)
=>
{
const
{
bottleId
,
hubId
}
=
ctx
.
request
.
body
;
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
newBottle
=
new
Bottle
({
bottleId
,
hubId
});
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
bottleId
,
hubId
}
=
ctx
.
request
.
body
;
const
isExistBottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
if
(
isExistBottle
)
{
...
...
@@ -23,23 +27,41 @@ exports.bottleConnect = async(ctx) => {
ctx
.
status
=
404
;
return
;
}
if
(
hub
.
getHub_UserId
()
!==
userId
)
{
ctx
.
status
=
403
;
return
;
}
const
hosting
=
await
hub
.
getHubHost
();
const
hosting
=
hub
.
getHubHost
();
if
(
!
hosting
)
{
ctx
.
status
=
404
;
return
;
}
const
newBottle
=
new
Bottle
({
bottleId
,
hubId
});
const
client
=
await
Mqtt
.
mqttOn
(
hosting
);
const
topic
=
'bottle/'
+
bottleId
+
'/bts'
;
const
topic
=
'bottle/'
+
newBottle
.
getBottleId
()
+
'/bts'
;
Mqtt
.
mqttSubscribe
(
client
,
topic
);
await
newBottle
.
save
();
ctx
.
status
=
20
0
;
ctx
.
status
=
20
1
;
};
//약병 등록 해제
exports
.
bottleDisconnect
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
bottleId
}
=
ctx
.
params
;
const
bottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
...
...
@@ -49,7 +71,12 @@ exports.bottleDisconnect = async(ctx) => {
}
const
hub
=
await
Hub
.
findByHubId
(
bottle
.
getHubId
());
const
hosting
=
await
hub
.
getHubHost
();
if
(
hub
.
getHub_UserId
()
!==
userId
)
{
ctx
.
status
=
403
;
return
;
}
const
hosting
=
hub
.
getHubHost
();
const
client
=
await
Mqtt
.
mqttOn
(
hosting
);
const
topic
=
'bottle/'
+
bottleId
+
'/bts'
;
...
...
@@ -57,11 +84,19 @@ exports.bottleDisconnect = async(ctx) => {
await
Bottle
.
deleteOne
({
bottleId
});
ctx
.
status
=
20
0
;
ctx
.
status
=
20
4
;
};
//약병 정보를 조회 -> 약병에 현재 데이터를 요청한다. message : req
exports
.
lookupInfo
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
bottleId
}
=
ctx
.
params
;
const
bottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
...
...
@@ -70,12 +105,32 @@ exports.lookupInfo = async(ctx) => {
return
;
}
const
hub
=
await
Hub
.
findByHubId
(
bottle
.
getHubId
());
if
(
hub
.
getHub_UserId
()
!==
userId
)
{
ctx
.
status
=
403
;
return
;
}
const
hosting
=
hub
.
getHubHost
();
//서버에서 bottle로 데이터를 요청한다.
const
client
=
await
Mqtt
.
mqttOn
(
hosting
);
const
topic
=
'bottle/'
+
bottleId
+
'/stb'
;
const
message
=
'req'
;
Mqtt
.
mqttPublishMessage
(
client
,
{
topic
,
message
});
ctx
.
status
=
200
;
ctx
.
body
=
bottle
;
}
//약병의 ID를 찾아서 약의 정보를 등록 : Post
exports
.
setMedicine
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
bottleId
}
=
ctx
.
params
;
const
{
medicineId
}
=
ctx
.
request
.
body
;
...
...
@@ -85,6 +140,12 @@ exports.setMedicine = async(ctx) => {
return
;
}
const
hub
=
await
Hub
.
findByHubId
(
bottle
.
getHubId
());
if
(
hub
.
getHub_UserId
()
!==
userId
)
{
ctx
.
status
=
403
;
return
;
}
const
medicine
=
await
Medicine
.
findByMedicineId
(
medicineId
);
if
(
!
medicine
)
{
ctx
.
status
=
404
;
...
...
server/src/api/hub/hub.ctrl.js
View file @
1228fd4
...
...
@@ -2,8 +2,16 @@
const
Hub
=
require
(
'../../models/hub'
);
const
Mqtt
=
require
(
'../../lib/MqttModule'
);
const
DataProcess
=
require
(
'../../lib/DataProcess'
);
const
jwt
=
require
(
'jsonwebtoken'
);
exports
.
hubConnect
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
hubId
,
host
,
port
}
=
ctx
.
request
.
body
;
const
isExistHub
=
await
Hub
.
findByHubId
(
hubId
);
...
...
@@ -21,16 +29,24 @@ exports.hubConnect = async (ctx) => {
const
hub
=
new
Hub
({
hubId
,
hosting
hosting
,
userId
});
await
hub
.
save
();
ctx
.
status
=
20
0
;
ctx
.
status
=
20
1
;
ctx
.
body
=
hub
;
};
exports
.
hubDisconnect
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
userId
}
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
const
{
hubId
}
=
ctx
.
params
;
const
hub
=
await
Hub
.
findByHubId
(
hubId
);
...
...
@@ -38,11 +54,15 @@ exports.hubDisconnect = async(ctx) => {
ctx
.
status
=
404
;
return
;
}
if
(
hub
.
getHub_UserId
()
!==
userId
)
{
ctx
.
status
=
403
;
return
;
}
const
hosting
=
await
hub
.
getHubHost
();
Mqtt
.
mqttOff
(
hosting
);
await
Hub
.
deleteOne
({
hubId
});
ctx
.
status
=
20
0
;
ctx
.
status
=
20
4
;
};
\ No newline at end of file
...
...
server/src/api/medicine/medicine.ctrl.js
View file @
1228fd4
//
해당하는 약의 정보를 불러오거나, 약을 검색
//
약의 정보를 검색하는 API : 약명, 제조사, 효능
const
Medicine
=
require
(
'../../models/medicine'
);
exports
.
medicineSearch
=
async
(
ctx
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
'access_token'
);
if
(
!
token
)
{
ctx
.
status
=
401
;
return
;
}
const
{
name
,
company
,
target
}
=
ctx
.
request
.
body
;
let
result
=
null
;
let
result
=
[]
;
if
(
name
&&
name
!==
''
&&
name
!==
undefined
)
result
=
await
medicineSearch_ByName
(
name
);
...
...
Please
register
or
login
to post a comment