Showing
1 changed file
with
20 additions
and
0 deletions
server/src/util/MqttServer.js
0 → 100644
1 | +const Mqtt = require('../lib/MqttModule'); | ||
2 | +const DataProcess = require('../lib/DataProcess'); | ||
3 | +const Hub = require('../models/hub'); | ||
4 | +const Bottle = require('../models/bottle'); | ||
5 | + | ||
6 | +exports.on = async() => { | ||
7 | + await subscribeOn(); | ||
8 | + console.log('\x1b[1;34mMQTT Server On\x1b[0m'); | ||
9 | +}; | ||
10 | + | ||
11 | +const subscribeOn = async () => { | ||
12 | + const bottleList = await Bottle.find(); | ||
13 | + | ||
14 | + bottleList.forEach(async(bottle) => { | ||
15 | + const topic = 'bottle/' + bottle.getBottleId() + '/bts'; | ||
16 | + const hub = await Hub.findByHubId(bottle.getHubId()); | ||
17 | + const client = await Mqtt.mqttOn(hub.getHubHost(), DataProcess.dataPublish); | ||
18 | + Mqtt.mqttSubscribe(client, topic); | ||
19 | + }) | ||
20 | +}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment