Skip to content

Commit

Permalink
Merge pull request #69 from wiresio/patch-1
Browse files Browse the repository at this point in the history
Add clientId
  • Loading branch information
danielpeintner authored Feb 6, 2019
2 parents df9ecdd + 0471342 commit e8e5022
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/scripts/wot-servient.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
"_mqtt" : {
"broker" : "mqtt://test.mosquitto.org",
"username" : "username",
"password" : "password",
"clientId" : "uniqueId",
"port": 1883
},
"log": {
Expand Down
7 changes: 5 additions & 2 deletions packages/binding-mqtt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -62,4 +65,4 @@ Start the script by the command `wot-servient -c mqtt-subscribe.js` or `node ../
}
}
}
```
```
14 changes: 12 additions & 2 deletions packages/binding-mqtt/src/mqtt-broker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExposedThing> = new Map<string, ExposedThing>();

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
Expand All @@ -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<void> {
Expand Down Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli-default-servient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit e8e5022

Please sign in to comment.