Showing
3 changed files
with
50 additions
and
18 deletions
| ... | @@ -306,7 +306,6 @@ exports.setMedicine = async(ctx) => { | ... | @@ -306,7 +306,6 @@ exports.setMedicine = async(ctx) => { |
| 306 | } | 306 | } |
| 307 | 307 | ||
| 308 | await BottleMedicine.updateMany({ bottleId }, { useYn : 'N '}); | 308 | await BottleMedicine.updateMany({ bottleId }, { useYn : 'N '}); |
| 309 | - | ||
| 310 | await bottleMedicine.save(); | 309 | await bottleMedicine.save(); |
| 311 | 310 | ||
| 312 | ctx.status = 200; | 311 | ctx.status = 200; |
| ... | @@ -349,13 +348,16 @@ exports.setMedicineWeight = async ctx => { | ... | @@ -349,13 +348,16 @@ exports.setMedicineWeight = async ctx => { |
| 349 | } | 348 | } |
| 350 | 349 | ||
| 351 | 350 | ||
| 352 | - | ||
| 353 | //toDo : 약병에서 가져온 무게 데이터를 이용하여, bottleMedicine값을 갱신. | 351 | //toDo : 약병에서 가져온 무게 데이터를 이용하여, bottleMedicine값을 갱신. |
| 352 | + const client = await Mqtt.mqttOn(await hub.getHubHost()); | ||
| 353 | + const topic = 'bottle/' + bottleId + '/stb'; | ||
| 354 | + const message = 'weight'; | ||
| 355 | + await Mqtt.mqttPublishMessage(client, { topic, message }); | ||
| 354 | 356 | ||
| 355 | 357 | ||
| 356 | - const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); | 358 | + // const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); |
| 357 | - const { totalWeight, totalDosage } = bottleMedicine; | 359 | + // const { totalWeight, totalDosage } = bottleMedicine; |
| 358 | - | 360 | + // if(totalDosage) bottleMedicine.setEachWeight(totalWeight / totalDosage); |
| 359 | 361 | ||
| 360 | ctx.status = 200; | 362 | ctx.status = 200; |
| 361 | 363 | ... | ... |
| ... | @@ -4,15 +4,24 @@ const TakeMedicineHist = require('../models/takeMedicineHistory'); | ... | @@ -4,15 +4,24 @@ const TakeMedicineHist = require('../models/takeMedicineHistory'); |
| 4 | 4 | ||
| 5 | //message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수 | 5 | //message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수 |
| 6 | exports.dataPublish = async (topic, message) => { | 6 | exports.dataPublish = async (topic, message) => { |
| 7 | - //client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴 | 7 | + if(message.includes('weight')) { |
| 8 | - const data = await factoring(topic, message); | 8 | + console.log('무게 갱신중'); |
| 9 | - //가공된 데이터를 bottleId의 약병에 업데이트 | 9 | + //무게 갱신 |
| 10 | - await bottleInfoUpdate(data); | 10 | + const result = await updateBottleMedicineWeight(topic, message); |
| 11 | - //가공된 데이터를 메시지로 만들어 topic과 message 리턴 | ||
| 12 | - const result = await transPublishingTopicAndMessage(data.bottleId); | ||
| 13 | 11 | ||
| 14 | - return result; | 12 | + return result; |
| 15 | 13 | ||
| 14 | + } else { | ||
| 15 | + //client가 subscribe를 하면 메시지를 보낸 약병의 topic과 message를 가공 및 보낸 약병의 bottleId를 가져옴 | ||
| 16 | + const data = await factoring(topic, message); | ||
| 17 | + //가공된 데이터를 bottleId의 약병에 업데이트 | ||
| 18 | + await bottleInfoUpdate(data); | ||
| 19 | + //가공된 데이터를 메시지로 만들어 topic과 message 리턴 | ||
| 20 | + const result = await transPublishingTopicAndMessage(data.bottleId); | ||
| 21 | + | ||
| 22 | + return result; | ||
| 23 | + | ||
| 24 | + } | ||
| 16 | }; | 25 | }; |
| 17 | 26 | ||
| 18 | //Hub topic : bottle/bottleId | 27 | //Hub topic : bottle/bottleId |
| ... | @@ -72,7 +81,7 @@ const transPublishingTopicAndMessage = async(bottleId) => { | ... | @@ -72,7 +81,7 @@ const transPublishingTopicAndMessage = async(bottleId) => { |
| 72 | const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); | 81 | const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); |
| 73 | const takeMedicineHistList = await TakeMedicineHist.find({ | 82 | const takeMedicineHistList = await TakeMedicineHist.find({ |
| 74 | bmId : bottleMedicine._id | 83 | bmId : bottleMedicine._id |
| 75 | - }).sort({ takeDate : 'asc' }).limit(1); | 84 | + }).sort({ takeDate : 'desc' }).limit(1); |
| 76 | 85 | ||
| 77 | const message = 'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage; | 86 | const message = 'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage; |
| 78 | 87 | ||
| ... | @@ -86,4 +95,23 @@ const transPublishingTopicAndMessage = async(bottleId) => { | ... | @@ -86,4 +95,23 @@ const transPublishingTopicAndMessage = async(bottleId) => { |
| 86 | const transDate = (date) => { | 95 | const transDate = (date) => { |
| 87 | return (date.getMonth() + 1 < 10 ? '0' + String(date.getMonth() + 1) : String(date.getMonth() + 1)) | 96 | return (date.getMonth() + 1 < 10 ? '0' + String(date.getMonth() + 1) : String(date.getMonth() + 1)) |
| 88 | + (date.getDate() < 10 ? '0' + String(date.getDate()) : String(date.getDate())); | 97 | + (date.getDate() < 10 ? '0' + String(date.getDate()) : String(date.getDate())); |
| 89 | -} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 98 | +}; | ||
| 99 | + | ||
| 100 | + | ||
| 101 | +//무게센서를 이용하여 데이터값을 갱신하는 함수 | ||
| 102 | +const updateBottleMedicineWeight = async (topic, message) => { | ||
| 103 | + const bottleId = parseInt(topic.split('/')[1]); | ||
| 104 | + //message = weight/무게 | ||
| 105 | + const totalWeight = parseFloat(message.split('/')[1]); | ||
| 106 | + | ||
| 107 | + const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); | ||
| 108 | + const totalDosage = parseInt(bottleMedicine.totalDosage); | ||
| 109 | + //받은 값으로 총 무게를 설정한 이후, 총 무게 / 총 복용량으로 개별 무게를 설정한다. | ||
| 110 | + await bottleMedicine.setTotalWeight(totalWeight); | ||
| 111 | + await bottleMedicine.setEachWeight(totalWeight / totalDosage); | ||
| 112 | + | ||
| 113 | + await bottleMedicine.save(); | ||
| 114 | + | ||
| 115 | + return null; | ||
| 116 | + | ||
| 117 | +}; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | -const mqtt = require('mqtt') | 1 | +const mqtt = require('mqtt'); |
| 2 | -const clientList = [] | 2 | +const clientList = []; |
| 3 | 3 | ||
| 4 | exports.mqttOn = async (hosting, foo) => { | 4 | exports.mqttOn = async (hosting, foo) => { |
| 5 | const filterIndex = clientList.findIndex(client => { | 5 | const filterIndex = clientList.findIndex(client => { |
| ... | @@ -19,7 +19,7 @@ exports.mqttOn = async (hosting, foo) => { | ... | @@ -19,7 +19,7 @@ exports.mqttOn = async (hosting, foo) => { |
| 19 | client.on('message', async (topic, message) => { | 19 | client.on('message', async (topic, message) => { |
| 20 | const result = await foo(topic, message.toString()); | 20 | const result = await foo(topic, message.toString()); |
| 21 | console.log('\x1b[1;32msubscribe : topic', topic, 'message : ', message.toString(), '\x1b[0m') | 21 | console.log('\x1b[1;32msubscribe : topic', topic, 'message : ', message.toString(), '\x1b[0m') |
| 22 | - this.mqttPublishMessage(client, result); | 22 | + if(result) this.mqttPublishMessage(client, result); |
| 23 | }); | 23 | }); |
| 24 | 24 | ||
| 25 | return client; | 25 | return client; |
| ... | @@ -29,7 +29,9 @@ exports.mqttOn = async (hosting, foo) => { | ... | @@ -29,7 +29,9 @@ exports.mqttOn = async (hosting, foo) => { |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | exports.mqttSubscribe = (client, topic) => { | 31 | exports.mqttSubscribe = (client, topic) => { |
| 32 | - client.subscribe(topic) | 32 | + client.subscribe(topic, () => { |
| 33 | + console.log('suscribe', topic); | ||
| 34 | + }); | ||
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | exports.mqttPublishMessage = (client, { topic, message }) => { | 37 | exports.mqttPublishMessage = (client, { topic, message }) => { | ... | ... |
-
Please register or login to post a comment