Showing
2 changed files
with
55 additions
and
2 deletions
1 | //어플에서 약병 등록 및, 약병에 관한 정보 조회 = 여기서 mqtt통신으로 broker에 데이터를 요청한다. | 1 | //어플에서 약병 등록 및, 약병에 관한 정보 조회 = 여기서 mqtt통신으로 broker에 데이터를 요청한다. |
2 | -const Bottle = require('../../models/bottle') | 2 | +const Bottle = require('../../models/bottle'); |
3 | +const Hub = require('../../models/hub'); | ||
3 | const DataProcess = require('../../lib/DataProcess'); | 4 | const DataProcess = require('../../lib/DataProcess'); |
4 | -const MqttModule = require('../../lib/MqttModule'); | 5 | +const Mqtt = require('../../lib/MqttModule'); |
6 | + | ||
7 | +exports.bottleRegister = async(ctx) => { | ||
8 | + const { bottleId, hubId, topic } = ctx.request.body; | ||
9 | + | ||
10 | + const newBottle = new Bottle({ | ||
11 | + bottleId, | ||
12 | + hubId | ||
13 | + }); | ||
14 | + | ||
15 | + const isExistBottle = await Bottle.findByBottleId(bottleId); | ||
16 | + if(isExistBottle) { | ||
17 | + ctx.status = 409; | ||
18 | + return; | ||
19 | + } | ||
20 | + | ||
21 | + const hub = await Hub.findByHubId(hubId); | ||
22 | + if(!hub) { | ||
23 | + ctx.status = 404; | ||
24 | + return; | ||
25 | + } | ||
26 | + | ||
27 | + const hosting = await hub.getHubHost(); | ||
28 | + if(!hosting) { | ||
29 | + ctx.status = 404; | ||
30 | + return; | ||
31 | + } | ||
32 | + | ||
33 | + const client = Mqtt.mqttOn({ | ||
34 | + host : hosting.host, | ||
35 | + port : hosting.port, | ||
36 | + clientId : hosting.clientId | ||
37 | + }); | ||
38 | + Mqtt.mqttSubscribe(client, topic); | ||
39 | + | ||
40 | + await newBottle.save(); | ||
41 | + | ||
42 | + ctx.status = 200; | ||
43 | +}; | ||
5 | 44 | ||
6 | exports.lookupInfo = async(ctx) => { | 45 | exports.lookupInfo = async(ctx) => { |
46 | + const { bottleId, topic } = ctx.request.body; | ||
7 | /** toDO | 47 | /** toDO |
8 | * 약병 데이터를 요청한다 | 48 | * 약병 데이터를 요청한다 |
9 | * 1. Broker에 데이터 요청 | 49 | * 1. Broker에 데이터 요청 |
... | @@ -12,6 +52,18 @@ exports.lookupInfo = async(ctx) => { | ... | @@ -12,6 +52,18 @@ exports.lookupInfo = async(ctx) => { |
12 | * 4. 유저에게 http response | 52 | * 4. 유저에게 http response |
13 | */ | 53 | */ |
14 | 54 | ||
55 | + const bottle = await Bottle.findByBottleId(bottleId); | ||
56 | + const hubId = await bottle.getHubId(); | ||
57 | + const hub = await Hub.findByHubId(hubId); | ||
58 | + const hosting = await hub.getHubHost(); | ||
59 | + | ||
60 | + const client = await Mqtt.mqttOn({ | ||
61 | + host : hosting.host, | ||
62 | + port : hosting.port, | ||
63 | + clientId : hosting.clientId, | ||
64 | + }); | ||
65 | + Mqtt.mqttSubscribe(client, topic); | ||
66 | + | ||
15 | const a = dataRequest(); //1. | 67 | const a = dataRequest(); //1. |
16 | const b = await getData(); | 68 | const b = await getData(); |
17 | const c = await dataProcess(); | 69 | const c = await dataProcess(); | ... | ... |
... | @@ -3,6 +3,7 @@ const bottleCtrl = require('./bottle.ctrl'); | ... | @@ -3,6 +3,7 @@ const bottleCtrl = require('./bottle.ctrl'); |
3 | 3 | ||
4 | const bottle = new Router(); | 4 | const bottle = new Router(); |
5 | 5 | ||
6 | +bottle.post('/register', bottleCtrl.bottleRegister); | ||
6 | bottle.post('/lookupInfo', bottleCtrl.lookupInfo); | 7 | bottle.post('/lookupInfo', bottleCtrl.lookupInfo); |
7 | bottle.post('/setmedicine', bottleCtrl.setMedicine); | 8 | bottle.post('/setmedicine', bottleCtrl.setMedicine); |
8 | 9 | ... | ... |
-
Please register or login to post a comment