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-10-10 20:45:43 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
646d1ba11848f79e98329e81745155620bcb4c3f
646d1ba1
2 parents
16dd47c1
695e0877
Merge branch 'server' into web
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
18 deletions
server/src/api/bottle/bottle.ctrl.js
server/src/util/DataProcess.js
server/src/util/MqttModule.js
server/src/api/bottle/bottle.ctrl.js
View file @
646d1ba
...
...
@@ -306,7 +306,6 @@ exports.setMedicine = async(ctx) => {
}
await
BottleMedicine
.
updateMany
({
bottleId
},
{
useYn
:
'N '
});
await
bottleMedicine
.
save
();
ctx
.
status
=
200
;
...
...
@@ -349,13 +348,16 @@ exports.setMedicineWeight = async ctx => {
}
//toDo : 약병에서 가져온 무게 데이터를 이용하여, bottleMedicine값을 갱신.
const
client
=
await
Mqtt
.
mqttOn
(
await
hub
.
getHubHost
());
const
topic
=
'bottle/'
+
bottleId
+
'/stb'
;
const
message
=
'weight'
;
await
Mqtt
.
mqttPublishMessage
(
client
,
{
topic
,
message
});
const
bottleMedicine
=
await
BottleMedicine
.
findOne
({
bottleId
,
useYn
:
'Y'
});
const
{
totalWeight
,
totalDosage
}
=
bottleMedicine
;
//
const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' });
//
const { totalWeight, totalDosage } = bottleMedicine;
// if(totalDosage) bottleMedicine.setEachWeight(totalWeight / totalDosage);
ctx
.
status
=
200
;
...
...
server/src/util/DataProcess.js
View file @
646d1ba
...
...
@@ -4,15 +4,24 @@ const TakeMedicineHist = require('../models/takeMedicineHistory');
//message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수
exports
.
dataPublish
=
async
(
topic
,
message
)
=>
{
//client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴
const
data
=
await
factoring
(
topic
,
message
);
//가공된 데이터를 bottleId의 약병에 업데이트
await
bottleInfoUpdate
(
data
);
//가공된 데이터를 메시지로 만들어 topic과 message 리턴
const
result
=
await
transPublishingTopicAndMessage
(
data
.
bottleId
);
if
(
message
.
includes
(
'weight'
))
{
console
.
log
(
'무게 갱신중'
);
//무게 갱신
const
result
=
await
updateBottleMedicineWeight
(
topic
,
message
);
return
result
;
return
result
;
}
else
{
//client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴
const
data
=
await
factoring
(
topic
,
message
);
//가공된 데이터를 bottleId의 약병에 업데이트
await
bottleInfoUpdate
(
data
);
//가공된 데이터를 메시지로 만들어 topic과 message 리턴
const
result
=
await
transPublishingTopicAndMessage
(
data
.
bottleId
);
return
result
;
}
};
//Hub topic : bottle/bottleId
...
...
@@ -72,7 +81,7 @@ const transPublishingTopicAndMessage = async(bottleId) => {
const
bottleMedicine
=
await
BottleMedicine
.
findOne
({
bottleId
,
useYn
:
'Y'
});
const
takeMedicineHistList
=
await
TakeMedicineHist
.
find
({
bmId
:
bottleMedicine
.
_id
}).
sort
({
takeDate
:
'
a
sc'
}).
limit
(
1
);
}).
sort
({
takeDate
:
'
de
sc'
}).
limit
(
1
);
const
message
=
'res/'
+
await
transDate
(
takeMedicineHistList
[
0
].
takeDate
)
+
'/'
+
takeMedicineHistList
[
0
].
dosage
;
...
...
@@ -86,4 +95,23 @@ const transPublishingTopicAndMessage = async(bottleId) => {
const
transDate
=
(
date
)
=>
{
return
(
date
.
getMonth
()
+
1
<
10
?
'0'
+
String
(
date
.
getMonth
()
+
1
)
:
String
(
date
.
getMonth
()
+
1
))
+
(
date
.
getDate
()
<
10
?
'0'
+
String
(
date
.
getDate
())
:
String
(
date
.
getDate
()));
}
\ No newline at end of file
};
//무게센서를 이용하여 데이터값을 갱신하는 함수
const
updateBottleMedicineWeight
=
async
(
topic
,
message
)
=>
{
const
bottleId
=
parseInt
(
topic
.
split
(
'/'
)[
1
]);
//message = weight/무게
const
totalWeight
=
parseFloat
(
message
.
split
(
'/'
)[
1
]);
const
bottleMedicine
=
await
BottleMedicine
.
findOne
({
bottleId
,
useYn
:
'Y'
});
const
totalDosage
=
parseInt
(
bottleMedicine
.
totalDosage
);
//받은 값으로 총 무게를 설정한 이후, 총 무게 / 총 복용량으로 개별 무게를 설정한다.
await
bottleMedicine
.
setTotalWeight
(
totalWeight
);
await
bottleMedicine
.
setEachWeight
(
totalWeight
/
totalDosage
);
await
bottleMedicine
.
save
();
return
null
;
};
\ No newline at end of file
...
...
server/src/util/MqttModule.js
View file @
646d1ba
const
mqtt
=
require
(
'mqtt'
)
const
clientList
=
[]
const
mqtt
=
require
(
'mqtt'
)
;
const
clientList
=
[]
;
exports
.
mqttOn
=
async
(
hosting
,
foo
)
=>
{
const
filterIndex
=
clientList
.
findIndex
(
client
=>
{
...
...
@@ -19,7 +19,7 @@ exports.mqttOn = async (hosting, foo) => {
client
.
on
(
'message'
,
async
(
topic
,
message
)
=>
{
const
result
=
await
foo
(
topic
,
message
.
toString
());
console
.
log
(
'\x1b[1;32msubscribe : topic'
,
topic
,
'message : '
,
message
.
toString
(),
'\x1b[0m'
)
this
.
mqttPublishMessage
(
client
,
result
);
if
(
result
)
this
.
mqttPublishMessage
(
client
,
result
);
});
return
client
;
...
...
@@ -29,7 +29,9 @@ exports.mqttOn = async (hosting, foo) => {
}
exports
.
mqttSubscribe
=
(
client
,
topic
)
=>
{
client
.
subscribe
(
topic
)
client
.
subscribe
(
topic
,
()
=>
{
console
.
log
(
'suscribe'
,
topic
);
});
}
exports
.
mqttPublishMessage
=
(
client
,
{
topic
,
message
})
=>
{
...
...
Please
register
or
login
to post a comment