@yidingdian/binding-mqtt 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +211 -0
- package/dist/mqtt-broker-server.d.ts +36 -0
- package/dist/mqtt-broker-server.js +361 -0
- package/dist/mqtt-broker-server.js.map +1 -0
- package/dist/mqtt-client-factory.d.ts +8 -0
- package/dist/mqtt-client-factory.js +29 -0
- package/dist/mqtt-client-factory.js.map +1 -0
- package/dist/mqtt-client.d.ts +25 -0
- package/dist/mqtt-client.js +261 -0
- package/dist/mqtt-client.js.map +1 -0
- package/dist/mqtt-message-pool.d.ts +12 -0
- package/dist/mqtt-message-pool.js +101 -0
- package/dist/mqtt-message-pool.js.map +1 -0
- package/dist/mqtt.d.ts +39 -0
- package/dist/mqtt.js +41 -0
- package/dist/mqtt.js.map +1 -0
- package/dist/mqtts-client-factory.d.ts +11 -0
- package/dist/mqtts-client-factory.js +30 -0
- package/dist/mqtts-client-factory.js.map +1 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +22 -0
- package/dist/util.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# MQTT Protocol Binding of node-wot
|
|
2
|
+
|
|
3
|
+
W3C Web of Things (WoT) Protocol Binding for [MQTT](https://en.wikipedia.org/wiki/MQTT).
|
|
4
|
+
This package uses [mqtt](https://www.npmjs.com/package/mqtt) as a low level library for MQTT.
|
|
5
|
+
W3C WoT Binding Template for MQTT can be found [here](https://w3c.github.io/wot-binding-templates/bindings/protocols/mqtt/index.html).
|
|
6
|
+
|
|
7
|
+
Current Maintainer(s): [@egekorkan](https://github.com/egekorkan) [@sebastiankb](https://github.com/sebastiankb) [@hasanheroglu](https://github.com/hasanheroglu)
|
|
8
|
+
|
|
9
|
+
## Protocol specifier
|
|
10
|
+
|
|
11
|
+
The protocol prefix handled by this binding is `mqtt://` or `mqtts://`.
|
|
12
|
+
|
|
13
|
+
## Getting Started
|
|
14
|
+
|
|
15
|
+
In the following examples it is shown how to use the MQTT binding of node-wot.
|
|
16
|
+
|
|
17
|
+
### Prerequisites
|
|
18
|
+
|
|
19
|
+
- `npm install @node-wot/core`
|
|
20
|
+
- `npm install @node-wot/binding-mqtt`
|
|
21
|
+
|
|
22
|
+
### MQTT Thing Example I
|
|
23
|
+
|
|
24
|
+
An MQTT Thing frequently publishes counter values (as an Event Affordance) to the topic `/MQTT-Test/events/counterEvent` of the MQTT broker running behind the address `test.mosquitto.org:1883`.
|
|
25
|
+
In addition, the Thing subscribes to the `resetCounter` topic (via its action handler) as a WoT Action Affordance so that
|
|
26
|
+
it can handle requests to reset the counter value.
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const { Servient } = require("@node-wot/core");
|
|
30
|
+
const { MqttBrokerServer } = require("@node-wot/binding-mqtt");
|
|
31
|
+
|
|
32
|
+
// create Servient add MQTT binding
|
|
33
|
+
const servient = new Servient();
|
|
34
|
+
servient.addServer(new MqttBrokerServer({ uri: "mqtt://test.mosquitto.org" }));
|
|
35
|
+
|
|
36
|
+
servient.start().then((WoT) => {
|
|
37
|
+
var counter = 0;
|
|
38
|
+
|
|
39
|
+
WoT.produce({
|
|
40
|
+
title: "MQTT-Test",
|
|
41
|
+
id: "urn:dev:wot:mqtt:counter",
|
|
42
|
+
actions: {
|
|
43
|
+
resetCounter: {
|
|
44
|
+
description: "Reset counter",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
events: {
|
|
48
|
+
counterEvent: {
|
|
49
|
+
description: "Counter Value",
|
|
50
|
+
data: {
|
|
51
|
+
type: "integer",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
.then((thing) => {
|
|
57
|
+
thing.setActionHandler("resetCounter", async () => {
|
|
58
|
+
console.log("Resetting counter");
|
|
59
|
+
counter = 0;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
thing.expose().then(() => {
|
|
63
|
+
console.info(thing.title + " ready");
|
|
64
|
+
|
|
65
|
+
setInterval(() => {
|
|
66
|
+
++counter;
|
|
67
|
+
thing.emitEvent("counterEvent", counter);
|
|
68
|
+
console.info("New count ", counter);
|
|
69
|
+
}, 1000);
|
|
70
|
+
});
|
|
71
|
+
})
|
|
72
|
+
.catch((e) => {
|
|
73
|
+
console.log(e);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### Sample Thing Description for MQTT Clients
|
|
79
|
+
|
|
80
|
+
The Thing Description corresponding to the previous example is shown below:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
{
|
|
84
|
+
"@context": "https://www.w3.org/2019/wot/td/v1",
|
|
85
|
+
"title": "MQTT-Test",
|
|
86
|
+
"id": "urn:dev:wot:mqtt:counter",
|
|
87
|
+
"actions" : {
|
|
88
|
+
"resetCounter": {
|
|
89
|
+
"description": "Reset counter"
|
|
90
|
+
"forms": [
|
|
91
|
+
{"href": "mqtt://test.mosquitto.org:1883/MQTT-Test/actions/resetCounter"}
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"events": {
|
|
96
|
+
"counterEvent": {
|
|
97
|
+
"description": "Counter Value",
|
|
98
|
+
"data": {
|
|
99
|
+
"type": "integer"
|
|
100
|
+
},
|
|
101
|
+
"forms": [
|
|
102
|
+
{"href": "mqtt://test.mosquitto.org:1883/MQTT-Test/events/counterEvent"}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### MQTT Client Example II
|
|
110
|
+
|
|
111
|
+
This example takes the Thing Description of the previous example and subscribes to the `counterEvent` and resets the counter every 20s via the `resetCounter` action.
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
const { Servient } = require("@node-wot/core");
|
|
115
|
+
const { MqttClientFactory } = require("@node-wot/binding-mqtt");
|
|
116
|
+
|
|
117
|
+
// create Servient and add MQTT binding
|
|
118
|
+
const servient = new Servient();
|
|
119
|
+
servient.addClientFactory(new MqttClientFactory(null));
|
|
120
|
+
|
|
121
|
+
// Thing Description can be also fetched
|
|
122
|
+
const td = `{
|
|
123
|
+
"@context": "https://www.w3.org/2019/td/v1",
|
|
124
|
+
"title": "MQTT-Test",
|
|
125
|
+
"id": "urn:dev:wot:mqtt:counter",
|
|
126
|
+
"actions" : {
|
|
127
|
+
"resetCounter": {
|
|
128
|
+
"forms": [
|
|
129
|
+
{"href": "mqtt://test.mosquitto.org:1883/MQTT-Test/actions/resetCounter"}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"events": {
|
|
134
|
+
"counterEvent": {
|
|
135
|
+
"description": "Counter Value",
|
|
136
|
+
"data": {
|
|
137
|
+
"type": "integer"
|
|
138
|
+
},
|
|
139
|
+
"forms": [
|
|
140
|
+
{"href": "mqtt://test.mosquitto.org:1883/MQTT-Test/events/counterEvent"}
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}`;
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
servient.start().then((WoT) => {
|
|
148
|
+
WoT.consume(JSON.parse(td)).then((source) => {
|
|
149
|
+
console.info("=== TD ===");
|
|
150
|
+
console.info(td);
|
|
151
|
+
console.info("==========");
|
|
152
|
+
|
|
153
|
+
source.subscribeEvent(
|
|
154
|
+
"counterEvent",
|
|
155
|
+
(x) => console.info("value:", x),
|
|
156
|
+
(e) => console.error("Error: %s", e),
|
|
157
|
+
() => console.info("Completed")
|
|
158
|
+
);
|
|
159
|
+
console.info("Subscribed");
|
|
160
|
+
|
|
161
|
+
source.invokeAction("resetCounter");
|
|
162
|
+
|
|
163
|
+
setInterval(async () => {
|
|
164
|
+
source
|
|
165
|
+
.invokeAction("resetCounter")
|
|
166
|
+
.then((res) => {})
|
|
167
|
+
.catch((err) => {
|
|
168
|
+
console.error("ResetCounter error:", err.message);
|
|
169
|
+
});
|
|
170
|
+
console.info("Reset counter!");
|
|
171
|
+
}, 20000);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
} catch (err) {
|
|
175
|
+
console.error("Script error:", err);
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### More Examples
|
|
180
|
+
|
|
181
|
+
There are example implementations provided in the [example/scripting folder](https://github.com/eclipse-thingweb/node-wot/tree/master/examples/scripts).
|
|
182
|
+
|
|
183
|
+
Please setup node-wot as described at the [node-wot main page](https://github.com/eclipse-thingweb/node-wot#as-a-standalone-application).
|
|
184
|
+
|
|
185
|
+
- example-mqtt-publish.js: Shows when node-wot acts as a MQTT Client that publishes data (latest counter value) to a broker.
|
|
186
|
+
At the same time, another client can invoke an action, such as `resetCounter`, by sending a publication message to the topic of this action.
|
|
187
|
+
This other client does not have to be node-wot, any MQTT client can interact with this Thing.
|
|
188
|
+
For node-wot clients, make sure to provide MQTT broker details (`host`, `port`, `username`, `password`, `clientId`) in the wot-servient.conf.json:
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
{
|
|
192
|
+
"mqtt" : {
|
|
193
|
+
"host" : "mqtt://test.mosquitto.org",
|
|
194
|
+
"username" : "username",
|
|
195
|
+
"password" : "password",
|
|
196
|
+
"clientId" : "uniqueId",
|
|
197
|
+
"port": 1883
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Start the script by the command `wot-servient mqtt-publish.js` or `node ../../packages/cli/dist/cli.js mqtt-publish.js`.
|
|
204
|
+
|
|
205
|
+
- example-mqtt-subscription.js: Shows how node-wot consumes a Thing Description to do MQTT subscription on the provided event (=latest counter value) as well as initiate the action (reset counter).
|
|
206
|
+
|
|
207
|
+
Start the script by the command `wot-servient -c mqtt-subscribe.js` or `node ../../packages/cli/dist/cli.js -c mqtt-subscribe.js`.
|
|
208
|
+
|
|
209
|
+
### More Details
|
|
210
|
+
|
|
211
|
+
See <https://github.com/eclipse-thingweb/node-wot/>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MqttBrokerServerConfig } from "./mqtt";
|
|
2
|
+
import { ProtocolServer, Servient, ExposedThing } from "@node-wot/core";
|
|
3
|
+
export default class MqttBrokerServer implements ProtocolServer {
|
|
4
|
+
private static brokerIsInitialized;
|
|
5
|
+
readonly scheme: string;
|
|
6
|
+
private readonly ACTION_SEGMENT_LENGTH;
|
|
7
|
+
private readonly PROPERTY_SEGMENT_LENGTH;
|
|
8
|
+
private readonly THING_NAME_SEGMENT_INDEX;
|
|
9
|
+
private readonly INTERACTION_TYPE_SEGMENT_INDEX;
|
|
10
|
+
private readonly INTERACTION_NAME_SEGMENT_INDEX;
|
|
11
|
+
private readonly INTERACTION_EXT_SEGMENT_INDEX;
|
|
12
|
+
private readonly defaults;
|
|
13
|
+
private port;
|
|
14
|
+
private address?;
|
|
15
|
+
private brokerURI;
|
|
16
|
+
private readonly things;
|
|
17
|
+
private readonly config;
|
|
18
|
+
private broker?;
|
|
19
|
+
private hostedServer?;
|
|
20
|
+
private hostedBroker?;
|
|
21
|
+
constructor(config: MqttBrokerServerConfig);
|
|
22
|
+
expose(thing: ExposedThing): Promise<void>;
|
|
23
|
+
private exposeProperty;
|
|
24
|
+
private exposeAction;
|
|
25
|
+
private exposeEvent;
|
|
26
|
+
private handleMessage;
|
|
27
|
+
private handleAction;
|
|
28
|
+
private handlePropertyWrite;
|
|
29
|
+
destroy(thingId: string): Promise<boolean>;
|
|
30
|
+
start(servient: Servient): Promise<void>;
|
|
31
|
+
stop(): Promise<void>;
|
|
32
|
+
getPort(): number;
|
|
33
|
+
getAddress(): string | undefined;
|
|
34
|
+
private selfHostAuthentication;
|
|
35
|
+
private startBroker;
|
|
36
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const mqtt = __importStar(require("mqtt"));
|
|
40
|
+
const url = __importStar(require("url"));
|
|
41
|
+
const aedes_1 = __importDefault(require("aedes"));
|
|
42
|
+
const net = __importStar(require("net"));
|
|
43
|
+
const tls = __importStar(require("tls"));
|
|
44
|
+
const mqtt_1 = require("./mqtt");
|
|
45
|
+
const core_1 = require("@node-wot/core");
|
|
46
|
+
const stream_1 = require("stream");
|
|
47
|
+
const util_1 = require("./util");
|
|
48
|
+
const { info, debug, error, warn } = (0, core_1.createLoggers)("binding-mqtt", "mqtt-broker-server");
|
|
49
|
+
class MqttBrokerServer {
|
|
50
|
+
static brokerIsInitialized(broker) {
|
|
51
|
+
if (broker === undefined) {
|
|
52
|
+
throw new Error(`Broker not initialized. You need to start the ${MqttBrokerServer.name} before you can expose things.`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
constructor(config) {
|
|
56
|
+
this.scheme = "mqtt";
|
|
57
|
+
this.ACTION_SEGMENT_LENGTH = 3;
|
|
58
|
+
this.PROPERTY_SEGMENT_LENGTH = 4;
|
|
59
|
+
this.THING_NAME_SEGMENT_INDEX = 0;
|
|
60
|
+
this.INTERACTION_TYPE_SEGMENT_INDEX = 1;
|
|
61
|
+
this.INTERACTION_NAME_SEGMENT_INDEX = 2;
|
|
62
|
+
this.INTERACTION_EXT_SEGMENT_INDEX = 3;
|
|
63
|
+
this.defaults = { uri: "mqtt://localhost:1883" };
|
|
64
|
+
this.port = -1;
|
|
65
|
+
this.address = undefined;
|
|
66
|
+
this.things = new Map();
|
|
67
|
+
this.config = config ?? this.defaults;
|
|
68
|
+
this.config.uri = this.config.uri ?? this.defaults.uri;
|
|
69
|
+
if (config.uri.indexOf("://") === -1) {
|
|
70
|
+
config.uri = this.scheme + "://" + config.uri;
|
|
71
|
+
}
|
|
72
|
+
this.brokerURI = config.uri;
|
|
73
|
+
}
|
|
74
|
+
async expose(thing) {
|
|
75
|
+
if (this.broker === undefined) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
let name = thing.title;
|
|
79
|
+
if (this.things.has(name)) {
|
|
80
|
+
const suffix = name.match(/.+_([0-9]+)$/);
|
|
81
|
+
if (suffix !== null) {
|
|
82
|
+
name = name.slice(0, -suffix[1].length) + (1 + parseInt(suffix[1]));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
name = name + "_2";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
debug(`MqttBrokerServer at ${this.brokerURI} exposes '${thing.title}' as unique '${name}/*'`);
|
|
89
|
+
this.things.set(name, thing);
|
|
90
|
+
for (const propertyName of Object.keys(thing.properties)) {
|
|
91
|
+
this.exposeProperty(name, propertyName, thing);
|
|
92
|
+
}
|
|
93
|
+
for (const actionName of Object.keys(thing.actions)) {
|
|
94
|
+
this.exposeAction(name, actionName, thing);
|
|
95
|
+
}
|
|
96
|
+
for (const eventName of Object.keys(thing.events)) {
|
|
97
|
+
this.exposeEvent(name, eventName, thing);
|
|
98
|
+
}
|
|
99
|
+
this.broker.on("message", this.handleMessage.bind(this));
|
|
100
|
+
this.broker.publish(name, JSON.stringify(thing.getThingDescription()), { retain: true });
|
|
101
|
+
}
|
|
102
|
+
exposeProperty(name, propertyName, thing) {
|
|
103
|
+
MqttBrokerServer.brokerIsInitialized(this.broker);
|
|
104
|
+
const topic = encodeURIComponent(name) + "/properties/" + encodeURIComponent(propertyName);
|
|
105
|
+
const property = thing.properties[propertyName];
|
|
106
|
+
const writeOnly = property.writeOnly ?? false;
|
|
107
|
+
if (!writeOnly) {
|
|
108
|
+
const href = this.brokerURI + "/" + topic;
|
|
109
|
+
const form = new core_1.Form(href, core_1.ContentSerdes.DEFAULT);
|
|
110
|
+
form.op = ["readproperty", "observeproperty", "unobserveproperty"];
|
|
111
|
+
property.forms.push(form);
|
|
112
|
+
debug(`MqttBrokerServer at ${this.brokerURI} assigns '${href}' to property '${propertyName}'`);
|
|
113
|
+
const observeListener = async (content) => {
|
|
114
|
+
debug(`MqttBrokerServer at ${this.brokerURI} publishing to Property topic '${propertyName}' `);
|
|
115
|
+
const buffer = await content.toBuffer();
|
|
116
|
+
if (this.broker === undefined) {
|
|
117
|
+
warn(`MqttBrokerServer at ${this.brokerURI} has no client to publish to. Probably it was closed.`);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.broker.publish(topic, buffer);
|
|
121
|
+
};
|
|
122
|
+
thing.handleObserveProperty(propertyName, observeListener, { formIndex: property.forms.length - 1 });
|
|
123
|
+
}
|
|
124
|
+
const readOnly = property.readOnly ?? false;
|
|
125
|
+
if (!readOnly) {
|
|
126
|
+
const href = this.brokerURI + "/" + topic + "/writeproperty";
|
|
127
|
+
this.broker.subscribe(topic + "/writeproperty");
|
|
128
|
+
const form = new core_1.Form(href, core_1.ContentSerdes.DEFAULT);
|
|
129
|
+
form.op = ["writeproperty"];
|
|
130
|
+
thing.properties[propertyName].forms.push(form);
|
|
131
|
+
debug(`MqttBrokerServer at ${this.brokerURI} assigns '${href}' to property '${propertyName}'`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exposeAction(name, actionName, thing) {
|
|
135
|
+
MqttBrokerServer.brokerIsInitialized(this.broker);
|
|
136
|
+
const topic = encodeURIComponent(name) + "/actions/" + encodeURIComponent(actionName);
|
|
137
|
+
this.broker.subscribe(topic);
|
|
138
|
+
const href = this.brokerURI + "/" + topic;
|
|
139
|
+
const form = new core_1.Form(href, core_1.ContentSerdes.DEFAULT);
|
|
140
|
+
form.op = ["invokeaction"];
|
|
141
|
+
thing.actions[actionName].forms.push(form);
|
|
142
|
+
debug(`MqttBrokerServer at ${this.brokerURI} assigns '${href}' to Action '${actionName}'`);
|
|
143
|
+
}
|
|
144
|
+
exposeEvent(name, eventName, thing) {
|
|
145
|
+
const topic = encodeURIComponent(name) + "/events/" + encodeURIComponent(eventName);
|
|
146
|
+
const event = thing.events[eventName];
|
|
147
|
+
const href = this.brokerURI + "/" + topic;
|
|
148
|
+
const form = new mqtt_1.MqttForm(href, core_1.ContentSerdes.DEFAULT);
|
|
149
|
+
form["mqv:qos"] = "2";
|
|
150
|
+
form.op = ["subscribeevent", "unsubscribeevent"];
|
|
151
|
+
event.forms.push(form);
|
|
152
|
+
debug(`MqttBrokerServer at ${this.brokerURI} assigns '${href}' to Event '${eventName}'`);
|
|
153
|
+
const eventListener = async (content) => {
|
|
154
|
+
if (this.broker === undefined) {
|
|
155
|
+
warn(`MqttBrokerServer at ${this.brokerURI} has no client to publish to. Probably it was closed.`);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (content == null) {
|
|
159
|
+
warn(`MqttBrokerServer on port ${this.getPort()} cannot process data for Event ${eventName}`);
|
|
160
|
+
thing.handleUnsubscribeEvent(eventName, eventListener, { formIndex: event.forms.length - 1 });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
debug(`MqttBrokerServer at ${this.brokerURI} publishing to Event topic '${eventName}' `);
|
|
164
|
+
const buffer = await content.toBuffer();
|
|
165
|
+
this.broker.publish(topic, buffer, { retain: form["mqv:retain"], qos: (0, util_1.mapQoS)(form["mqv:qos"]) });
|
|
166
|
+
};
|
|
167
|
+
thing.handleSubscribeEvent(eventName, eventListener, { formIndex: event.forms.length - 1 });
|
|
168
|
+
}
|
|
169
|
+
handleMessage(receivedTopic, rawPayload, packet) {
|
|
170
|
+
const segments = receivedTopic.split("/");
|
|
171
|
+
let payload;
|
|
172
|
+
if (rawPayload instanceof Buffer) {
|
|
173
|
+
payload = rawPayload;
|
|
174
|
+
}
|
|
175
|
+
else if (typeof rawPayload === "string") {
|
|
176
|
+
payload = Buffer.from(rawPayload);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
warn(`MqttBrokerServer on port ${this.getPort()} received unexpected payload type`);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (segments.length === this.ACTION_SEGMENT_LENGTH) {
|
|
183
|
+
debug(`MqttBrokerServer at ${this.brokerURI} received message for '${receivedTopic}'`);
|
|
184
|
+
const thing = this.things.get(segments[this.THING_NAME_SEGMENT_INDEX]);
|
|
185
|
+
if (thing != null) {
|
|
186
|
+
if (segments[this.INTERACTION_TYPE_SEGMENT_INDEX] === "actions") {
|
|
187
|
+
const action = thing.actions[segments[this.INTERACTION_NAME_SEGMENT_INDEX]];
|
|
188
|
+
if (action != null) {
|
|
189
|
+
this.handleAction(action, packet, payload, segments, thing);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else if (segments.length === this.PROPERTY_SEGMENT_LENGTH &&
|
|
196
|
+
segments[this.INTERACTION_EXT_SEGMENT_INDEX] === "writeproperty") {
|
|
197
|
+
const thing = this.things.get(segments[this.THING_NAME_SEGMENT_INDEX]);
|
|
198
|
+
if (thing != null) {
|
|
199
|
+
if (segments[this.INTERACTION_TYPE_SEGMENT_INDEX] === "properties") {
|
|
200
|
+
const property = thing.properties[segments[this.INTERACTION_NAME_SEGMENT_INDEX]];
|
|
201
|
+
if (property != null) {
|
|
202
|
+
this.handlePropertyWrite(property, packet, payload, segments, thing);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
warn(`MqttBrokerServer at ${this.brokerURI} received message for invalid topic '${receivedTopic}'`);
|
|
209
|
+
}
|
|
210
|
+
handleAction(action, packet, payload, segments, thing) {
|
|
211
|
+
const contentType = packet?.properties?.contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
212
|
+
const options = {
|
|
213
|
+
formIndex: core_1.ProtocolHelpers.findRequestMatchingFormIndex(action.forms, this.scheme, this.brokerURI, contentType),
|
|
214
|
+
};
|
|
215
|
+
const formContentType = action.forms[options.formIndex].contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
216
|
+
const inputContent = new core_1.Content(formContentType, stream_1.Readable.from(payload));
|
|
217
|
+
thing
|
|
218
|
+
.handleInvokeAction(segments[this.INTERACTION_NAME_SEGMENT_INDEX], inputContent, options)
|
|
219
|
+
.then((output) => {
|
|
220
|
+
if (output != null) {
|
|
221
|
+
warn(`MqttBrokerServer at ${this.brokerURI} cannot return output '${segments[this.INTERACTION_NAME_SEGMENT_INDEX]}'`);
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
.catch((err) => {
|
|
225
|
+
error(`MqttBrokerServer at ${this.brokerURI} got error on invoking '${segments[this.INTERACTION_NAME_SEGMENT_INDEX]}': ${err.message}`);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
handlePropertyWrite(property, packet, payload, segments, thing) {
|
|
229
|
+
const readOnly = property.readOnly ?? false;
|
|
230
|
+
if (!readOnly) {
|
|
231
|
+
const contentType = packet?.properties?.contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
232
|
+
const options = {
|
|
233
|
+
formIndex: core_1.ProtocolHelpers.findRequestMatchingFormIndex(property.forms, this.scheme, this.brokerURI, contentType),
|
|
234
|
+
};
|
|
235
|
+
const formContentType = property.forms[options.formIndex].contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
236
|
+
const inputContent = new core_1.Content(formContentType, stream_1.Readable.from(payload));
|
|
237
|
+
try {
|
|
238
|
+
thing.handleWriteProperty(segments[this.INTERACTION_NAME_SEGMENT_INDEX], inputContent, options);
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
error(`MqttBrokerServer at ${this.brokerURI} got error on writing to property '${segments[this.INTERACTION_NAME_SEGMENT_INDEX]}': ${err}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
warn(`MqttBrokerServer at ${this.brokerURI} received message for readOnly property at '${segments.join("/")}'`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async destroy(thingId) {
|
|
249
|
+
debug(`MqttBrokerServer on port ${this.getPort()} destroying thingId '${thingId}'`);
|
|
250
|
+
let removedThing;
|
|
251
|
+
for (const name of Array.from(this.things.keys())) {
|
|
252
|
+
const expThing = this.things.get(name);
|
|
253
|
+
if (expThing != null && expThing.id != null && expThing.id === thingId) {
|
|
254
|
+
this.things.delete(name);
|
|
255
|
+
removedThing = expThing;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (removedThing != null) {
|
|
259
|
+
info(`MqttBrokerServer successfully destroyed '${removedThing.title}'`);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
info(`MqttBrokerServer failed to destroy thing with thingId '${thingId}'`);
|
|
263
|
+
}
|
|
264
|
+
return removedThing !== undefined;
|
|
265
|
+
}
|
|
266
|
+
async start(servient) {
|
|
267
|
+
if (this.brokerURI === undefined) {
|
|
268
|
+
warn(`No broker defined for MQTT server binding - skipping`);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
const selfHost = this.config.selfHost ?? false;
|
|
272
|
+
if (selfHost) {
|
|
273
|
+
await this.startBroker();
|
|
274
|
+
}
|
|
275
|
+
if (this.config.psw === undefined) {
|
|
276
|
+
debug(`MqttBrokerServer trying to connect to broker at ${this.brokerURI}`);
|
|
277
|
+
}
|
|
278
|
+
else if (this.config.clientId === undefined) {
|
|
279
|
+
debug(`MqttBrokerServer trying to connect to secured broker at ${this.brokerURI}`);
|
|
280
|
+
}
|
|
281
|
+
else if (this.config.protocolVersion === undefined) {
|
|
282
|
+
debug(`MqttBrokerServer trying to connect to secured broker at ${this.brokerURI} with client ID ${this.config.clientId}`);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
debug(`MqttBrokerServer trying to connect to secured broker at ${this.brokerURI} with client ID ${this.config.clientId}`);
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
this.broker = await mqtt.connectAsync(this.brokerURI, this.config);
|
|
289
|
+
info(`MqttBrokerServer connected to broker at ${this.brokerURI}`);
|
|
290
|
+
const parsed = new url.URL(this.brokerURI);
|
|
291
|
+
this.address = parsed.hostname;
|
|
292
|
+
const port = parseInt(parsed.port);
|
|
293
|
+
this.port = port > 0 ? port : 1883;
|
|
294
|
+
}
|
|
295
|
+
catch (err) {
|
|
296
|
+
error(`MqttBrokerServer could not connect to broker at ${this.brokerURI}`);
|
|
297
|
+
throw err;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
async stop() {
|
|
302
|
+
if (this.broker !== undefined) {
|
|
303
|
+
this.broker.unsubscribe("*");
|
|
304
|
+
this.broker.end(true);
|
|
305
|
+
}
|
|
306
|
+
if (this.hostedBroker !== undefined) {
|
|
307
|
+
await new Promise((resolve) => this.hostedServer.close(() => resolve()));
|
|
308
|
+
await new Promise((resolve) => this.hostedBroker.close(() => resolve()));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
getPort() {
|
|
312
|
+
return this.port;
|
|
313
|
+
}
|
|
314
|
+
getAddress() {
|
|
315
|
+
return this.address;
|
|
316
|
+
}
|
|
317
|
+
selfHostAuthentication(_client, username, password, done) {
|
|
318
|
+
if (this.config.selfHostAuthentication && username !== undefined) {
|
|
319
|
+
for (let i = 0; i < this.config.selfHostAuthentication.length; i++) {
|
|
320
|
+
if (username === this.config.selfHostAuthentication[i].username &&
|
|
321
|
+
password !== undefined &&
|
|
322
|
+
password.equals(Buffer.from(this.config.selfHostAuthentication[i].password ?? ""))) {
|
|
323
|
+
done(null, true);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
done(null, false);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
done(null, true);
|
|
331
|
+
}
|
|
332
|
+
async startBroker() {
|
|
333
|
+
return new Promise((resolve, reject) => {
|
|
334
|
+
this.hostedServer = new aedes_1.default({});
|
|
335
|
+
let server;
|
|
336
|
+
if (this.config.key) {
|
|
337
|
+
server = tls.createServer({ key: this.config.key, cert: this.config.cert }, this.hostedServer.handle);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
server = net.createServer(this.hostedServer.handle);
|
|
341
|
+
}
|
|
342
|
+
const parsed = new url.URL(this.brokerURI);
|
|
343
|
+
const port = parseInt(parsed.port);
|
|
344
|
+
this.port = port > 0 ? port : 1883;
|
|
345
|
+
this.hostedServer.authenticate = this.selfHostAuthentication.bind(this);
|
|
346
|
+
const errorListener = (err) => {
|
|
347
|
+
error(`error listening for ${this.brokerURI}, ${err}`);
|
|
348
|
+
reject(err);
|
|
349
|
+
};
|
|
350
|
+
server.once("error", errorListener);
|
|
351
|
+
debug(`MqttBrokerServer creating server for ${this.brokerURI}`);
|
|
352
|
+
this.hostedBroker = server.listen(port, parsed.hostname, () => {
|
|
353
|
+
debug(`MqttBrokerServer listening ${this.brokerURI}`);
|
|
354
|
+
server.removeListener("error", errorListener);
|
|
355
|
+
resolve();
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
exports.default = MqttBrokerServer;
|
|
361
|
+
//# sourceMappingURL=mqtt-broker-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtt-broker-server.js","sourceRoot":"","sources":["../src/mqtt-broker-server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,2CAA6B;AAC7B,yCAA2B;AAC3B,kDAA0B;AAE1B,yCAA2B;AAC3B,yCAA2B;AAC3B,iCAA0D;AAC1D,yCASwB;AAGxB,mCAAkC;AAClC,iCAAgC;AAEhC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAA,oBAAa,EAAC,cAAc,EAAE,oBAAoB,CAAC,CAAC;AAEzF,MAAqB,gBAAgB;IACzB,MAAM,CAAC,mBAAmB,CAAC,MAAwB;QACvD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CACX,iDAAiD,gBAAgB,CAAC,IAAI,gCAAgC,CACzG,CAAC;QACN,CAAC;IACL,CAAC;IA4BD,YAAY,MAA8B;QA1BjC,WAAM,GAAW,MAAM,CAAC;QAEhB,0BAAqB,GAAG,CAAC,CAAC;QAC1B,4B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,CAAC,CAAC;gBAC9C,OAAO,EAAE,CAAC;YACd,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AAlbD,mCAkbC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ProtocolClientFactory, ProtocolClient } from "@node-wot/core";
|
|
2
|
+
export default class MqttClientFactory implements ProtocolClientFactory {
|
|
3
|
+
readonly scheme: string;
|
|
4
|
+
private readonly clients;
|
|
5
|
+
getClient(): ProtocolClient;
|
|
6
|
+
init(): boolean;
|
|
7
|
+
destroy(): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const core_1 = require("@node-wot/core");
|
|
7
|
+
const mqtt_client_1 = __importDefault(require("./mqtt-client"));
|
|
8
|
+
const debug = (0, core_1.createDebugLogger)("binding-mqtt", "mqtt-client-factory");
|
|
9
|
+
class MqttClientFactory {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.scheme = "mqtt";
|
|
12
|
+
this.clients = [];
|
|
13
|
+
}
|
|
14
|
+
getClient() {
|
|
15
|
+
const client = new mqtt_client_1.default();
|
|
16
|
+
this.clients.push(client);
|
|
17
|
+
return client;
|
|
18
|
+
}
|
|
19
|
+
init() {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
destroy() {
|
|
23
|
+
debug(`MqttClientFactory stopping all clients for '${this.scheme}'`);
|
|
24
|
+
this.clients.forEach((client) => client.stop());
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = MqttClientFactory;
|
|
29
|
+
//# sourceMappingURL=mqtt-client-factory.js.map
|