Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 47 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | 'use strict' |
||
2 | |||
3 | module.exports = (input, callback) => { |
||
4 | const mqtt = require('mqtt') |
||
5 | const validate = require('./validate') |
||
6 | const configuration = require('./configuration') |
||
7 | let error = null |
||
8 | let output = null |
||
9 | |||
10 | validate.configuration(input, (validatedInput, thrownError) => { |
||
11 | input = validatedInput |
||
12 | error = thrownError |
||
13 | }) |
||
14 | |||
15 | // Send MQTT |
||
16 | if ( !error ) { |
||
17 | let configurationMqtt = configuration.mqtt(input) |
||
18 | let client = mqtt.connect(input.source.url, configurationMqtt) |
||
19 | let version = null |
||
20 | |||
21 | client.on('connect', () => { |
||
22 | client.subscribe(input.source.prefix, (errorConnection) => { |
||
23 | if ( !errorConnection ) { |
||
24 | client.publish(input.source.topic, input.params.payload, { |
||
25 | qos: input.params.qos, |
||
26 | retain: false |
||
27 | }) |
||
28 | } else { |
||
29 | error = errorConnection |
||
30 | } |
||
31 | }) |
||
32 | }) |
||
33 | client.on('message', (topic, message) => { |
||
34 | version = message.toString() |
||
35 | client.end() |
||
36 | }) |
||
37 | |||
38 | |||
39 | |||
40 | output = [ |
||
41 | {'ref': version} |
||
42 | ] |
||
43 | } |
||
44 | |||
45 | |||
46 | callback(error, output) |
||
47 | } |
||
48 |