1
|
|
|
const mqtt = require('async-mqtt') |
2
|
|
|
const configuration = require('./configuration') |
3
|
|
|
const validate = require('./validate') |
4
|
|
|
|
5
|
|
|
module.exports = (input, callback) => { |
6
|
|
|
|
7
|
|
|
let error = null |
8
|
|
|
let output = {} |
9
|
|
|
|
10
|
|
|
validate.sourceConfiguration(input, (validatedInput, thrownError) => { |
11
|
|
|
input = validatedInput |
12
|
|
|
error = thrownError |
13
|
|
|
}) |
14
|
|
|
validate.paramConfiguration(input, (validatedInput, thrownError) => { |
15
|
|
|
input = validatedInput |
16
|
|
|
error = thrownError |
17
|
|
|
}) |
18
|
|
|
|
19
|
|
|
// if file exist do: |
20
|
|
|
// message = require('fs').readFile('/tmp/build/put/out/message', (err,buf) => { process.stdout.write(buf)}) |
21
|
|
|
// endif |
22
|
|
|
// {'name': 'pwd', 'value': process.cwd()}, |
23
|
|
|
|
24
|
|
|
// Send MQTT |
25
|
|
|
if (!error) { |
26
|
|
|
let configurationMqtt = configuration.mqtt(input) |
27
|
|
|
let client = mqtt.connect(input.source.url, configurationMqtt) |
28
|
|
|
input.params.qos = 0 |
29
|
|
|
const sendMessage = async () => { |
30
|
|
|
try { |
31
|
|
|
await client.publish( |
32
|
|
|
input.params.topic, |
33
|
|
|
input.params.payload.toString() |
34
|
|
|
) |
35
|
|
|
output = { |
36
|
|
|
'version': { |
37
|
|
|
'ref': client.getLastMessageId().toString() || 'none', |
38
|
|
|
'message': input.params.payload.toString() |
39
|
|
|
}, |
40
|
|
|
'metadata': [ |
41
|
|
|
{'name': 'ATC_EXTERNAL_URL', 'value': process.env.ATC_EXTERNAL_URL || 'not set'}, |
42
|
|
|
{'name': 'BUILD_ID', 'value': process.env.BUILD_ID || 'not set'}, |
43
|
|
|
{'name': 'BUILD_NAME', 'value': process.env.BUILD_NAME || 'not set'}, |
44
|
|
|
{'name': 'BUILD_JOB_NAME', 'value': process.env.BUILD_JOB_NAME || 'not set'}, |
45
|
|
|
{'name': 'BUILD_JOB_ID', 'value': process.env.BUILD_JOB_ID || 'not set'}, |
46
|
|
|
{'name': 'BUILD_PIPELINE_NAME', 'value': process.env.BUILD_PIPELINE_NAME || 'not set'}, |
47
|
|
|
{'name': 'BUILD_PIPELINE_ID', 'value': process.env.BUILD_PIPELINE_ID || 'not set'}, |
48
|
|
|
{'name': 'BUILD_TEAM_NAME', 'value': process.env.BUILD_TEAM_NAME || 'not set'}, |
49
|
|
|
{'name': 'MQTT_LAST_MESSAGE_ID', 'value': client.getLastMessageId().toString() || 'not set'}, |
50
|
|
|
{'name': 'source.url', 'value': input.source.url || 'not set'}, |
51
|
|
|
{'name': 'source.port', 'value': input.source.port.toString() || 'not set'}, |
52
|
|
|
{'name': 'params.payload', 'value': input.params.payload || 'not set'}, |
53
|
|
|
{'name': 'params.topic', 'value': input.params.topic || 'not set'}, |
54
|
|
|
{'name': 'params.qos', 'value': input.params.qos.toString() || 'not set'} |
55
|
|
|
] |
56
|
|
|
} |
57
|
|
|
await client.end() |
58
|
|
|
callback(error, output) |
59
|
|
|
} catch (e) { |
60
|
|
|
callback(e.stack, {}) |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
client.on('connect', sendMessage) |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|