박권수

feat. data processing 수정 및 에러 수정

...@@ -5,7 +5,6 @@ const TakeMedicineHist = require('../models/takeMedicineHistory'); ...@@ -5,7 +5,6 @@ const TakeMedicineHist = require('../models/takeMedicineHistory');
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 if(message.includes('weight')) { 7 if(message.includes('weight')) {
8 - console.log('무게 갱신중');
9 //무게 갱신 8 //무게 갱신
10 const result = await updateBottleMedicineWeight(topic, message); 9 const result = await updateBottleMedicineWeight(topic, message);
11 10
...@@ -29,7 +28,7 @@ exports.dataPublish = async (topic, message) => { ...@@ -29,7 +28,7 @@ exports.dataPublish = async (topic, message) => {
29 const factoring = async (topic, message) => { 28 const factoring = async (topic, message) => {
30 const bottleId = parseInt(topic.split('/')[1]); 29 const bottleId = parseInt(topic.split('/')[1]);
31 const data = message.split('/'); 30 const data = message.split('/');
32 - const [isOpen, humidity, totalWeight, temperature] = data; 31 + const [isOpen, temperature, totalWeight, humidity] = data;
33 32
34 return { 33 return {
35 bottleId, 34 bottleId,
...@@ -45,32 +44,33 @@ const factoring = async (topic, message) => { ...@@ -45,32 +44,33 @@ const factoring = async (topic, message) => {
45 const bottleInfoUpdate = async(data) => { 44 const bottleInfoUpdate = async(data) => {
46 let { bottleId, isOpen, temperature, humidity, totalWeight } = data; 45 let { bottleId, isOpen, temperature, humidity, totalWeight } = data;
47 46
48 - bottleId = parseInt(bottleId); 47 + if(!parseInt(isOpen)) {
49 - isOpen = parseInt(isOpen); 48 + bottleId = parseInt(bottleId);
50 - temperature = parseFloat(temperature); 49 + temperature = parseFloat(temperature);
51 - humidity = parseFloat(humidity); 50 + humidity = parseFloat(humidity);
52 - totalWeight = parseFloat(totalWeight); 51 + totalWeight = parseFloat(totalWeight);
52 +
53 + const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' });
53 54
54 - const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); 55 + if(bottleMedicine) {
55 - 56 + const lastTotalWeight = parseFloat(bottleMedicine.totalWeight);
56 - if(bottleMedicine) {
57 - const lastTotalWeight = parseFloat(bottleMedicine.totalWeight);
58 -
59 - if(isOpen) {
60 const { eachWeight } = bottleMedicine; 57 const { eachWeight } = bottleMedicine;
58 +
61 const dosage = Math.round((lastTotalWeight - totalWeight) / parseFloat(eachWeight)); 59 const dosage = Math.round((lastTotalWeight - totalWeight) / parseFloat(eachWeight));
62 60
63 - const takeMedicineHist = new TakeMedicineHist({ 61 + if(dosage > 0) {
64 - bmId : bottleMedicine._id, 62 + const takeMedicineHist = new TakeMedicineHist({
65 - temperature, 63 + bmId : bottleMedicine._id,
66 - humidity, 64 + temperature,
67 - dosage, 65 + humidity,
68 - }); 66 + dosage,
69 - await takeMedicineHist.save(); 67 + });
68 + await takeMedicineHist.save();
69 + }
70 +
71 + await bottleMedicine.setTotalWeight(totalWeight);
72 + await bottleMedicine.save();
70 } 73 }
71 -
72 - await bottleMedicine.setTotalWeight(totalWeight);
73 - await bottleMedicine.save();
74 } 74 }
75 } 75 }
76 76
...@@ -83,7 +83,9 @@ const transPublishingTopicAndMessage = async(bottleId) => { ...@@ -83,7 +83,9 @@ const transPublishingTopicAndMessage = async(bottleId) => {
83 bmId : bottleMedicine._id 83 bmId : bottleMedicine._id
84 }).sort({ takeDate : 'desc' }).limit(1); 84 }).sort({ takeDate : 'desc' }).limit(1);
85 85
86 - const message = 'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage; 86 + const message = takeMedicineHistList && takeMedicineHistList[0] ?
87 + 'res/' + await transDate(takeMedicineHistList[0].takeDate) + '/' + takeMedicineHistList[0].dosage :
88 + 'res/' + await transDate(new Date()) + '/' + 0;
87 89
88 return { 90 return {
89 topic, 91 topic,
......