Total Complexity | 9 |
Complexity/F | 1.5 |
Lines of Code | 43 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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() |
||
|
|||
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 |