Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clientId #69

Merged
merged 7 commits into from Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/scripts/wot-servient.conf.json
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
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
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
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