DataProcess.js
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Bottle = require('../models/bottle');
//Hub topic : bottle/bottleId
//Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서
exports.factoring = (topic, message) => {
const bottleId = parseInt(topic.split('/')[1]);
const data = message.split('/');
const [isOpen, temperature, humidity, balance] = data;
const openDate = new Date();
return {
bottleId,
isOpen,
openDate,
temperature,
humidity,
balance
};
}
//bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다.
exports.bottleInfoUpdate = async(data) => {
const { bottleId, isOpen, openDate, temperature, humidity, balance } = data;
if(isOpen === '1') {
await Bottle.findOneAndUpdate({
bottleId
}, { recentOpen : openDate }, {
new : true
});
}
await Bottle.findByIdAndUpdate({
bottleId
}, {
temperature,
humidity,
balance
}, { new : true });
}
//해당 MQTT Broker(client)에 bottleId의 정보에 관한 message를 발행한다.
exports.dataPublishing = async(client, bottleId) => {
const topic = 'bottle/'.concat(bottleId);
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);
})
}
//날짜를 yymmdd로 변환해주는 함수
const transDate = (date) => {
return String(date.getFullYear()).substr(2, 2)
+ (date.getMonth() + 1 < 10 ? '0' + String(date.getMonth() + 1) : String(date.getMonth() + 1))
+ (date.getDate() < 10 ? '0' + String(date.getDate()) : String(date.getDate()));
}