@wiotp/sdk 0.4.2 → 0.6.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.
Files changed (48) hide show
  1. package/LICENSE +203 -203
  2. package/README.md +68 -68
  3. package/dist/BaseClient.js +259 -0
  4. package/dist/BaseConfig.js +194 -0
  5. package/dist/api/ApiClient.js +508 -0
  6. package/dist/api/ApiErrors.js +118 -0
  7. package/dist/api/DscClient.js +332 -0
  8. package/dist/api/LecClient.js +48 -0
  9. package/dist/api/MgmtClient.js +77 -0
  10. package/dist/api/RegistryClient.js +234 -0
  11. package/dist/api/RulesClient.js +105 -0
  12. package/dist/api/StateClient.js +738 -0
  13. package/dist/application/ApplicationClient.js +436 -0
  14. package/dist/application/ApplicationConfig.js +233 -0
  15. package/dist/application/index.js +23 -0
  16. package/dist/bundled/wiotp-bundle.js +35592 -0
  17. package/dist/bundled/wiotp-bundle.min.js +47 -0
  18. package/dist/device/DeviceClient.js +125 -0
  19. package/dist/device/DeviceConfig.js +216 -0
  20. package/dist/device/index.js +23 -0
  21. package/dist/gateway/GatewayClient.js +159 -0
  22. package/dist/gateway/GatewayConfig.js +52 -0
  23. package/dist/gateway/index.js +23 -0
  24. package/dist/index.js +55 -0
  25. package/dist/util.js +50 -0
  26. package/package.json +92 -84
  27. package/src/BaseClient.js +215 -215
  28. package/src/BaseConfig.js +157 -157
  29. package/src/api/ApiClient.js +454 -454
  30. package/src/api/ApiErrors.js +33 -33
  31. package/src/api/DscClient.js +164 -145
  32. package/src/api/LecClient.js +32 -32
  33. package/src/api/MgmtClient.js +57 -57
  34. package/src/api/RegistryClient.js +194 -194
  35. package/src/api/RulesClient.js +84 -84
  36. package/src/api/StateClient.js +650 -650
  37. package/src/application/ApplicationClient.js +348 -348
  38. package/src/application/ApplicationConfig.js +191 -191
  39. package/src/application/index.js +12 -12
  40. package/src/device/DeviceClient.js +78 -78
  41. package/src/device/DeviceConfig.js +175 -175
  42. package/src/device/index.js +14 -14
  43. package/src/gateway/GatewayClient.js +114 -114
  44. package/src/gateway/GatewayConfig.js +21 -21
  45. package/src/gateway/index.js +13 -13
  46. package/{index.js → src/index.js} +19 -19
  47. package/src/util.js +38 -38
  48. package/src/util/IoTFoundation.pem +0 -82
