박권수

feat. new logic

//어플에서 약병 등록 및, 약병에 관한 정보 조회 = 여기서 mqtt통신으로 broker에 데이터를 요청한다.
const Bottle = require('../../models/bottle');
const Hub = require('../../models/hub');
const Medicine = require('../../models/medicine');
const DataProcess = require('../../lib/DataProcess');
const Mqtt = require('../../lib/MqttModule');
exports.bottleRegister = async(ctx) => {
const { bottleId, hubId, topic } = ctx.request.body;
const { bottleId, hubId } = ctx.request.body;
const topic = 'bottle/' + String(bottleId) + '/bts';
const newBottle = new Bottle({
bottleId,
......@@ -30,12 +32,12 @@ exports.bottleRegister = async(ctx) => {
return;
}
const client = Mqtt.mqttOn({
const client = await Mqtt.mqttOn({
host : hosting.host,
port : hosting.port,
clientId : hosting.clientId
});
Mqtt.mqttSubscribe(client, topic);
Mqtt.mqttSubscribe(client, topic, DataProcess.dataPublish);
await newBottle.save();
......@@ -43,41 +45,33 @@ exports.bottleRegister = async(ctx) => {
};
exports.lookupInfo = async(ctx) => {
const { bottleId, topic } = ctx.request.body;
/** toDO
* 약병 데이터를 요청한다
* 1. Broker에 데이터 요청
* 2. Broker에게서 받은 데이터를
* 3. 가공한 후
* 4. 유저에게 http response
*/
const { bottleId } = ctx.params;
const bottle = await Bottle.findByBottleId(bottleId);
const hubId = await bottle.getHubId();
const hub = await Hub.findByHubId(hubId);
const hosting = await hub.getHubHost();
const client = await Mqtt.mqttOn({
host : hosting.host,
port : hosting.port,
clientId : hosting.clientId,
});
Mqtt.mqttSubscribe(client, topic);
const a = dataRequest(); //1.
const b = await getData();
const c = await dataProcess();
ctx.body = {
a,
b,
c
if(!bottle) {
ctx.status = 404;
return;
}
ctx.body = bottle;
}
//약병의 ID를 찾아서 약의 정보를 등록 : Post
exports.setMedicine = async(ctx) => {
const { medicineId, bottleId } = ctx.request.body;
const { bottleId } = ctx.params;
const { medicineId } = ctx.request.body;
const bottle = await Bottle.findByBottleId(bottleId);
if(!bottle) {
ctx.status = 404;
return;
}
const medicine = await Medicine.findByMedicineId(medicineId);
if(!medicine) {
ctx.status = 404;
return;
}
await Bottle.findOneAndUpdate({
bottleId
......
......@@ -4,7 +4,7 @@ const bottleCtrl = require('./bottle.ctrl');
const bottle = new Router();
bottle.post('/register', bottleCtrl.bottleRegister);
bottle.post('/lookupInfo', bottleCtrl.lookupInfo);
bottle.post('/setmedicine', bottleCtrl.setMedicine);
bottle.post('/lookupInfo/:bottleId', bottleCtrl.lookupInfo);
bottle.post('/setmedicine/:bottleId', bottleCtrl.setMedicine);
module.exports = bottle;
\ No newline at end of file
......
......@@ -3,7 +3,7 @@ const Hub = require('../../models/hub');
const Mqtt = require('../../lib/MqttModule');
exports.hubConnect = async (ctx) => {
const { host, port, hubId } = ctx.request.body;
const { hubId, host, port } = ctx.request.body;
const hosting = {
host,
......@@ -21,5 +21,16 @@ exports.hubConnect = async (ctx) => {
}
exports.hubDisconnect = async(ctx) => {
const { hubId } = ctx.params;
const hub = await Hub.findByHubId(hubId);
if(!hub) {
ctx.status = 404;
return;
}
const hosting = await hub.getHubHost();
Mqtt.mqttOff(hosting);
await Hub.deleteOne({ hubId });
}
\ No newline at end of file
......
......@@ -4,6 +4,6 @@ const hubCtrl = require('./hub.ctrl');
const hub = new Router();
hub.post('/connect', hubCtrl.hubConnect);
hub.post('/disconnect', hubCtrl.hubDisconnect);
hub.post('/disconnect/:hubId', hubCtrl.hubDisconnect);
module.exports = hub;
\ No newline at end of file
......