Showing
1 changed file
with
34 additions
and
4 deletions
... | @@ -12,14 +12,19 @@ exports.dataPublish = async (topic, message) => { | ... | @@ -12,14 +12,19 @@ exports.dataPublish = async (topic, message) => { |
12 | const result = await transPublishingTopicAndMessage(bottleId); | 12 | const result = await transPublishingTopicAndMessage(bottleId); |
13 | 13 | ||
14 | return result; | 14 | return result; |
15 | + | ||
15 | }; | 16 | }; |
16 | 17 | ||
17 | //Hub topic : bottle/bottleId | 18 | //Hub topic : bottle/bottleId |
18 | //Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서 | 19 | //Hub로부터 받은 message : 개폐여부/온도/습도/초음파센서 |
19 | -const factoring = (topic, message) => { | 20 | +const factoring = async (topic, message) => { |
20 | const bottleId = parseInt(topic.split('/')[1]); | 21 | const bottleId = parseInt(topic.split('/')[1]); |
21 | const data = message.split('/'); | 22 | const data = message.split('/'); |
22 | - const [isOpen, temperature, humidity, balance] = data; | 23 | + let [isOpen, temperature, humidity, balance] = data; |
24 | + | ||
25 | + if(isOpen === '0') | ||
26 | + balance = await balanceFactoring(balance); | ||
27 | + else balance = '-1'; | ||
23 | 28 | ||
24 | const openDate = new Date(); | 29 | const openDate = new Date(); |
25 | 30 | ||
... | @@ -31,23 +36,48 @@ const factoring = (topic, message) => { | ... | @@ -31,23 +36,48 @@ const factoring = (topic, message) => { |
31 | humidity, | 36 | humidity, |
32 | balance | 37 | balance |
33 | }; | 38 | }; |
39 | + | ||
40 | +} | ||
41 | + | ||
42 | +const balanceFactoring = (balance) => { | ||
43 | + const max = 11; | ||
44 | + const slicingBalance = max / 5; | ||
45 | + | ||
46 | + if(parseInt(balance) < slicingBalance || parseInt(balance) > max * 2) | ||
47 | + return '100'; | ||
48 | + else if(parseInt(balance) < slicingBalance * 2) | ||
49 | + return '80'; | ||
50 | + else if(parseInt(balance) < slicingBalance * 3) | ||
51 | + return '60'; | ||
52 | + else if(parseInt(balance) < slicingBalance * 4) | ||
53 | + return '40'; | ||
54 | + else if(parseInt(balance) < slicingBalance * 5) | ||
55 | + return '20'; | ||
56 | + else return '0'; | ||
57 | + | ||
34 | } | 58 | } |
35 | 59 | ||
36 | //bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다. | 60 | //bottleId가 포함된 data를 받아서 해당 약병의 data를 업데이트한다. |
37 | const bottleInfoUpdate = async(data) => { | 61 | const bottleInfoUpdate = async(data) => { |
38 | const { bottleId, isOpen, openDate, temperature, humidity, balance } = data; | 62 | const { bottleId, isOpen, openDate, temperature, humidity, balance } = data; |
63 | + | ||
39 | if(isOpen === '1') { | 64 | if(isOpen === '1') { |
40 | await Bottle.findOneAndUpdate({ | 65 | await Bottle.findOneAndUpdate({ |
41 | bottleId | 66 | bottleId |
42 | }, { recentOpen : openDate }); | 67 | }, { recentOpen : openDate }); |
43 | } | 68 | } |
44 | 69 | ||
70 | + if(balance !== '-1') { | ||
71 | + await Bottle.findOneAndUpdate({ | ||
72 | + bottleId | ||
73 | + }, { balance }) | ||
74 | + } | ||
75 | + | ||
45 | await Bottle.findOneAndUpdate({ | 76 | await Bottle.findOneAndUpdate({ |
46 | bottleId | 77 | bottleId |
47 | }, { | 78 | }, { |
48 | temperature, | 79 | temperature, |
49 | - humidity, | 80 | + humidity |
50 | - balance | ||
51 | }); | 81 | }); |
52 | } | 82 | } |
53 | 83 | ... | ... |
-
Please register or login to post a comment