@@ -0,0 +1,436 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _util = require("../util");
9
+
10
+ var _BaseClient2 = _interopRequireDefault(require("../BaseClient"));
11
+
12
+ var _ApiClient = _interopRequireDefault(require("../api/ApiClient"));
13
+
14
+ var _RegistryClient = _interopRequireDefault(require("../api/RegistryClient"));
15
+
16
+ var _MgmtClient = _interopRequireDefault(require("../api/MgmtClient"));
17
+
18
+ var _LecClient = _interopRequireDefault(require("../api/LecClient"));
19
+
20
+ var _DscClient = _interopRequireDefault(require("../api/DscClient"));
21
+
22
+ var _RulesClient = _interopRequireDefault(require("../api/RulesClient"));
23
+
24
+ var _StateClient = _interopRequireDefault(require("../api/StateClient"));
25
+
26
+ var _ApplicationConfig = _interopRequireDefault(require("./ApplicationConfig"));
27
+
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
29
+
30
+ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
31
+
32
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
33
+
34
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
35
+
36
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
37
+
38
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
39
+
40
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
41
+
42
+ function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
43
+
44
+ function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
45
+
46
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
47
+
48
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
49
+
50
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
51
+
52
+ var DEVICE_EVT_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/evt\/(.+)\/fmt\/(.+)$/;
53
+ var DEVICE_CMD_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/cmd\/(.+)\/fmt\/(.+)$/;
54
+ var DEVICE_STATE_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/intf\/(.+)\/evt\/state$/;
55
+ var DEVICE_STATE_ERROR_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/err\/data$/;
56
+ var RULE_TRIGGER_RE = /^iot-2\/intf\/(.+)\/rule\/(.+)\/evt\/trigger$/;
57
+ var RULE_ERROR_RE = /^iot-2\/intf\/(.+)\/rule\/(.+)\/err\/data$/;
58
+ var DEVICE_MON_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/mon$/;
59
+ var APP_MON_RE = /^iot-2\/app\/(.+)\/mon$/;
60
+
61
+ var ApplicationClient =
62
+ /*#__PURE__*/
63
+ function (_BaseClient) {
64
+ _inherits(ApplicationClient, _BaseClient);
65
+
66
+ function ApplicationClient(config) {
67
+ var _this;
68
+
69
+ _classCallCheck(this, ApplicationClient);
70
+
71
+ if (!config instanceof _ApplicationConfig["default"]) {
72
+ throw new Error("Config must be an instance of ApplicationConfig");
73
+ }
74
+
75
+ _this = _possibleConstructorReturn(this, _getPrototypeOf(ApplicationClient).call(this, config));
76
+ _this.useLtpa = config.auth && config.auth.useLtpa;
77
+
78
+ if (config.auth && config.auth.useLtpa || config.getOrgId() != "quickstart") {
79
+ _this._apiClient = new _ApiClient["default"](_this.config);
80
+ _this.dsc = new _DscClient["default"](_this._apiClient);
81
+ _this.lec = new _LecClient["default"](_this._apiClient);
82
+ _this.mgmt = new _MgmtClient["default"](_this._apiClient);
83
+ _this.registry = new _RegistryClient["default"](_this._apiClient);
84
+ _this.rules = new _RulesClient["default"](_this._apiClient);
85
+ _this.state = new _StateClient["default"](_this._apiClient);
86
+ }
87
+
88
+ _this.log.debug("[ApplicationClient:constructor] ApplicationClient initialized for organization : " + config.getOrgId());
89
+
90
+ return _this;
91
+ }
92
+
93
+ _createClass(ApplicationClient, [{
94
+ key: "connect",
95
+ value: function connect() {
96
+ var _this2 = this;
97
+
98
+ _get(_getPrototypeOf(ApplicationClient.prototype), "connect", this).call(this);
99
+
100
+ this.mqtt.on('message', function (topic, payload) {
101
+ _this2.log.trace("[ApplicationClient:onMessage] mqtt: ", topic, payload.toString()); // For each type of registered callback, check the incoming topic against a Regexp.
102
+ // If matches, forward the payload and various fields from the topic (extracted using groups in the regexp)
103
+
104
+
105
+ var match = DEVICE_EVT_RE.exec(topic);
106
+
107
+ if (match) {
108
+ _this2.emit('deviceEvent', match[1], match[2], match[3], match[4], payload, topic);
109
+
110
+ return;
111
+ }
112
+
113
+ var match = DEVICE_CMD_RE.exec(topic);
114
+
115
+ if (match) {
116
+ _this2.emit('deviceCommand', match[1], match[2], match[3], match[4], payload, topic);
117
+
118
+ return;
119
+ }
120
+
121
+ var match = DEVICE_STATE_RE.exec(topic);
122
+
123
+ if (match) {
124
+ _this2.emit('deviceState', match[1], match[2], match[3], payload, topic);
125
+
126
+ return;
127
+ }
128
+
129
+ var match = DEVICE_STATE_ERROR_RE.exec(topic);
130
+
131
+ if (match) {
132
+ _this2.emit('deviceStateError', match[1], match[2], payload, topic);
133
+
134
+ return;
135
+ }
136
+
137
+ var match = RULE_TRIGGER_RE.exec(topic);
138
+
139
+ if (match) {
140
+ _this2.emit('ruleTrigger', match[1], match[2], payload, topic);
141
+
142
+ return;
143
+ }
144
+
145
+ var match = RULE_ERROR_RE.exec(topic);
146
+
147
+ if (match) {
148
+ _this2.emit('ruleError', match[1], match[2], payload, topic);
149
+
150
+ return;
151
+ }
152
+
153
+ var match = DEVICE_MON_RE.exec(topic);
154
+
155
+ if (match) {
156
+ _this2.emit('deviceStatus', match[1], match[2], payload, topic);
157
+
158
+ return;
159
+ }
160
+
161
+ var match = APP_MON_RE.exec(topic);
162
+
163
+ if (match) {
164
+ _this2.emit('appStatus', match[1], payload, topic);
165
+
166
+ return;
167
+ } // catch all which logs the receipt of an unexpected message
168
+
169
+
170
+ _this2.log.warn("[ApplicationClient:onMessage] Message received on unexpected topic" + ", " + topic + ", " + payload);
171
+ });
172
+ } // ==========================================================================
173
+ // Device Events
174
+ // ==========================================================================
175
+
176
+ }, {
177
+ key: "publishEvent",
178
+ value: function publishEvent(typeId, deviceId, eventId, format, data, qos, callback) {
179
+ qos = qos || 0;
180
+
181
+ if (!(0, _util.isDefined)(typeId) || !(0, _util.isDefined)(deviceId) || !(0, _util.isDefined)(eventId) || !(0, _util.isDefined)(format)) {
182
+ this.log.error("[ApplicationClient:publishDeviceEvent] Required params for publishDeviceEvent not present");
183
+ this.emit('error', "[ApplicationClient:publishDeviceEvent] Required params for publishDeviceEvent not present");
184
+ return;
185
+ }
186
+
187
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
188
+
189
+ this._publish(topic, data, qos, callback);
190
+
191
+ return this;
192
+ }
193
+ }, {
194
+ key: "subscribeToEvents",
195
+ value: function subscribeToEvents(typeId, deviceId, eventId, format, qos, callback) {
196
+ typeId = typeId || '+';
197
+ deviceId = deviceId || '+';
198
+ eventId = eventId || '+';
199
+ format = format || '+';
200
+ qos = qos || 0;
201
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
202
+
203
+ this._subscribe(topic, qos, callback);
204
+
205
+ return this;
206
+ }
207
+ }, {
208
+ key: "unsubscribeFromEvents",
209
+ value: function unsubscribeFromEvents(typeId, deviceId, eventId, format) {
210
+ typeId = typeId || '+';
211
+ deviceId = deviceId || '+';
212
+ eventId = eventId || '+';
213
+ format = format || '+';
214
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
215
+
216
+ this._unsubscribe(topic, callback);
217
+
218
+ return this;
219
+ } // ==========================================================================
220
+ // Device Commands
221
+ // ==========================================================================
222
+
223
+ }, {
224
+ key: "publishCommand",
225
+ value: function publishCommand(typeId, deviceId, commandId, format, data, qos, callback) {
226
+ qos = qos || 0;
227
+
228
+ if (!(0, _util.isDefined)(typeId) || !(0, _util.isDefined)(deviceId) || !(0, _util.isDefined)(commandId) || !(0, _util.isDefined)(format)) {
229
+ this.log.error("[ApplicationClient:publishDeviceCommand] Required params for publishDeviceCommand not present");
230
+ this.emit('error', "[ApplicationClient:publishDeviceCommand] Required params for publishDeviceCommand not present");
231
+ return;
232
+ }
233
+
234
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
235
+
236
+ this._publish(topic, data, qos, callback);
237
+
238
+ return this;
239
+ }
240
+ }, {
241
+ key: "subscribeToCommands",
242
+ value: function subscribeToCommands(typeId, deviceId, commandId, format, qos, callback) {
243
+ typeId = typeId || '+';
244
+ deviceId = deviceId || '+';
245
+ commandId = commandId || '+';
246
+ format = format || '+';
247
+ qos = qos || 0;
248
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
249
+ this.log.debug("[ApplicationClient:subscribeToDeviceCommands] Calling subscribe with QoS " + qos);
250
+
251
+ this._subscribe(topic, qos, callback);
252
+
253
+ return this;
254
+ }
255
+ }, {
256
+ key: "unsubscribeFromCommands",
257
+ value: function unsubscribeFromCommands(typeId, deviceId, commandId, format, callback) {
258
+ typeId = typeId || '+';
259
+ deviceId = deviceId || '+';
260
+ commandId = commandId || '+';
261
+ format = format || '+';
262
+ var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
263
+
264
+ this._unsubscribe(topic, callback);
265
+
266
+ return this;
267
+ } // ==========================================================================
268
+ // Device State Events
269
+ // ==========================================================================
270
+
271
+ }, {
272
+ key: "subscribeToDeviceState",
273
+ value: function subscribeToDeviceState(type, id, interfaceId, qos) {
274
+ type = type || '+';
275
+ id = id || '+';
276
+ interfaceId = interfaceId || '+';
277
+ qos = qos || 0;
278
+ var topic = "iot-2/type/" + type + "/id/" + id + "/intf/" + interfaceId + "/evt/state";
279
+ this.log.debug("[ApplicationClient:subscribeToDeviceState] Calling subscribe with QoS " + qos);
280
+
281
+ this._subscribe(topic, qos);
282
+
283
+ return this;
284
+ }
285
+ }, {
286
+ key: "unsubscribeToDeviceState",
287
+ value: function unsubscribeToDeviceState(type, id, interfaceId) {
288
+ type = type || '+';
289
+ id = id || '+';
290
+ interfaceId = interfaceId || '+';
291
+ var topic = "iot-2/type/" + type + "/id/" + id + "/intf/" + interfaceId + "/evt/state";
292
+
293
+ this._unsubscribe(topic);
294
+
295
+ return this;
296
+ } // ==========================================================================
297
+ // Device State Errors
298
+ // ==========================================================================
299
+
300
+ }, {
301
+ key: "subscribeToDeviceErrors",
302
+ value: function subscribeToDeviceErrors(type, id, qos) {
303
+ type = type || '+';
304
+ id = id || '+';
305
+ qos = qos || 0;
306
+ var topic = "iot-2/type/" + type + "/id/" + id + "/err/data";
307
+ this.log.debug("[ApplicationClient:subscribeToDeviceErrors] Calling subscribe with QoS " + qos);
308
+
309
+ this._subscribe(topic, qos);
310
+
311
+ return this;
312
+ }
313
+ }, {
314
+ key: "unsubscribeToDeviceErrors",
315
+ value: function unsubscribeToDeviceErrors(type, id) {
316
+ type = type || '+';
317
+ id = id || '+';
318
+ var topic = "iot-2/type/" + type + "/id/" + id + "/err/data";
319
+
320
+ this._unsubscribe(topic);
321
+
322
+ return this;
323
+ } // ==========================================================================
324
+ // Rule Triggers
325
+ // ==========================================================================
326
+
327
+ }, {
328
+ key: "subscribeToRuleTriggers",
329
+ value: function subscribeToRuleTriggers(interfaceId, ruleId, qos) {
330
+ interfaceId = interfaceId || '+';
331
+ ruleId = ruleId || '+';
332
+ qos = qos || 0;
333
+ var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/evt/trigger";
334
+ this.log.debug("[ApplicationClient:subscribeToRuleTriggers] Calling subscribe with QoS " + qos);
335
+
336
+ this._subscribe(topic, qos);
337
+
338
+ return this;
339
+ }
340
+ }, {
341
+ key: "unsubscribeToRuleTriggers",
342
+ value: function unsubscribeToRuleTriggers(interfaceId, ruleId) {
343
+ interfaceId = interfaceId || '+';
344
+ ruleId = ruleId || '+';
345
+ var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/evt/trigger";
346
+
347
+ this._unsubscribe(topic);
348
+
349
+ return this;
350
+ } // ==========================================================================
351
+ // Rule Trigger Errors
352
+ // ==========================================================================
353
+
354
+ }, {
355
+ key: "subscribeToRuleErrors",
356
+ value: function subscribeToRuleErrors(interfaceId, ruleId, qos) {
357
+ interfaceId = interfaceId || '+';
358
+ ruleId = ruleId || '+';
359
+ qos = qos || 0;
360
+ var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/err/data";
361
+ this.log.debug("[ApplicationClient:subscribeToRuleErrors] Calling subscribe with QoS " + qos);
362
+
363
+ this._subscribe(topic, qos);
364
+
365
+ return this;
366
+ }
367
+ }, {
368
+ key: "unsubscribeToRuleErrors",
369
+ value: function unsubscribeToRuleErrors(interfaceId, ruleId) {
370
+ interfaceId = interfaceId || '+';
371
+ ruleId = ruleId || '+';
372
+ var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/err/data";
373
+
374
+ this._unsubscribe(topic);
375
+
376
+ return this;
377
+ } // ==========================================================================
378
+ // Device Status
379
+ // ==========================================================================
380
+
381
+ }, {
382
+ key: "subscribeToDeviceStatus",
383
+ value: function subscribeToDeviceStatus(type, id, qos) {
384
+ type = type || '+';
385
+ id = id || '+';
386
+ qos = qos || 0;
387
+ var topic = "iot-2/type/" + type + "/id/" + id + "/mon";
388
+ this.log.debug("[ApplicationClient:subscribeToDeviceStatus] Calling subscribe with QoS " + qos);
389
+
390
+ this._subscribe(topic, qos);
391
+
392
+ return this;
393
+ }
394
+ }, {
395
+ key: "unsubscribeToDeviceStatus",
396
+ value: function unsubscribeToDeviceStatus(type, id) {
397
+ type = type || '+';
398
+ id = id || '+';
399
+ var topic = "iot-2/type/" + type + "/id/" + id + "/mon";
400
+
401
+ this._unsubscribe(topic);
402
+
403
+ return this;
404
+ } // ==========================================================================
405
+ // Application Status
406
+ // ==========================================================================
407
+
408
+ }, {
409
+ key: "subscribeToAppStatus",
410
+ value: function subscribeToAppStatus(id, qos) {
411
+ id = id || '+';
412
+ qos = qos || 0;
413
+ var topic = "iot-2/app/" + id + "/mon";
414
+ this.log.debug("[ApplicationClient:subscribeToAppStatus] Calling subscribe with QoS " + qos);
415
+
416
+ this._subscribe(topic, qos);
417
+
418
+ return this;
419
+ }
420
+ }, {
421
+ key: "unsubscribeToAppStatus",
422
+ value: function unsubscribeToAppStatus(id) {
423
+ id = id || '+';
424
+ var topic = "iot-2/app/" + id + "/mon";
425
+
426
+ this._unsubscribe(topic);
427
+
428
+ return this;
429
+ }
430
+ }]);
431
+
432
+ return ApplicationClient;
433
+ }(_BaseClient2["default"]);
434
+
435
+ exports["default"] = ApplicationClient;
436
+ ;
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _BaseConfig2 = _interopRequireDefault(require("../BaseConfig"));
9
+
10
+ var _loglevel = _interopRequireDefault(require("loglevel"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
+
14
+ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
15
+
16
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
+
18
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
19
+
20
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
21
+
22
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
23
+
24
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
25
+
26
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
27
+
28
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
+
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
+
32
+ var YAML = require('yaml');
33
+
34
+ var fs = require('fs');
35
+
36
+ var uuidv4 = require('uuid/v4');
37
+
38
+ var ApplicationConfig =
39
+ /*#__PURE__*/
40
+ function (_BaseConfig) {
41
+ _inherits(ApplicationConfig, _BaseConfig);
42
+
43
+ function ApplicationConfig(identity, auth, options) {
44
+ var _this;
45
+
46
+ _classCallCheck(this, ApplicationConfig);
47
+
48
+ _this = _possibleConstructorReturn(this, _getPrototypeOf(ApplicationConfig).call(this, identity, auth, options)); // Authentication is not supported for quickstart
49
+ // and not required when auth.useLtpa is set
50
+
51
+ if (_this.auth != null && !_this.auth.useLtpa) {
52
+ if (!("key" in _this.auth) || _this.auth.key == null) {
53
+ throw new Error("Missing auth.key from configuration");
54
+ }
55
+
56
+ if (!("token" in _this.auth) || _this.auth.token == null) {
57
+ throw new Error("Missing auth.token from configuration");
58
+ }
59
+ } // Set defaults for optional configuration
60
+
61
+
62
+ if (_this.identity == null) {
63
+ _this.identity = {};
64
+ }
65
+
66
+ if (!("appId" in _this.identity)) {
67
+ _this.identity.appId = uuidv4();
68
+ }
69
+
70
+ if (!("sharedSubscription" in _this.options.mqtt)) {
71
+ _this.options.mqtt.sharedSubscription = false;
72
+ }
73
+
74
+ return _this;
75
+ }
76
+
77
+ _createClass(ApplicationConfig, [{
78
+ key: "getOrgId",
79
+ value: function getOrgId() {
80
+ if (this.auth == null) {
81
+ return "quickstart";
82
+ } else if (this.auth.key) {
83
+ return this.auth.key.split("-")[1];
84
+ } else {
85
+ return null;
86
+ }
87
+ }
88
+ }, {
89
+ key: "getApiBaseUri",
90
+ value: function getApiBaseUri() {
91
+ return this.auth && this.auth.useLtpa ? "/api/v0002" : "https://".concat(this.getOrgId(), ".").concat(this.options.domain, "/api/v0002");
92
+ }
93
+ }, {
94
+ key: "getClientId",
95
+ value: function getClientId() {
96
+ var clientIdPrefix = "a";
97
+
98
+ if (this.sharedSubscription == true) {
99
+ clientIdPrefix = "A";
100
+ }
101
+
102
+ return clientIdPrefix + ":" + this.getOrgId() + ":" + this.identity.appId;
103
+ }
104
+ }, {
105
+ key: "getMqttUsername",
106
+ value: function getMqttUsername() {
107
+ return this.auth.key;
108
+ }
109
+ }, {
110
+ key: "getMqttPassword",
111
+ value: function getMqttPassword() {
112
+ return this.auth.token;
113
+ }
114
+ }], [{
115
+ key: "parseEnvVars",
116
+ value: function parseEnvVars() {
117
+ // Auth
118
+ var authKey = process.env.WIOTP_AUTH_KEY || null;
119
+ var authToken = process.env.WIOTP_AUTH_TOKEN || null; // Also support WIOTP_API_KEY / WIOTP_API_TOKEN usage
120
+
121
+ if (authKey == null && authToken == null) {
122
+ authKey = process.env.WIOTP_API_KEY || null;
123
+ authToken = process.env.WIOTP_API_TOKEN || null;
124
+ } // Identity
125
+
126
+
127
+ var appId = process.env.WIOTP_IDENTITY_APPID || uuidv4(); // Options
128
+
129
+ var domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
130
+ var logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
131
+ var port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
132
+ var transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
133
+ var caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
134
+ var cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
135
+ var sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
136
+ var keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
137
+ var sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
138
+ var verifyCert = process.env.WIOTP_OPTIONS_HTTP_VERIFY || "true"; // String to int conversions
139
+
140
+ if (port != null) {
141
+ port = parseInt(port);
142
+ }
143
+
144
+ sessionExpiry = parseInt(sessionExpiry);
145
+ keepAlive = parseInt(keepAlive);
146
+ var identity = {
147
+ appId: appId
148
+ };
149
+ var options = {
150
+ domain: domain,
151
+ logLevel: logLevel,
152
+ mqtt: {
153
+ port: port,
154
+ transport: transport,
155
+ cleanStart: ["True", "true", "1"].includes(cleanStart),
156
+ sessionExpiry: sessionExpiry,
157
+ keepAlive: keepAlive,
158
+ sharedSubscription: ["True", "true", "1"].includes(sharedSubs),
159
+ caFile: caFile
160
+ },
161
+ http: {
162
+ verify: ["True", "true", "1"].includes(verifyCert)
163
+ }
164
+ };
165
+ var auth = null; // Quickstart doesn't support auth, so ensure we only add this if it's defined
166
+
167
+ if (authToken != null) {
168
+ auth = {
169
+ key: authKey,
170
+ token: authToken
171
+ };
172
+ }
173
+
174
+ return new ApplicationConfig(identity, auth, options);
175
+ }
176
+ }, {
177
+ key: "parseConfigFile",
178
+ value: function parseConfigFile(configFilePath) {
179
+ //Example Application Configuration File:
180
+
181
+ /*
182
+ identity:
183
+ appId: myApp
184
+ auth:
185
+ key: a-23gh56-sdsdajhjnee
186
+ token: Ab$76s)asj8_s5
187
+ options:
188
+ domain: internetofthings.ibmcloud.com
189
+ logLevel: error|warning|info|debug
190
+ mqtt:
191
+ instanceId: myInstance
192
+ port: 8883
193
+ transport: tcp
194
+ cleanStart: false
195
+ sessionExpiry: 3600
196
+ keepAlive: 60
197
+ caFile: /path/to/certificateAuthorityFile.pem
198
+ http:
199
+ verify: true
200
+ */
201
+ var configFile = fs.readFileSync(configFilePath, 'utf8');
202
+ var data = YAML.parse(configFile);
203
+
204
+ if (!fs.existsSync(configFilePath)) {
205
+ throw new Error("File not found");
206
+ } else {
207
+ try {
208
+ var _configFile = fs.readFileSync(configFilePath, 'utf8');
209
+
210
+ var data = YAML.parse(_configFile);
211
+ } catch (err) {
212
+ throw new Error("Error reading device configuration file: " + err.code);
213
+ }
214
+ }
215
+
216
+ if ("options" in data & "logLevel" in data["options"]) {
217
+ var validLevels = ["error", "warning", "info", "debug"];
218
+
219
+ if (!validLevels.includes(data["options"]["logLevel"])) {
220
+ throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug");
221
+ }
222
+ } else {
223
+ data["options"]["logLevel"] = _loglevel["default"].GetLogger(data["options"]["logLevel"].toUpperCase());
224
+ }
225
+
226
+ return new ApplicationConfig(data['identity'], data['auth'], data['options']);
227
+ }
228
+ }]);
229
+
230
+ return ApplicationConfig;
231
+ }(_BaseConfig2["default"]);
232
+
233
+ exports["default"] = ApplicationConfig;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "ApplicationClient", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _ApplicationClient["default"];
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "ApplicationConfig", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _ApplicationConfig["default"];
16
+ }
17
+ });
18
+
19
+ var _ApplicationClient = _interopRequireDefault(require("./ApplicationClient"));
20
+
21
+ var _ApplicationConfig = _interopRequireDefault(require("./ApplicationConfig"));
22
+
23
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }