Showing
3 changed files
with
41 additions
and
16 deletions
1 | const Bottle = require('../models/bottle'); | 1 | const Bottle = require('../models/bottle'); |
2 | 2 | ||
3 | +//message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수 | ||
4 | +exports.dataPublish = async (topic, message) => { | ||
5 | + //client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴 | ||
6 | + const data = await factoring(topic, message); | ||
7 | + const { bottleId } = data; | ||
8 | + | ||
9 | + //가공된 데이터를 bottleId의 약병에 업데이트 | ||
10 | + await bottleInfoUpdate(data); | ||
11 | + //가공된 데이터를 메시지로 만들어 topic과 message 리턴 | ||
12 | + const result = await transPublishingTopicAndMessage(bottleId); | ||
13 | + | ||
14 | + return result; | ||
15 | +}; | ||
16 | + | ||
3 | //Hub topic : bottle/bottleId | 17 | //Hub topic : bottle/bottleId |
4 | //Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서 | 18 | //Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서 |
5 | -exports.factoring = (topic, message) => { | 19 | +const factoring = (topic, message) => { |
6 | const bottleId = parseInt(topic.split('/')[1]); | 20 | const bottleId = parseInt(topic.split('/')[1]); |
7 | const data = message.split('/'); | 21 | const data = message.split('/'); |
8 | const [isOpen, temperature, humidity, balance] = data; | 22 | const [isOpen, temperature, humidity, balance] = data; |
... | @@ -20,7 +34,7 @@ exports.factoring = (topic, message) => { | ... | @@ -20,7 +34,7 @@ exports.factoring = (topic, message) => { |
20 | } | 34 | } |
21 | 35 | ||
22 | //bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다. | 36 | //bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다. |
23 | -exports.bottleInfoUpdate = async(data) => { | 37 | +const bottleInfoUpdate = async(data) => { |
24 | const { bottleId, isOpen, openDate, temperature, humidity, balance } = data; | 38 | const { bottleId, isOpen, openDate, temperature, humidity, balance } = data; |
25 | if(isOpen === '1') { | 39 | if(isOpen === '1') { |
26 | await Bottle.findOneAndUpdate({ | 40 | await Bottle.findOneAndUpdate({ |
... | @@ -30,7 +44,7 @@ exports.bottleInfoUpdate = async(data) => { | ... | @@ -30,7 +44,7 @@ exports.bottleInfoUpdate = async(data) => { |
30 | }); | 44 | }); |
31 | } | 45 | } |
32 | 46 | ||
33 | - await Bottle.findByIdAndUpdate({ | 47 | + await Bottle.findOneAndUpdate({ |
34 | bottleId | 48 | bottleId |
35 | }, { | 49 | }, { |
36 | temperature, | 50 | temperature, |
... | @@ -39,18 +53,19 @@ exports.bottleInfoUpdate = async(data) => { | ... | @@ -39,18 +53,19 @@ exports.bottleInfoUpdate = async(data) => { |
39 | }, { new : true }); | 53 | }, { new : true }); |
40 | } | 54 | } |
41 | 55 | ||
42 | -//해당 MQTT Broker(client)에 bottleId의 정보에 관한 message를 발행한다. | 56 | +//해당 MQTT Broker(client)에 bottleId의 정보에 관한 topic과 message를 리턴한다. |
43 | -exports.dataPublishing = async(client, bottleId) => { | 57 | +const transPublishingTopicAndMessage = async(bottleId) => { |
44 | - const topic = 'bottle/'.concat(bottleId); | 58 | + const topic = 'bottle/'.concat(bottleId) + '/stb'; |
45 | 59 | ||
46 | const bottle = await Bottle.findByBottleId(bottleId); | 60 | const bottle = await Bottle.findByBottleId(bottleId); |
47 | const recentOpen = await bottle.getRecentOpenDate(); | 61 | const recentOpen = await bottle.getRecentOpenDate(); |
48 | 62 | ||
49 | const message = await transDate(recentOpen); | 63 | const message = await transDate(recentOpen); |
50 | 64 | ||
51 | - client.publish(topic, message, () => { | 65 | + return { |
52 | - console.log('topic : ', topic, 'message : ', message); | 66 | + topic, |
53 | - }) | 67 | + message |
68 | + } | ||
54 | } | 69 | } |
55 | 70 | ||
56 | //날짜를 yymmdd로 변환해주는 함수 | 71 | //날짜를 yymmdd로 변환해주는 함수 | ... | ... |
... | @@ -21,14 +21,24 @@ exports.mqttOn = async (hosting) => { | ... | @@ -21,14 +21,24 @@ exports.mqttOn = async (hosting) => { |
21 | }; | 21 | }; |
22 | }; | 22 | }; |
23 | 23 | ||
24 | -exports.mqttSubscribe = (client, topic) => { | 24 | +exports.mqttSubscribe = (client, topic, func) => { |
25 | client.subscribe(topic); | 25 | client.subscribe(topic); |
26 | - client.on('message', (topic, message, packet) => { | 26 | + client.on('message', async (topic, message, packet) => { |
27 | - console.log('\x1b[1;37mtopic : ', topic, '\x1b[0m'); | 27 | + const result = await func(topic, message.toString()); |
28 | - console.log('\x1b[1;37mmessage : ', message.toString(), '\x1b[0m', '\n'); | 28 | + this.mqttPublishMessage(client, result); |
29 | }); | 29 | }); |
30 | }; | 30 | }; |
31 | 31 | ||
32 | -exports.mqttPublishMessage = (client, topic, message) => { | 32 | +exports.mqttPublishMessage = (client, { topic, message }) => { |
33 | client.publish(topic, message, () => {}); | 33 | client.publish(topic, message, () => {}); |
34 | }; | 34 | }; |
35 | + | ||
36 | +exports.mqttOff = (hosting) => { | ||
37 | + const filterIndex = clientList.findIndex(client => { | ||
38 | + return (client.options.clientId === hosting.clientId | ||
39 | + && client.options.host === hosting.host | ||
40 | + && client.options.port === hosting.port) | ||
41 | + }); | ||
42 | + clientList[filterIndex].end(); | ||
43 | + clientList.splice(filterIndex, 1); | ||
44 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -14,13 +14,13 @@ const jwtMiddleware = async (ctx, next) => { | ... | @@ -14,13 +14,13 @@ const jwtMiddleware = async (ctx, next) => { |
14 | userId : decoded.userId | 14 | userId : decoded.userId |
15 | }; | 15 | }; |
16 | const now = Math.floor(Date.now() / 1000); | 16 | const now = Math.floor(Date.now() / 1000); |
17 | - if (decoded.exp - now < 60 * 60 * 24 * 3.5) { | 17 | + if (decoded.exp - now < 60 * 60 * 24 * 7) { |
18 | const user = await User.findById(decoded._id); | 18 | const user = await User.findById(decoded._id); |
19 | const token = user.generateToken(); | 19 | const token = user.generateToken(); |
20 | 20 | ||
21 | ctx.cookies.set('access_token', token, { | 21 | ctx.cookies.set('access_token', token, { |
22 | httpOnly : true, | 22 | httpOnly : true, |
23 | - maxAge : 1000 * 60 * 60 * 24 * 7 | 23 | + maxAge : 1000 * 60 * 60 * 24 * 30 |
24 | }) | 24 | }) |
25 | } | 25 | } |
26 | 26 | ... | ... |
-
Please register or login to post a comment