@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtt-client-factory.js","sourceRoot":"","sources":["../src/mqtt-client-factory.ts"],"names":[],"mappings":";;;;;AAmBA,yCAA0F;AAC1F,gEAAuC;AAEvC,MAAM,KAAK,GAAG,IAAA,wBAAiB,EAAC,cAAc,EAAE,qBAAqB,CAAC,CAAC;AAEvE,MAAqB,iBAAiB;IAAtC;QACoB,WAAM,GAAW,MAAM,CAAC;QACvB,YAAO,GAA0B,EAAE,CAAC;IAiBzD,CAAC;IAfG,SAAS;QACL,MAAM,MAAM,GAAG,IAAI,qBAAU,EAAE,CAAC;QAChC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,IAAI;QACA,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,OAAO;QACH,KAAK,CAAC,+CAA+C,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QACrE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAnBD,oCAmBC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ProtocolClient, Content, Form, SecurityScheme } from "@node-wot/core";
|
|
2
|
+
import { MqttClientConfig, MqttForm } from "./mqtt";
|
|
3
|
+
import { Subscription } from "rxjs/Subscription";
|
|
4
|
+
declare interface MqttClientSecurityParameters {
|
|
5
|
+
username: string;
|
|
6
|
+
password: string;
|
|
7
|
+
}
|
|
8
|
+
export default class MqttClient implements ProtocolClient {
|
|
9
|
+
private config;
|
|
10
|
+
private scheme;
|
|
11
|
+
private pools;
|
|
12
|
+
constructor(config?: MqttClientConfig, secure?: boolean);
|
|
13
|
+
private client?;
|
|
14
|
+
private publishAndWait;
|
|
15
|
+
subscribeResource(form: MqttForm, next: (value: Content) => void, error?: (error: Error) => void, complete?: () => void): Promise<Subscription>;
|
|
16
|
+
readResource(form: MqttForm): Promise<Content>;
|
|
17
|
+
writeResource(form: MqttForm, content: Content): Promise<void | Content>;
|
|
18
|
+
invokeResource(form: MqttForm, content: Content): Promise<Content>;
|
|
19
|
+
unlinkResource(form: Form): Promise<void>;
|
|
20
|
+
requestThingDescription(uri: string): Promise<Content>;
|
|
21
|
+
start(): Promise<void>;
|
|
22
|
+
stop(): Promise<void>;
|
|
23
|
+
setSecurity(metadata: Array<SecurityScheme>, credentials?: MqttClientSecurityParameters): boolean;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,261 @@
|
|
|
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 core_1 = require("@node-wot/core");
|
|
40
|
+
const url = __importStar(require("url"));
|
|
41
|
+
const Subscription_1 = require("rxjs/Subscription");
|
|
42
|
+
const stream_1 = require("stream");
|
|
43
|
+
const mqtt_message_pool_1 = __importDefault(require("./mqtt-message-pool"));
|
|
44
|
+
const util_1 = require("./util");
|
|
45
|
+
const { debug, warn } = (0, core_1.createLoggers)("binding-mqtt", "mqtt-client");
|
|
46
|
+
const DEFAULT_TIMEOUT = 5000;
|
|
47
|
+
class MqttClient {
|
|
48
|
+
constructor(config = {}, secure = false) {
|
|
49
|
+
this.config = config;
|
|
50
|
+
this.pools = new Map();
|
|
51
|
+
this.scheme = "mqtt" + (secure ? "s" : "");
|
|
52
|
+
}
|
|
53
|
+
async publishAndWait(pool, topic, buffer, options, responseTopic, contentType) {
|
|
54
|
+
let resolve;
|
|
55
|
+
let reject;
|
|
56
|
+
const responsePromise = new Promise((res, rej) => {
|
|
57
|
+
resolve = res;
|
|
58
|
+
reject = rej;
|
|
59
|
+
});
|
|
60
|
+
let timer;
|
|
61
|
+
const messageHandler = (topic, message, packet) => {
|
|
62
|
+
if (timer)
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
resolve(new core_1.Content(contentType, stream_1.Readable.from(message), packet));
|
|
65
|
+
};
|
|
66
|
+
await pool.subscribe(responseTopic, messageHandler, (e) => {
|
|
67
|
+
if (timer)
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
reject(e);
|
|
70
|
+
});
|
|
71
|
+
timer = setTimeout(() => {
|
|
72
|
+
pool.unsubscribe(responseTopic).catch(() => { });
|
|
73
|
+
reject(new Error(`Timeout waiting for response on topic ${responseTopic}`));
|
|
74
|
+
}, this.config.timeout ?? DEFAULT_TIMEOUT);
|
|
75
|
+
try {
|
|
76
|
+
await pool.publish(topic, buffer, options);
|
|
77
|
+
const result = await responsePromise;
|
|
78
|
+
await pool.unsubscribe(responseTopic);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (timer)
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
await pool.unsubscribe(responseTopic).catch(() => { });
|
|
85
|
+
throw err;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async subscribeResource(form, next, error, complete) {
|
|
89
|
+
const contentType = form.contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
90
|
+
const requestUri = new url.URL(form.href);
|
|
91
|
+
const brokerUri = `${this.scheme}://` + requestUri.host;
|
|
92
|
+
const _path = requestUri.pathname.slice(1) || '';
|
|
93
|
+
const filter = _path.length ? _path : form["mqv:filter"];
|
|
94
|
+
if (!filter) {
|
|
95
|
+
throw new Error("No topic or filter provided");
|
|
96
|
+
}
|
|
97
|
+
let pool = this.pools.get(brokerUri);
|
|
98
|
+
if (pool == null) {
|
|
99
|
+
pool = new mqtt_message_pool_1.default();
|
|
100
|
+
this.pools.set(brokerUri, pool);
|
|
101
|
+
}
|
|
102
|
+
await pool.connect(brokerUri, this.config);
|
|
103
|
+
await pool.subscribe(filter, (topic, message, packet) => {
|
|
104
|
+
next(new core_1.Content(contentType, stream_1.Readable.from(message), packet));
|
|
105
|
+
}, (e) => {
|
|
106
|
+
if (error)
|
|
107
|
+
error(e);
|
|
108
|
+
});
|
|
109
|
+
return new Subscription_1.Subscription(() => { });
|
|
110
|
+
}
|
|
111
|
+
async readResource(form) {
|
|
112
|
+
const contentType = form.contentType ?? core_1.ContentSerdes.DEFAULT;
|
|
113
|
+
const requestUri = new url.URL(form.href);
|
|
114
|
+
const brokerUri = `${this.scheme}://` + requestUri.host;
|
|
115
|
+
const _path = requestUri.pathname.slice(1) || '';
|
|
116
|
+
let pool = this.pools.get(brokerUri);
|
|
117
|
+
if (pool == null) {
|
|
118
|
+
pool = new mqtt_message_pool_1.default();
|
|
119
|
+
this.pools.set(brokerUri, pool);
|
|
120
|
+
}
|
|
121
|
+
await pool.connect(brokerUri, this.config);
|
|
122
|
+
const filter = _path.length ? _path : form["mqv:filter"];
|
|
123
|
+
const explicitResponseTopic = form["mqv:properties"] && form["mqv:properties"].responseTopic;
|
|
124
|
+
const useDefaultResponseTopic = !form["mqv:properties"] && !form["mqv:retain"];
|
|
125
|
+
if (explicitResponseTopic || useDefaultResponseTopic) {
|
|
126
|
+
const filterString = typeof filter === "string" ? filter : undefined;
|
|
127
|
+
const responseTopic = explicitResponseTopic || (filterString ? `${filterString}/response` : undefined);
|
|
128
|
+
if (!responseTopic) {
|
|
129
|
+
throw new Error("No response topic could be determined");
|
|
130
|
+
}
|
|
131
|
+
const topic = _path.length ? _path : (form["mqv:topic"] || filterString);
|
|
132
|
+
if (!topic) {
|
|
133
|
+
throw new Error("No topic provided for read request");
|
|
134
|
+
}
|
|
135
|
+
const options = {
|
|
136
|
+
qos: (0, util_1.mapQoS)(form["mqv:qos"]),
|
|
137
|
+
retain: form["mqv:retain"],
|
|
138
|
+
properties: form["mqv:properties"],
|
|
139
|
+
};
|
|
140
|
+
return this.publishAndWait(pool, topic, Buffer.from(""), options, responseTopic, contentType);
|
|
141
|
+
}
|
|
142
|
+
if (!filter) {
|
|
143
|
+
throw new Error("No topic or filter provided");
|
|
144
|
+
}
|
|
145
|
+
let timer;
|
|
146
|
+
const result = await new Promise((resolve, reject) => {
|
|
147
|
+
timer = setTimeout(() => {
|
|
148
|
+
reject(new Error(`Timeout waiting for message on topic ${filter}`));
|
|
149
|
+
}, this.config.timeout ?? DEFAULT_TIMEOUT);
|
|
150
|
+
pool.subscribe(filter, (topic, message, packet) => {
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
resolve(new core_1.Content(contentType, stream_1.Readable.from(message), packet));
|
|
153
|
+
}, (e) => {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
reject(e);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
await pool.unsubscribe(filter);
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
async writeResource(form, content) {
|
|
162
|
+
const requestUri = new url.URL(form.href);
|
|
163
|
+
const brokerUri = `${this.scheme}://${requestUri.host}`;
|
|
164
|
+
const _path = requestUri.pathname.slice(1) || '';
|
|
165
|
+
const topic = _path.length ? _path : form["mqv:topic"];
|
|
166
|
+
if (!topic) {
|
|
167
|
+
throw new Error("No topic provided");
|
|
168
|
+
}
|
|
169
|
+
let pool = this.pools.get(brokerUri);
|
|
170
|
+
if (pool == null) {
|
|
171
|
+
pool = new mqtt_message_pool_1.default();
|
|
172
|
+
this.pools.set(brokerUri, pool);
|
|
173
|
+
}
|
|
174
|
+
await pool.connect(brokerUri, this.config);
|
|
175
|
+
const buffer = content === undefined ? Buffer.from("") : await content.toBuffer();
|
|
176
|
+
const options = {
|
|
177
|
+
retain: form["mqv:retain"],
|
|
178
|
+
qos: (0, util_1.mapQoS)(form["mqv:qos"]),
|
|
179
|
+
};
|
|
180
|
+
if (content && content.meta && content.meta.properties) {
|
|
181
|
+
options.properties = content.meta.properties;
|
|
182
|
+
}
|
|
183
|
+
if (options.properties && options.properties.responseTopic) {
|
|
184
|
+
return this.publishAndWait(pool, topic, buffer, options, options.properties.responseTopic, form.contentType ?? core_1.ContentSerdes.DEFAULT);
|
|
185
|
+
}
|
|
186
|
+
await pool.publish(topic, buffer, options);
|
|
187
|
+
}
|
|
188
|
+
async invokeResource(form, content) {
|
|
189
|
+
const requestUri = new url.URL(form.href);
|
|
190
|
+
const brokerUri = `${this.scheme}://${requestUri.host}`;
|
|
191
|
+
const _path = requestUri.pathname.slice(1) || '';
|
|
192
|
+
const topic = _path.length ? _path : form["mqv:topic"];
|
|
193
|
+
if (!topic) {
|
|
194
|
+
throw new Error("No topic provided");
|
|
195
|
+
}
|
|
196
|
+
let pool = this.pools.get(brokerUri);
|
|
197
|
+
if (pool == null) {
|
|
198
|
+
pool = new mqtt_message_pool_1.default();
|
|
199
|
+
this.pools.set(brokerUri, pool);
|
|
200
|
+
}
|
|
201
|
+
await pool.connect(brokerUri, this.config);
|
|
202
|
+
const buffer = content === undefined ? Buffer.from("") : await content.toBuffer();
|
|
203
|
+
const options = {
|
|
204
|
+
retain: form["mqv:retain"],
|
|
205
|
+
qos: (0, util_1.mapQoS)(form["mqv:qos"]),
|
|
206
|
+
};
|
|
207
|
+
if (content && content.meta && content.meta.properties) {
|
|
208
|
+
options.properties = content.meta.properties;
|
|
209
|
+
}
|
|
210
|
+
if (options.properties && options.properties.responseTopic) {
|
|
211
|
+
return this.publishAndWait(pool, topic, buffer, options, options.properties.responseTopic, form.contentType ?? core_1.ContentSerdes.DEFAULT);
|
|
212
|
+
}
|
|
213
|
+
await pool.publish(topic, buffer, options);
|
|
214
|
+
return new core_1.DefaultContent(stream_1.Readable.from([]));
|
|
215
|
+
}
|
|
216
|
+
async unlinkResource(form) {
|
|
217
|
+
const requestUri = new url.URL(form.href);
|
|
218
|
+
const brokerUri = `${this.scheme}://` + requestUri.host;
|
|
219
|
+
const _path = requestUri.pathname.slice(1) || '';
|
|
220
|
+
const topic = _path.length ? _path : form["mqv:filter"];
|
|
221
|
+
if (!topic) {
|
|
222
|
+
throw new Error("No topic or filter provided");
|
|
223
|
+
}
|
|
224
|
+
const pool = this.pools.get(brokerUri);
|
|
225
|
+
if (pool != null) {
|
|
226
|
+
await pool.unsubscribe(topic);
|
|
227
|
+
debug(`MqttClient unsubscribed from topic '${topic}'`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async requestThingDescription(uri) {
|
|
231
|
+
throw new Error("Method not implemented");
|
|
232
|
+
}
|
|
233
|
+
async start() {
|
|
234
|
+
}
|
|
235
|
+
async stop() {
|
|
236
|
+
for (const pool of this.pools.values()) {
|
|
237
|
+
await pool.end();
|
|
238
|
+
}
|
|
239
|
+
if (this.client)
|
|
240
|
+
return this.client.endAsync();
|
|
241
|
+
}
|
|
242
|
+
setSecurity(metadata, credentials) {
|
|
243
|
+
if (metadata === undefined || !Array.isArray(metadata) || metadata.length === 0) {
|
|
244
|
+
warn(`MqttClient received empty security metadata`);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
const security = metadata[0];
|
|
248
|
+
if (security.scheme === "basic") {
|
|
249
|
+
if (credentials === undefined) {
|
|
250
|
+
throw new Error("binding-mqtt: security wants to be basic but you have provided no credentials");
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
this.config.username = credentials.username;
|
|
254
|
+
this.config.password = credentials.password;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
exports.default = MqttClient;
|
|
261
|
+
//# sourceMappingURL=mqtt-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtt-client.js","sourceRoot":"","sources":["../src/mqtt-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,yCAQwB;AAGxB,yCAA2B;AAC3B,oDAAiD;AACjD,mCAAkC;AAClC,4EAAkD;AAClD,iCAAgC;AAEhC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAA,oBAAa,EAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AAErE,MAAM,eAAe,GAAG,IAAI,CAAC;AAO7B,MAAqB,UAAU;IAI3B,YACY,SAA2B,EAAE,EACrC,MAAM,GAAG,KAAK;QADN,WAAM,GAAN,MAAM,CAAuB;QAHjC,UAAK,GAAiC,IAAI,GAAG,EAAE,CAAC;QAMpD,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/C,CAAC;IAIO,KAAK,CAAC,cAAc,CACxB,IAAqB,EACrB,KAAa,EACb,MAAc,EACd,OAAmC,EACnC,aAAqB,EACrB,WAAmB;QAEnB,IAAI,OAAwD,CAAC;QAC7D,IAAI,MAA8B,CAAC;QACnC,MAAM,eAAe,GAAG,IAAI,OAAO,CAAU,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YACtD,OAAO,GAAG,GAAG,CAAC;YACd,MAAM,GAAG,GAAG,CAAC;QACjB,CAAC,CAAC,CAAC;QAEH,IAAI,KAAqB,CA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AM,EAAE,EAAE,CAAC;YACrC,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC;QACrB,CAAC;QACD,IAAI,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IACnD,CAAC;IAEM,WAAW,CAAC,QAA+B,EAAE,WAA0C;QAC1F,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9E,IAAI,CAAC,6CAA6C,CAAC,CAAC;YACpD,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,QAAQ,GAAmB,QAAQ,CAAC,CAAC,CAAC,CAAC;QAE7C,IAAI,QAAQ,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;YAC9B,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;gBAE5B,MAAM,IAAI,KAAK,CAAC,+EAA+E,CAAC,CAAC;YACrG,CAAC;iBAAM,CAAC;gBACJ,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;gBAC5C,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;YAChD,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAzUD,6BAyUC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MqttClientConfig } from "./mqtt";
|
|
2
|
+
import * as mqtt from "mqtt";
|
|
3
|
+
export default class MQTTMessagePool {
|
|
4
|
+
client?: mqtt.MqttClient;
|
|
5
|
+
subscribers: Map<string, (topic: string, message: Buffer, packet: mqtt.IPublishPacket) => void>;
|
|
6
|
+
errors: Map<string, (error: Error) => void>;
|
|
7
|
+
connect(brokerURI: string, config: MqttClientConfig): Promise<void>;
|
|
8
|
+
subscribe(filter: string | string[], callback: (topic: string, message: Buffer, packet: mqtt.IPublishPacket) => void, error: (error: Error) => void): Promise<void>;
|
|
9
|
+
unsubscribe(filter: string | string[]): Promise<void>;
|
|
10
|
+
publish(topic: string, message: Buffer, options?: mqtt.IClientPublishOptions): Promise<void>;
|
|
11
|
+
end(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const core_1 = require("@node-wot/core");
|
|
37
|
+
const mqtt = __importStar(require("mqtt"));
|
|
38
|
+
const { debug, warn } = (0, core_1.createLoggers)("binding-mqtt", "mqtt-message-pool");
|
|
39
|
+
class MQTTMessagePool {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.subscribers = new Map();
|
|
42
|
+
this.errors = new Map();
|
|
43
|
+
}
|
|
44
|
+
async connect(brokerURI, config) {
|
|
45
|
+
if (this.client === undefined) {
|
|
46
|
+
this.client = await mqtt.connectAsync(brokerURI, config);
|
|
47
|
+
this.client.on("message", (receivedTopic, payload, packet) => {
|
|
48
|
+
debug(`Received MQTT message from ${brokerURI} (topic: ${receivedTopic}, data length: ${payload.length})`);
|
|
49
|
+
this.subscribers.get(receivedTopic)?.(receivedTopic, payload, packet);
|
|
50
|
+
});
|
|
51
|
+
this.client.on("error", (error) => {
|
|
52
|
+
warn(`MQTT client error: ${error.message}`);
|
|
53
|
+
this.errors.forEach((errorCallback) => {
|
|
54
|
+
errorCallback(error);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async subscribe(filter, callback, error) {
|
|
60
|
+
if (this.client == null) {
|
|
61
|
+
throw new Error("MQTT client is not connected");
|
|
62
|
+
}
|
|
63
|
+
const filters = Array.isArray(filter) ? filter : [filter];
|
|
64
|
+
filters.forEach((f) => {
|
|
65
|
+
if (this.subscribers.has(f)) {
|
|
66
|
+
warn(`Already subscribed to ${f}; we are not supporting multiple subscribers to the same topic`);
|
|
67
|
+
warn(`The subscription will be ignored`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.subscribers.set(f, callback);
|
|
71
|
+
this.errors.set(f, error);
|
|
72
|
+
});
|
|
73
|
+
await this.client.subscribeAsync(filters);
|
|
74
|
+
}
|
|
75
|
+
async unsubscribe(filter) {
|
|
76
|
+
if (this.client == null) {
|
|
77
|
+
throw new Error("MQTT client is not connected");
|
|
78
|
+
}
|
|
79
|
+
const filters = Array.isArray(filter) ? filter : [filter];
|
|
80
|
+
filters.forEach((f) => {
|
|
81
|
+
this.subscribers.delete(f);
|
|
82
|
+
this.errors.delete(f);
|
|
83
|
+
});
|
|
84
|
+
await this.client.unsubscribeAsync(filters);
|
|
85
|
+
}
|
|
86
|
+
async publish(topic, message, options) {
|
|
87
|
+
if (this.client == null) {
|
|
88
|
+
throw new Error("MQTT client is not connected");
|
|
89
|
+
}
|
|
90
|
+
debug(`Publishing MQTT message to ${topic} (data length: ${message.length})`);
|
|
91
|
+
await this.client.publishAsync(topic, message, options);
|
|
92
|
+
}
|
|
93
|
+
async end() {
|
|
94
|
+
for (const filter of this.subscribers.keys()) {
|
|
95
|
+
this.unsubscribe(filter);
|
|
96
|
+
}
|
|
97
|
+
return this.client?.endAsync();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.default = MQTTMessagePool;
|
|
101
|
+
//# sourceMappingURL=mqtt-message-pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtt-message-pool.js","sourceRoot":"","sources":["../src/mqtt-message-pool.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,yCAA+C;AAE/C,2CAA6B;AAE7B,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAA,oBAAa,EAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC;AAE3E,MAAqB,eAAe;IAApC;QAEI,gBAAW,GAAuF,IAAI,GAAG,EAAE,CAAC;QAC5G,WAAM,GAAwC,IAAI,GAAG,EAAE,CAAC;IA4E5D,CAAC;IA1EU,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,MAAwB;QAC5D,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC5B,I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K,CAAC,GAAG;QACZ,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC;YAC3C,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnC,CAAC;CACJ;AA/ED,kCA+EC"}
|
package/dist/mqtt.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Form } from "@node-wot/core";
|
|
2
|
+
export { default as MqttClient } from "./mqtt-client";
|
|
3
|
+
export { default as MqttClientFactory } from "./mqtt-client-factory";
|
|
4
|
+
export { default as MqttsClientFactory } from "./mqtts-client-factory";
|
|
5
|
+
export { default as MqttBrokerServer } from "./mqtt-broker-server";
|
|
6
|
+
export * from "./mqtt-client";
|
|
7
|
+
export * from "./mqtt-client-factory";
|
|
8
|
+
export * from "./mqtts-client-factory";
|
|
9
|
+
export * from "./mqtt-broker-server";
|
|
10
|
+
export type MqttQoS = "0" | "1" | "2";
|
|
11
|
+
export declare class MqttForm extends Form {
|
|
12
|
+
"mqv:qos"?: MqttQoS;
|
|
13
|
+
"mqv:retain"?: boolean;
|
|
14
|
+
"mqv:topic"?: string;
|
|
15
|
+
"mqv:filter"?: string | string[];
|
|
16
|
+
"mqv:controlPacket"?: "publish" | "subscribe" | "unsubscribe";
|
|
17
|
+
"mqv:properties"?: {
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface MqttClientConfig {
|
|
22
|
+
username?: string;
|
|
23
|
+
password?: string;
|
|
24
|
+
rejectUnauthorized?: boolean;
|
|
25
|
+
protocolVersion?: 3 | 4 | 5;
|
|
26
|
+
timeout?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface MqttBrokerServerConfig {
|
|
29
|
+
uri: string;
|
|
30
|
+
user?: string;
|
|
31
|
+
psw?: string;
|
|
32
|
+
clientId?: string;
|
|
33
|
+
protocolVersion?: 3 | 4 | 5;
|
|
34
|
+
rejectUnauthorized?: boolean;
|
|
35
|
+
selfHost?: boolean;
|
|
36
|
+
key?: Buffer;
|
|
37
|
+
cert?: Buffer | undefined;
|
|
38
|
+
selfHostAuthentication?: MqttClientConfig[];
|
|
39
|
+
}
|
package/dist/mqtt.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.MqttForm = exports.MqttBrokerServer = exports.MqttsClientFactory = exports.MqttClientFactory = exports.MqttClient = void 0;
|
|
21
|
+
const core_1 = require("@node-wot/core");
|
|
22
|
+
var mqtt_client_1 = require("./mqtt-client");
|
|
23
|
+
Object.defineProperty(exports, "MqttClient", { enumerable: true, get: function () { return __importDefault(mqtt_client_1).default; } });
|
|
24
|
+
var mqtt_client_factory_1 = require("./mqtt-client-factory");
|
|
25
|
+
Object.defineProperty(exports, "MqttClientFactory", { enumerable: true, get: function () { return __importDefault(mqtt_client_factory_1).default; } });
|
|
26
|
+
var mqtts_client_factory_1 = require("./mqtts-client-factory");
|
|
27
|
+
Object.defineProperty(exports, "MqttsClientFactory", { enumerable: true, get: function () { return __importDefault(mqtts_client_factory_1).default; } });
|
|
28
|
+
var mqtt_broker_server_1 = require("./mqtt-broker-server");
|
|
29
|
+
Object.defineProperty(exports, "MqttBrokerServer", { enumerable: true, get: function () { return __importDefault(mqtt_broker_server_1).default; } });
|
|
30
|
+
__exportStar(require("./mqtt-client"), exports);
|
|
31
|
+
__exportStar(require("./mqtt-client-factory"), exports);
|
|
32
|
+
__exportStar(require("./mqtts-client-factory"), exports);
|
|
33
|
+
__exportStar(require("./mqtt-broker-server"), exports);
|
|
34
|
+
class MqttForm extends core_1.Form {
|
|
35
|
+
constructor() {
|
|
36
|
+
super(...arguments);
|
|
37
|
+
this["mqv:qos"] = "0";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.MqttForm = MqttForm;
|
|
41
|
+
//# sourceMappingURL=mqtt.js.map
|
package/dist/mqtt.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtt.js","sourceRoot":"","sources":["../src/mqtt.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAmBA,yCAAsC;AAEtC,6CAAsD;AAA7C,0HAAA,OAAO,OAAc;AAC9B,6DAAqE;AAA5D,yIAAA,OAAO,OAAqB;AACrC,+DAAuE;AAA9D,2IAAA,OAAO,OAAsB;AACtC,2DAAmE;AAA1D,uIAAA,OAAO,OAAoB;AAEpC,gDAA8B;AAC9B,wDAAsC;AACtC,yDAAuC;AACvC,uDAAqC;AAGrC,MAAa,QAAS,SAAQ,WAAI;IAAlC;;QACW,eAAS,GAAa,GAAG,CAAC;IAYrC,CAAC;CAAA;AAbD,4BAaC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ProtocolClientFactory, ProtocolClient } from "@node-wot/core";
|
|
2
|
+
import { MqttClientConfig } from "./mqtt";
|
|
3
|
+
export default class MqttsClientFactory implements ProtocolClientFactory {
|
|
4
|
+
private readonly config;
|
|
5
|
+
readonly scheme: string;
|
|
6
|
+
private readonly clients;
|
|
7
|
+
constructor(config: MqttClientConfig);
|
|
8
|
+
getClient(): ProtocolClient;
|
|
9
|
+
init(): boolean;
|
|
10
|
+
destroy(): boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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", "mqtts-client-factory");
|
|
9
|
+
class MqttsClientFactory {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.scheme = "mqtts";
|
|
13
|
+
this.clients = [];
|
|
14
|
+
}
|
|
15
|
+
getClient() {
|
|
16
|
+
const client = new mqtt_client_1.default(this.config, true);
|
|
17
|
+
this.clients.push(client);
|
|
18
|
+
return client;
|
|
19
|
+
}
|
|
20
|
+
init() {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
destroy() {
|
|
24
|
+
debug(`MqttClientFactory stopping all clients for '${this.scheme}'`);
|
|
25
|
+
this.clients.forEach((client) => client.stop());
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.default = MqttsClientFactory;
|
|
30
|
+
//# sourceMappingURL=mqtts-client-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mqtts-client-factory.js","sourceRoot":"","sources":["../src/mqtts-client-factory.ts"],"names":[],"mappings":";;;;;AAmBA,yCAA0F;AAE1F,gEAAuC;AAEvC,MAAM,KAAK,GAAG,IAAA,wBAAiB,EAAC,cAAc,EAAE,sBAAsB,CAAC,CAAC;AAExE,MAAqB,kBAAkB;IAInC,YAA6B,MAAwB;QAAxB,WAAM,GAAN,MAAM,CAAkB;QAHrC,WAAM,GAAW,OAAO,CAAC;QACxB,YAAO,GAA0B,EAAE,CAAC;IAEG,CAAC;IACzD,SAAS;QACL,MAAM,MAAM,GAAG,IAAI,qBAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACjD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1B,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,IAAI;QACA,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,OAAO;QACH,KAAK,CAAC,+CAA+C,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QACrE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AApBD,qCAoBC"}
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapQoS = mapQoS;
|
|
4
|
+
const core_1 = require("@node-wot/core");
|
|
5
|
+
const { warn } = (0, core_1.createLoggers)("binding-mqtt", "mqtt-util");
|
|
6
|
+
function mapQoS(qos) {
|
|
7
|
+
switch (qos) {
|
|
8
|
+
case "0":
|
|
9
|
+
return 0;
|
|
10
|
+
case "1":
|
|
11
|
+
return 1;
|
|
12
|
+
case "2":
|
|
13
|
+
return 2;
|
|
14
|
+
case undefined:
|
|
15
|
+
return 0;
|
|
16
|
+
default:
|
|
17
|
+
warn(`MqttClient received unsupported QoS level '${qos}'`);
|
|
18
|
+
warn(`MqttClient falling back to QoS level '0'`);
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=util.js.map
|
package/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAoBA,wBAeC;AArBD,yCAA+C;AAI/C,MAAM,EAAE,IAAI,EAAE,GAAG,IAAA,oBAAa,EAAC,cAAc,EAAE,WAAW,CAAC,CAAC;AAE5D,SAAgB,MAAM,CAAC,GAAwB;IAC3C,QAAQ,GAAG,EAAE,CAAC;QACV,KAAK,GAAG;YACJ,OAAO,CAAC,CAAC;QACb,KAAK,GAAG;YACJ,OAAO,CAAC,CAAC;QACb,KAAK,GAAG;YACJ,OAAO,CAAC,CAAC;QACb,KAAK,SAAS;YACV,OAAO,CAAC,CAAC;QACb;YACI,IAAI,CAAC,8CAA8C,GAAG,GAAG,CAAC,CAAC;YAC3D,IAAI,CAAC,0CAA0C,CAAC,CAAC;YACjD,OAAO,CAAC,CAAC;IACjB,CAAC;AACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yidingdian/binding-mqtt",
|
|
3
|
+
"version": "0.9.2",
|
|
4
|
+
"description": "MQTT binding for m-node-wot",
|
|
5
|
+
"author": "yidingdian <iadom@163.com>",
|
|
6
|
+
"license": "EPL-2.0 OR W3C-20150513",
|
|
7
|
+
"repository": "https://github.com/yidingdian/m-node-wot/tree/master/packages/binding-mqtt",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist/"
|
|
13
|
+
],
|
|
14
|
+
"main": "dist/mqtt.js",
|
|
15
|
+
"types": "dist/mqtt.d.ts",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@yidingdian/core": "0.9.2",
|
|
18
|
+
"aedes": "^0.51.3",
|
|
19
|
+
"mqtt": "^5.3.2",
|
|
20
|
+
"rxjs": "^5.5.11"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -b",
|
|
24
|
+
"test": "mocha --require ts-node/register --extension ts",
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"lint:fix": "eslint . --fix",
|
|
27
|
+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"**/*.json\""
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/yidingdian/m-node-wot/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/yidingdian/m-node-wot/tree/master/packages/binding-mqtt#readme",
|
|
33
|
+
"directories": {
|
|
34
|
+
"test": "test"
|
|
35
|
+
}
|
|
36
|
+
}
|