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:38:53 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ebff0cb15741a71e31bd779e5efe5d5506f37dce
ebff0cb1
1 parent
16aaa1c4
feat. new logic
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
35 deletions
server/src/api/bottle/bottle.ctrl.js
server/src/api/bottle/index.js
server/src/api/hub/hub.ctrl.js
server/src/api/hub/index.js
server/src/api/bottle/bottle.ctrl.js
View file @
ebff0cb
//어플에서 약병 등록 및, 약병에 관한 정보 조회 = 여기서 mqtt통신으로 broker에 데이터를 요청한다.
const
Bottle
=
require
(
'../../models/bottle'
);
const
Hub
=
require
(
'../../models/hub'
);
const
Medicine
=
require
(
'../../models/medicine'
);
const
DataProcess
=
require
(
'../../lib/DataProcess'
);
const
Mqtt
=
require
(
'../../lib/MqttModule'
);
exports
.
bottleRegister
=
async
(
ctx
)
=>
{
const
{
bottleId
,
hubId
,
topic
}
=
ctx
.
request
.
body
;
const
{
bottleId
,
hubId
}
=
ctx
.
request
.
body
;
const
topic
=
'bottle/'
+
String
(
bottleId
)
+
'/bts'
;
const
newBottle
=
new
Bottle
({
bottleId
,
...
...
@@ -30,12 +32,12 @@ exports.bottleRegister = async(ctx) => {
return
;
}
const
client
=
Mqtt
.
mqttOn
({
const
client
=
await
Mqtt
.
mqttOn
({
host
:
hosting
.
host
,
port
:
hosting
.
port
,
clientId
:
hosting
.
clientId
});
Mqtt
.
mqttSubscribe
(
client
,
topic
);
Mqtt
.
mqttSubscribe
(
client
,
topic
,
DataProcess
.
dataPublish
);
await
newBottle
.
save
();
...
...
@@ -43,41 +45,33 @@ exports.bottleRegister = async(ctx) => {
};
exports
.
lookupInfo
=
async
(
ctx
)
=>
{
const
{
bottleId
,
topic
}
=
ctx
.
request
.
body
;
/** toDO
* 약병 데이터를 요청한다
* 1. Broker에 데이터 요청
* 2. Broker에게서 받은 데이터를
* 3. 가공한 후
* 4. 유저에게 http response
*/
const
{
bottleId
}
=
ctx
.
params
;
const
bottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
const
hubId
=
await
bottle
.
getHubId
();
const
hub
=
await
Hub
.
findByHubId
(
hubId
);
const
hosting
=
await
hub
.
getHubHost
();
const
client
=
await
Mqtt
.
mqttOn
({
host
:
hosting
.
host
,
port
:
hosting
.
port
,
clientId
:
hosting
.
clientId
,
});
Mqtt
.
mqttSubscribe
(
client
,
topic
);
const
a
=
dataRequest
();
//1.
const
b
=
await
getData
();
const
c
=
await
dataProcess
();
ctx
.
body
=
{
a
,
b
,
c
if
(
!
bottle
)
{
ctx
.
status
=
404
;
return
;
}
ctx
.
body
=
bottle
;
}
//약병의 ID를 찾아서 약의 정보를 등록 : Post
exports
.
setMedicine
=
async
(
ctx
)
=>
{
const
{
medicineId
,
bottleId
}
=
ctx
.
request
.
body
;
const
{
bottleId
}
=
ctx
.
params
;
const
{
medicineId
}
=
ctx
.
request
.
body
;
const
bottle
=
await
Bottle
.
findByBottleId
(
bottleId
);
if
(
!
bottle
)
{
ctx
.
status
=
404
;
return
;
}
const
medicine
=
await
Medicine
.
findByMedicineId
(
medicineId
);
if
(
!
medicine
)
{
ctx
.
status
=
404
;
return
;
}
await
Bottle
.
findOneAndUpdate
({
bottleId
...
...
server/src/api/bottle/index.js
View file @
ebff0cb
...
...
@@ -4,7 +4,7 @@ const bottleCtrl = require('./bottle.ctrl');
const
bottle
=
new
Router
();
bottle
.
post
(
'/register'
,
bottleCtrl
.
bottleRegister
);
bottle
.
post
(
'/lookupInfo'
,
bottleCtrl
.
lookupInfo
);
bottle
.
post
(
'/setmedicine'
,
bottleCtrl
.
setMedicine
);
bottle
.
post
(
'/lookupInfo
/:bottleId
'
,
bottleCtrl
.
lookupInfo
);
bottle
.
post
(
'/setmedicine
/:bottleId
'
,
bottleCtrl
.
setMedicine
);
module
.
exports
=
bottle
;
\ No newline at end of file
...
...
server/src/api/hub/hub.ctrl.js
View file @
ebff0cb
...
...
@@ -3,7 +3,7 @@ const Hub = require('../../models/hub');
const
Mqtt
=
require
(
'../../lib/MqttModule'
);
exports
.
hubConnect
=
async
(
ctx
)
=>
{
const
{
h
ost
,
port
,
hubId
}
=
ctx
.
request
.
body
;
const
{
h
ubId
,
host
,
port
}
=
ctx
.
request
.
body
;
const
hosting
=
{
host
,
...
...
@@ -21,5 +21,16 @@ exports.hubConnect = async (ctx) => {
}
exports
.
hubDisconnect
=
async
(
ctx
)
=>
{
const
{
hubId
}
=
ctx
.
params
;
const
hub
=
await
Hub
.
findByHubId
(
hubId
);
if
(
!
hub
)
{
ctx
.
status
=
404
;
return
;
}
const
hosting
=
await
hub
.
getHubHost
();
Mqtt
.
mqttOff
(
hosting
);
await
Hub
.
deleteOne
({
hubId
});
}
\ No newline at end of file
...
...
server/src/api/hub/index.js
View file @
ebff0cb
...
...
@@ -4,6 +4,6 @@ const hubCtrl = require('./hub.ctrl');
const
hub
=
new
Router
();
hub
.
post
(
'/connect'
,
hubCtrl
.
hubConnect
);
hub
.
post
(
'/disconnect'
,
hubCtrl
.
hubDisconnect
);
hub
.
post
(
'/disconnect
/:hubId
'
,
hubCtrl
.
hubDisconnect
);
module
.
exports
=
hub
;
\ No newline at end of file
...
...
Please
register
or
login
to post a comment