Add MQTT & Trigger Node (#1705)

*  MQTT-Node

*  Small fix

*  Error when the publish method faile

*  Improvements

*  Improvements

*  Add Send Input Data parameter

*  Minor improvements

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza
2021-04-30 21:23:25 -04:00
committed by GitHub
parent 0dd760f67d
commit 6c773d7a86
7 changed files with 259 additions and 22 deletions

View File

@@ -13,14 +13,14 @@ import {
import * as mqtt from 'mqtt';
import {
IClientOptions,
IClientOptions, ISubscriptionMap,
} from 'mqtt';
export class MqttTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'MQTT Trigger',
name: 'mqttTrigger',
icon: 'file:mqtt.png',
icon: 'file:mqtt.svg',
group: ['trigger'],
version: 1,
description: 'Listens to MQTT events',
@@ -43,7 +43,9 @@ export class MqttTrigger implements INodeType {
type: 'string',
default: '',
description: `Topics to subscribe to, multiple can be defined with comma.<br/>
wildcard characters are supported (+ - for single level and # - for multi level)`,
wildcard characters are supported (+ - for single level and # - for multi level)<br>
By default all subscription used QoS=0. To set a different QoS, write the QoS desired<br>
after the topic preceded by a colom. For Example: topicA:1,topicB:2`,
},
{
displayName: 'Options',
@@ -52,6 +54,13 @@ export class MqttTrigger implements INodeType {
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'JSON Parse Body',
name: 'jsonParseBody',
type: 'boolean',
default: false,
description: 'Try to parse the message to an object.',
},
{
displayName: 'Only Message',
name: 'onlyMessage',
@@ -59,13 +68,6 @@ export class MqttTrigger implements INodeType {
default: false,
description: 'Returns only the message property.',
},
{
displayName: 'JSON Parse Message',
name: 'jsonParseMessage',
type: 'boolean',
default: false,
description: 'Try to parse the message to an object.',
},
],
},
],
@@ -81,6 +83,13 @@ export class MqttTrigger implements INodeType {
const topics = (this.getNodeParameter('topics') as string).split(',');
const topicsQoS: IDataObject = {};
for (const data of topics) {
const [topic, qos] = data.split(':');
topicsQoS[topic] = (qos) ? { qos: parseInt(qos, 10) } : { qos: 0 };
}
const options = this.getNodeParameter('options') as IDataObject;
if (!topics) {
@@ -91,9 +100,13 @@ export class MqttTrigger implements INodeType {
const host = credentials.host as string;
const brokerUrl = `${protocol}://${host}`;
const port = credentials.port as number || 1883;
const clientId = credentials.clientId as string || `mqttjs_${Math.random().toString(16).substr(2, 8)}`;
const clean = credentials.clean as boolean;
const clientOptions: IClientOptions = {
port,
clean,
clientId,
};
if (credentials.username && credentials.password) {
@@ -108,20 +121,19 @@ export class MqttTrigger implements INodeType {
async function manualTriggerFunction() {
await new Promise((resolve, reject) => {
client.on('connect', () => {
client.subscribe(topics, (err, granted) => {
client.subscribe(topicsQoS as ISubscriptionMap, (err, granted) => {
if (err) {
reject(err);
}
client.on('message', (topic: string, message: Buffer | string) => { // tslint:disable-line:no-any
let result: IDataObject = {};
message = message.toString() as string;
if (options.jsonParseMessage) {
if (options.jsonParseBody) {
try {
message = JSON.parse(message.toString());
} catch (error) { }
} catch (err) { }
}
result.message = message;
@@ -129,10 +141,9 @@ export class MqttTrigger implements INodeType {
if (options.onlyMessage) {
//@ts-ignore
result = message;
result = [message as string];
}
self.emit([self.helpers.returnJsonArray([result])]);
self.emit([self.helpers.returnJsonArray(result)]);
resolve(true);
});
});
@@ -144,7 +155,9 @@ export class MqttTrigger implements INodeType {
});
}
manualTriggerFunction();
if (this.getMode() === 'trigger') {
manualTriggerFunction();
}
async function closeFunction() {
client.end();