박권수

fix. data processing

......@@ -5,7 +5,6 @@ const TakeMedicineHist = require('../models/takeMedicineHistory');
//message subscribe 후 message를 가공한 이후 해당 데이터를 보낼 topic과 message를 리턴하는 함수
exports.dataPublish = async (topic, message) => {
if(message.includes('weight')) {
console.log('무게 갱신중');
//무게 갱신
const result = await updateBottleMedicineWeight(topic, message);
......@@ -29,7 +28,7 @@ exports.dataPublish = async (topic, message) => {
const factoring = async (topic, message) => {
const bottleId = parseInt(topic.split('/')[1]);
const data = message.split('/');
const [isOpen, humidity, totalWeight, temperature] = data;
const [isOpen, temperature, totalWeight, humidity] = data;
return {
bottleId,
......@@ -45,21 +44,21 @@ const factoring = async (topic, message) => {
const bottleInfoUpdate = async(data) => {
let { bottleId, isOpen, temperature, humidity, totalWeight } = data;
if(!parseInt(isOpen)) {
bottleId = parseInt(bottleId);
isOpen = parseInt(isOpen);
temperature = parseFloat(temperature).toFixed(1);
humidity = parseFloat(humidity).toFixed(1);
totalWeight = parseFloat(totalWeight).toFixed(2);
temperature = parseFloat(temperature);
humidity = parseFloat(humidity);
totalWeight = parseFloat(totalWeight);
const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' });
if(bottleMedicine) {
const lastTotalWeight = parseFloat(bottleMedicine.totalWeight);
if(isOpen) {
const { eachWeight } = bottleMedicine;
const dosage = Math.round((lastTotalWeight - totalWeight) / parseFloat(eachWeight));
if(dosage > 0) {
const takeMedicineHist = new TakeMedicineHist({
bmId : bottleMedicine._id,
temperature,
......@@ -72,6 +71,7 @@ const bottleInfoUpdate = async(data) => {
await bottleMedicine.setTotalWeight(totalWeight);
await bottleMedicine.save();
}
}
}
//해당 MQTT Broker(client)에 bottleId의 정보에 관한 topic과 message를 리턴한다.
......@@ -83,7 +83,9 @@ const transPublishingTopicAndMessage = async(bottleId) => {
bmId : bottleMedicine._id
}).sort({ takeDate : 'desc' }).limit(1);
const message = 'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage;
const message = takeMedicineHistList && takeMedicineHistList[0] ?
'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage :
'res/' + await transDate(new Date()) + '/' + 0;
return {
topic,
......