Showing
4 changed files
with
40 additions
and
35 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 Hub = require('../../models/hub'); |
| 4 | +const Medicine = require('../../models/medicine'); | ||
| 4 | const DataProcess = require('../../lib/DataProcess'); | 5 | const DataProcess = require('../../lib/DataProcess'); |
| 5 | const Mqtt = require('../../lib/MqttModule'); | 6 | const Mqtt = require('../../lib/MqttModule'); |
| 6 | 7 | ||
| 7 | exports.bottleRegister = async(ctx) => { | 8 | exports.bottleRegister = async(ctx) => { |
| 8 | - const { bottleId, hubId, topic } = ctx.request.body; | 9 | + const { bottleId, hubId } = ctx.request.body; |
| 10 | + const topic = 'bottle/' + String(bottleId) + '/bts'; | ||
| 9 | 11 | ||
| 10 | const newBottle = new Bottle({ | 12 | const newBottle = new Bottle({ |
| 11 | bottleId, | 13 | bottleId, |
| ... | @@ -30,12 +32,12 @@ exports.bottleRegister = async(ctx) => { | ... | @@ -30,12 +32,12 @@ exports.bottleRegister = async(ctx) => { |
| 30 | return; | 32 | return; |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | - const client = Mqtt.mqttOn({ | 35 | + const client = await Mqtt.mqttOn({ |
| 34 | host : hosting.host, | 36 | host : hosting.host, |
| 35 | port : hosting.port, | 37 | port : hosting.port, |
| 36 | clientId : hosting.clientId | 38 | clientId : hosting.clientId |
| 37 | }); | 39 | }); |
| 38 | - Mqtt.mqttSubscribe(client, topic); | 40 | + Mqtt.mqttSubscribe(client, topic, DataProcess.dataPublish); |
| 39 | 41 | ||
| 40 | await newBottle.save(); | 42 | await newBottle.save(); |
| 41 | 43 | ||
| ... | @@ -43,41 +45,33 @@ exports.bottleRegister = async(ctx) => { | ... | @@ -43,41 +45,33 @@ exports.bottleRegister = async(ctx) => { |
| 43 | }; | 45 | }; |
| 44 | 46 | ||
| 45 | exports.lookupInfo = async(ctx) => { | 47 | exports.lookupInfo = async(ctx) => { |
| 46 | - const { bottleId, topic } = ctx.request.body; | 48 | + const { bottleId } = ctx.params; |
| 47 | - /** toDO | ||
| 48 | - * 약병 데이터를 요청한다 | ||
| 49 | - * 1. Broker에 데이터 요청 | ||
| 50 | - * 2. Broker에게서 받은 데이터를 | ||
| 51 | - * 3. 가공한 후 | ||
| 52 | - * 4. 유저에게 http response | ||
| 53 | - */ | ||
| 54 | 49 | ||
| 55 | const bottle = await Bottle.findByBottleId(bottleId); | 50 | const bottle = await Bottle.findByBottleId(bottleId); |
| 56 | - const hubId = await bottle.getHubId(); | 51 | + if(!bottle) { |
| 57 | - const hub = await Hub.findByHubId(hubId); | 52 | + ctx.status = 404; |
| 58 | - const hosting = await hub.getHubHost(); | 53 | + return; |
| 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 | - | ||
| 67 | - const a = dataRequest(); //1. | ||
| 68 | - const b = await getData(); | ||
| 69 | - const c = await dataProcess(); | ||
| 70 | - | ||
| 71 | - ctx.body = { | ||
| 72 | - a, | ||
| 73 | - b, | ||
| 74 | - c | ||
| 75 | } | 54 | } |
| 55 | + | ||
| 56 | + ctx.body = bottle; | ||
| 76 | } | 57 | } |
| 77 | 58 | ||
| 78 | //약병의 ID를 찾아서 약의 정보를 등록 : Post | 59 | //약병의 ID를 찾아서 약의 정보를 등록 : Post |
| 79 | exports.setMedicine = async(ctx) => { | 60 | exports.setMedicine = async(ctx) => { |
| 80 | - const { medicineId, bottleId } = ctx.request.body; | 61 | + const { bottleId } = ctx.params; |
| 62 | + const { medicineId } = ctx.request.body; | ||
| 63 | + | ||
| 64 | + const bottle = await Bottle.findByBottleId(bottleId); | ||
| 65 | + if(!bottle) { | ||
| 66 | + ctx.status = 404; | ||
| 67 | + return; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + const medicine = await Medicine.findByMedicineId(medicineId); | ||
| 71 | + if(!medicine) { | ||
| 72 | + ctx.status = 404; | ||
| 73 | + return; | ||
| 74 | + } | ||
| 81 | 75 | ||
| 82 | await Bottle.findOneAndUpdate({ | 76 | await Bottle.findOneAndUpdate({ |
| 83 | bottleId | 77 | bottleId | ... | ... |
| ... | @@ -4,7 +4,7 @@ const bottleCtrl = require('./bottle.ctrl'); | ... | @@ -4,7 +4,7 @@ const bottleCtrl = require('./bottle.ctrl'); |
| 4 | const bottle = new Router(); | 4 | const bottle = new Router(); |
| 5 | 5 | ||
| 6 | bottle.post('/register', bottleCtrl.bottleRegister); | 6 | bottle.post('/register', bottleCtrl.bottleRegister); |
| 7 | -bottle.post('/lookupInfo', bottleCtrl.lookupInfo); | 7 | +bottle.post('/lookupInfo/:bottleId', bottleCtrl.lookupInfo); |
| 8 | -bottle.post('/setmedicine', bottleCtrl.setMedicine); | 8 | +bottle.post('/setmedicine/:bottleId', bottleCtrl.setMedicine); |
| 9 | 9 | ||
| 10 | module.exports = bottle; | 10 | module.exports = bottle; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -3,7 +3,7 @@ const Hub = require('../../models/hub'); | ... | @@ -3,7 +3,7 @@ const Hub = require('../../models/hub'); |
| 3 | const Mqtt = require('../../lib/MqttModule'); | 3 | const Mqtt = require('../../lib/MqttModule'); |
| 4 | 4 | ||
| 5 | exports.hubConnect = async (ctx) => { | 5 | exports.hubConnect = async (ctx) => { |
| 6 | - const { host, port, hubId } = ctx.request.body; | 6 | + const { hubId, host, port } = ctx.request.body; |
| 7 | 7 | ||
| 8 | const hosting = { | 8 | const hosting = { |
| 9 | host, | 9 | host, |
| ... | @@ -21,5 +21,16 @@ exports.hubConnect = async (ctx) => { | ... | @@ -21,5 +21,16 @@ exports.hubConnect = async (ctx) => { |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | exports.hubDisconnect = async(ctx) => { | 23 | exports.hubDisconnect = async(ctx) => { |
| 24 | + const { hubId } = ctx.params; | ||
| 24 | 25 | ||
| 26 | + const hub = await Hub.findByHubId(hubId); | ||
| 27 | + if(!hub) { | ||
| 28 | + ctx.status = 404; | ||
| 29 | + return; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + const hosting = await hub.getHubHost(); | ||
| 33 | + Mqtt.mqttOff(hosting); | ||
| 34 | + | ||
| 35 | + await Hub.deleteOne({ hubId }); | ||
| 25 | } | 36 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -4,6 +4,6 @@ const hubCtrl = require('./hub.ctrl'); | ... | @@ -4,6 +4,6 @@ const hubCtrl = require('./hub.ctrl'); |
| 4 | const hub = new Router(); | 4 | const hub = new Router(); |
| 5 | 5 | ||
| 6 | hub.post('/connect', hubCtrl.hubConnect); | 6 | hub.post('/connect', hubCtrl.hubConnect); |
| 7 | -hub.post('/disconnect', hubCtrl.hubDisconnect); | 7 | +hub.post('/disconnect/:hubId', hubCtrl.hubDisconnect); |
| 8 | 8 | ||
| 9 | module.exports = hub; | 9 | module.exports = hub; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment