박권수

fix. 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,21 +44,21 @@ const factoring = async (topic, message) => { ...@@ -45,21 +44,21 @@ 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
47 + if(!parseInt(isOpen)) {
48 bottleId = parseInt(bottleId); 48 bottleId = parseInt(bottleId);
49 - isOpen = parseInt(isOpen); 49 + temperature = parseFloat(temperature);
50 - temperature = parseFloat(temperature).toFixed(1); 50 + humidity = parseFloat(humidity);
51 - humidity = parseFloat(humidity).toFixed(1); 51 + totalWeight = parseFloat(totalWeight);
52 - totalWeight = parseFloat(totalWeight).toFixed(2);
53 52
54 const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); 53 const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' });
55 54
56 if(bottleMedicine) { 55 if(bottleMedicine) {
57 const lastTotalWeight = parseFloat(bottleMedicine.totalWeight); 56 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
61 + if(dosage > 0) {
63 const takeMedicineHist = new TakeMedicineHist({ 62 const takeMedicineHist = new TakeMedicineHist({
64 bmId : bottleMedicine._id, 63 bmId : bottleMedicine._id,
65 temperature, 64 temperature,
...@@ -72,6 +71,7 @@ const bottleInfoUpdate = async(data) => { ...@@ -72,6 +71,7 @@ const bottleInfoUpdate = async(data) => {
72 await bottleMedicine.setTotalWeight(totalWeight); 71 await bottleMedicine.setTotalWeight(totalWeight);
73 await bottleMedicine.save(); 72 await bottleMedicine.save();
74 } 73 }
74 + }
75 } 75 }
76 76
77 //해당 MQTT Broker(client)에 bottleId의 정보에 관한 topic과 message를 리턴한다. 77 //해당 MQTT Broker(client)에 bottleId의 정보에 관한 topic과 message를 리턴한다.
...@@ -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,
......