diff --git a/examples/scripts/wot-servient.conf.json b/examples/scripts/wot-servient.conf.json index dcf184bc3..d1ca42c63 100644 --- a/examples/scripts/wot-servient.conf.json +++ b/examples/scripts/wot-servient.conf.json @@ -13,6 +13,9 @@ }, "_mqtt" : { "broker" : "mqtt://test.mosquitto.org", + "username" : "username", + "password" : "password", + "clientId" : "uniqueId", "port": 1883 }, "log": { diff --git a/packages/binding-mqtt/README.md b/packages/binding-mqtt/README.md index a048644cd..dc712c8e7 100644 --- a/packages/binding-mqtt/README.md +++ b/packages/binding-mqtt/README.md @@ -20,13 +20,16 @@ Setup node-wot as described at the [node-wot main page](./../../README.md). There are sample implementations provided in the [example/scripting folder](./examples/scripts): -* example-mqtt-publish.js: Shows when node-wot act as a MQTT Client that publish data (latest counter value) to a broker. In the same time the client setup an action (reset counter) that can be initated by another MQTT client by sending a publication message to this action based topic. Please note to provide MQTT broker details (host, port, [username], [password]) in the wot-servient.conf.json: +* example-mqtt-publish.js: Shows when node-wot act as a MQTT Client that publish data (latest counter value) to a broker. In the same time the client setup an action (reset counter) that can be initated by another MQTT client by sending a publication message to this action based topic. Please note to provide MQTT broker details (host, port, [username], [password], [clientId]) in the wot-servient.conf.json: ``` { "mqtt" : { "host" : "mqtt://test.mosquitto.org", + "username" : "username", + "password" : "password", + "clientId" : "uniqueId", "port": 1883 } } @@ -62,4 +65,4 @@ Start the script by the command `wot-servient -c mqtt-subscribe.js` or `node ../ } } } -``` \ No newline at end of file +``` diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index 0b7a43c3b..59d825ada 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -34,13 +34,16 @@ export default class MqttBrokerServer implements ProtocolServer { private user: string = undefined; // in the case usesername is required to connect the broker private psw: string = undefined; // in the case password is required to connect the broker + + private clientId: string = undefined; // in the case clientId can be used to identify the device + private brokerURI: string = undefined; private readonly things: Map = new Map(); private broker: any; - constructor(uri: string, user?: string, psw?: string) { + constructor(uri: string, user?: string, psw?: string, clientId?: string) { if (uri !== undefined) { //if there is a MQTT protocol identicator missing, add this @@ -56,6 +59,9 @@ export default class MqttBrokerServer implements ProtocolServer { if (psw !== undefined) { this.psw = psw; } + if (clientId !== undefined) { + this.clientId = clientId; + } } public expose(thing: ExposedThing): Promise { @@ -164,10 +170,14 @@ export default class MqttBrokerServer implements ProtocolServer { console.info(`MqttBrokerServer trying to connect to broker at ${this.brokerURI}`); // TODO test if mqtt extracts port from passed URI (this.address) this.broker = mqtt.connect(this.brokerURI); - } else { + } else if (this.clientId === undefined) { console.info(`MqttBrokerServer trying to connect to secured broker at ${this.brokerURI}`); // TODO test if mqtt extracts port from passed URI (this.address) this.broker = mqtt.connect(this.brokerURI, { username: this.user, password: this.psw }); + } else { + console.info(`MqttBrokerServer trying to connect to secured broker at ${this.brokerURI} with client ID ${this.clientId}`); + // TODO test if mqtt extracts port from passed URI (this.address) + this.broker = mqtt.connect(this.brokerURI, { username: this.user, password: this.psw, clientId: this.clientId }); } this.broker.on("connect", () => { diff --git a/packages/cli/src/cli-default-servient.ts b/packages/cli/src/cli-default-servient.ts index 7096dcc4b..56f9ea7a9 100644 --- a/packages/cli/src/cli-default-servient.ts +++ b/packages/cli/src/cli-default-servient.ts @@ -89,7 +89,7 @@ export default class DefaultServient extends Servient { this.addServer(coapServer); } if (this.config.mqtt !== undefined) { - let mqttBrokerServer = new MqttBrokerServer(this.config.mqtt.broker, (typeof this.config.mqtt.username === "string") ? this.config.mqtt.username : undefined, (typeof this.config.mqtt.password === "number") ? this.config.mqtt.password : undefined); + let mqttBrokerServer = new MqttBrokerServer(this.config.mqtt.broker, (typeof this.config.mqtt.username === "string") ? this.config.mqtt.username : undefined, (typeof this.config.mqtt.password === "string") ? this.config.mqtt.password : undefined, (typeof this.config.mqtt.clientId === "string") ? this.config.mqtt.clientId : undefined); this.addServer(mqttBrokerServer); } }