Passed
Push — master ( d06fb8...9f4cb8 )
by André
03:15 queued 01:29
created

src/check.js   A

Complexity

Total Complexity 9
Complexity/F 1.5

Size

Lines of Code 43
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 29
mnd 3
bc 3
fnc 6
dl 0
loc 43
rs 10
bpm 0.5
cpm 1.5
noi 1
c 0
b 0
f 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()
0 ignored issues
show
Unused Code introduced by
The variable version seems to be never used. Consider removing it.
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