vergissberlin /
mqtt-resource
| 1 | const mqtt = require('mqtt') |
||
| 2 | const validate = require('./validate') |
||
| 3 | const configuration = require('./configuration') |
||
| 4 | |||
| 5 | module.exports = (input, callback) => { |
||
| 6 | let error = null, |
||
| 7 | output = [] |
||
| 8 | |||
| 9 | validate.sourceConfiguration(input, (validatedInput, thrownError) => { |
||
| 10 | input = validatedInput |
||
| 11 | error = thrownError |
||
| 12 | }) |
||
| 13 | |||
| 14 | if (!error) { |
||
| 15 | let configurationMqtt = configuration.mqtt(input) |
||
| 16 | let client = mqtt.connect(input.source.url, configurationMqtt) |
||
| 17 | let version = 0 |
||
| 18 | let topic = input.params.topics |
||
| 19 | |||
| 20 | client.on('connect', () => { |
||
| 21 | client.subscribe(topic, (errorConnection) => { |
||
| 22 | if (!errorConnection) { |
||
| 23 | client.on('message', (topic, message) => { |
||
| 24 | version = message.toString() |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 25 | }) |
||
| 26 | } else { |
||
| 27 | error = errorConnection.toString() |
||
| 28 | callback(error, output) |
||
| 29 | } |
||
| 30 | }) |
||
| 31 | }) |
||
| 32 | |||
| 33 | client.on('message', function (topic, message) { |
||
| 34 | if (message.toString() !== 'none') { |
||
| 35 | output.push({'message': message.toString()}) |
||
| 36 | } |
||
| 37 | client.end() |
||
| 38 | callback(error, output) |
||
| 39 | }) |
||
| 40 | } else { |
||
| 41 | callback(error, output) |
||
| 42 | } |
||
| 43 | } |
||
| 44 |