@wiotp/sdk 0.8.3 → 0.8.4

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.
@@ -1,356 +0,0 @@
1
- "use strict";
2
-
3
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = void 0;
8
- var _util = require("../util");
9
- var _BaseClient2 = _interopRequireDefault(require("../BaseClient"));
10
- var _ApiClient = _interopRequireDefault(require("../api/ApiClient"));
11
- var _RegistryClient = _interopRequireDefault(require("../api/RegistryClient"));
12
- var _MgmtClient = _interopRequireDefault(require("../api/MgmtClient"));
13
- var _LecClient = _interopRequireDefault(require("../api/LecClient"));
14
- var _DscClient = _interopRequireDefault(require("../api/DscClient"));
15
- var _RulesClient = _interopRequireDefault(require("../api/RulesClient"));
16
- var _StateClient = _interopRequireDefault(require("../api/StateClient"));
17
- var _ApplicationConfig = _interopRequireDefault(require("./ApplicationConfig"));
18
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
19
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
20
- function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
21
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
22
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
23
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
24
- function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
25
- function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
26
- function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
27
- function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
28
- function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
29
- function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
30
- function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
31
- function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
32
- function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
33
- function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } /**
34
- *****************************************************************************
35
- Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
36
- All rights reserved. This program and the accompanying materials
37
- are made available under the terms of the Eclipse Public License v1.0
38
- which accompanies this distribution, and is available at
39
- http://www.eclipse.org/legal/epl-v10.html
40
- *****************************************************************************
41
- *
42
- */
43
- var DEVICE_EVT_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/evt\/(.+)\/fmt\/(.+)$/;
44
- var DEVICE_CMD_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/cmd\/(.+)\/fmt\/(.+)$/;
45
- var DEVICE_STATE_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/intf\/(.+)\/evt\/state$/;
46
- var DEVICE_STATE_ERROR_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/err\/data$/;
47
- var RULE_TRIGGER_RE = /^iot-2\/intf\/(.+)\/rule\/(.+)\/evt\/trigger$/;
48
- var RULE_ERROR_RE = /^iot-2\/intf\/(.+)\/rule\/(.+)\/err\/data$/;
49
- var DEVICE_MON_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/mon$/;
50
- var APP_MON_RE = /^iot-2\/app\/(.+)\/mon$/;
51
- var ApplicationClient = exports["default"] = /*#__PURE__*/function (_BaseClient) {
52
- function ApplicationClient(config) {
53
- var _this;
54
- _classCallCheck(this, ApplicationClient);
55
- if (!config instanceof _ApplicationConfig["default"]) {
56
- throw new Error("Config must be an instance of ApplicationConfig");
57
- }
58
- _this = _callSuper(this, ApplicationClient, [config]);
59
- _this.useLtpa = config.auth && config.auth.useLtpa;
60
- if (config.auth && config.auth.useLtpa || config.getOrgId() != "quickstart") {
61
- _this._apiClient = new _ApiClient["default"](_this.config);
62
- _this.dsc = new _DscClient["default"](_this._apiClient);
63
- _this.lec = new _LecClient["default"](_this._apiClient);
64
- _this.mgmt = new _MgmtClient["default"](_this._apiClient);
65
- _this.registry = new _RegistryClient["default"](_this._apiClient);
66
- _this.rules = new _RulesClient["default"](_this._apiClient);
67
- _this.state = new _StateClient["default"](_this._apiClient);
68
- }
69
- _this.log.debug("[ApplicationClient:constructor] ApplicationClient initialized for organization : " + config.getOrgId());
70
- return _this;
71
- }
72
- _inherits(ApplicationClient, _BaseClient);
73
- return _createClass(ApplicationClient, [{
74
- key: "connect",
75
- value: function connect() {
76
- var _this2 = this;
77
- _superPropGet(ApplicationClient, "connect", this, 3)([]);
78
- this.mqtt.on('message', function (topic, payload) {
79
- _this2.log.trace("[ApplicationClient:onMessage] mqtt: ", topic, payload.toString());
80
-
81
- // For each type of registered callback, check the incoming topic against a Regexp.
82
- // If matches, forward the payload and various fields from the topic (extracted using groups in the regexp)
83
-
84
- var match = DEVICE_EVT_RE.exec(topic);
85
- if (match) {
86
- _this2.emit('deviceEvent', match[1], match[2], match[3], match[4], payload, topic);
87
- return;
88
- }
89
- var match = DEVICE_CMD_RE.exec(topic);
90
- if (match) {
91
- _this2.emit('deviceCommand', match[1], match[2], match[3], match[4], payload, topic);
92
- return;
93
- }
94
- var match = DEVICE_STATE_RE.exec(topic);
95
- if (match) {
96
- _this2.emit('deviceState', match[1], match[2], match[3], payload, topic);
97
- return;
98
- }
99
- var match = DEVICE_STATE_ERROR_RE.exec(topic);
100
- if (match) {
101
- _this2.emit('deviceStateError', match[1], match[2], payload, topic);
102
- return;
103
- }
104
- var match = RULE_TRIGGER_RE.exec(topic);
105
- if (match) {
106
- _this2.emit('ruleTrigger', match[1], match[2], payload, topic);
107
- return;
108
- }
109
- var match = RULE_ERROR_RE.exec(topic);
110
- if (match) {
111
- _this2.emit('ruleError', match[1], match[2], payload, topic);
112
- return;
113
- }
114
- var match = DEVICE_MON_RE.exec(topic);
115
- if (match) {
116
- _this2.emit('deviceStatus', match[1], match[2], payload, topic);
117
- return;
118
- }
119
- var match = APP_MON_RE.exec(topic);
120
- if (match) {
121
- _this2.emit('appStatus', match[1], payload, topic);
122
- return;
123
- }
124
-
125
- // catch all which logs the receipt of an unexpected message
126
- _this2.log.warn("[ApplicationClient:onMessage] Message received on unexpected topic" + ", " + topic + ", " + payload);
127
- });
128
- }
129
-
130
- // ==========================================================================
131
- // Device Events
132
- // ==========================================================================
133
- }, {
134
- key: "publishEvent",
135
- value: function publishEvent(typeId, deviceId, eventId, format, data, qos, callback) {
136
- qos = qos || 0;
137
- if (!(0, _util.isDefined)(typeId) || !(0, _util.isDefined)(deviceId) || !(0, _util.isDefined)(eventId) || !(0, _util.isDefined)(format)) {
138
- this.log.error("[ApplicationClient:publishDeviceEvent] Required params for publishDeviceEvent not present");
139
- this.emit('error', "[ApplicationClient:publishDeviceEvent] Required params for publishDeviceEvent not present");
140
- return;
141
- }
142
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
143
- this._publish(topic, data, qos, callback);
144
- return this;
145
- }
146
- }, {
147
- key: "subscribeToEvents",
148
- value: function subscribeToEvents(typeId, deviceId, eventId, format, qos, callback) {
149
- typeId = typeId || '+';
150
- deviceId = deviceId || '+';
151
- eventId = eventId || '+';
152
- format = format || '+';
153
- qos = qos || 0;
154
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
155
- this._subscribe(topic, qos, callback);
156
- return this;
157
- }
158
- }, {
159
- key: "unsubscribeFromEvents",
160
- value: function unsubscribeFromEvents(typeId, deviceId, eventId, format, callback) {
161
- typeId = typeId || '+';
162
- deviceId = deviceId || '+';
163
- eventId = eventId || '+';
164
- format = format || '+';
165
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/evt/" + eventId + "/fmt/" + format;
166
- this._unsubscribe(topic, callback);
167
- return this;
168
- }
169
-
170
- // ==========================================================================
171
- // Device Commands
172
- // ==========================================================================
173
- }, {
174
- key: "publishCommand",
175
- value: function publishCommand(typeId, deviceId, commandId, format, data, qos, callback) {
176
- qos = qos || 0;
177
- if (!(0, _util.isDefined)(typeId) || !(0, _util.isDefined)(deviceId) || !(0, _util.isDefined)(commandId) || !(0, _util.isDefined)(format)) {
178
- this.log.error("[ApplicationClient:publishDeviceCommand] Required params for publishDeviceCommand not present");
179
- this.emit('error', "[ApplicationClient:publishDeviceCommand] Required params for publishDeviceCommand not present");
180
- return;
181
- }
182
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
183
- this._publish(topic, data, qos, callback);
184
- return this;
185
- }
186
- }, {
187
- key: "subscribeToCommands",
188
- value: function subscribeToCommands(typeId, deviceId, commandId, format, qos, callback) {
189
- typeId = typeId || '+';
190
- deviceId = deviceId || '+';
191
- commandId = commandId || '+';
192
- format = format || '+';
193
- qos = qos || 0;
194
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
195
- this.log.debug("[ApplicationClient:subscribeToDeviceCommands] Calling subscribe with QoS " + qos);
196
- this._subscribe(topic, qos, callback);
197
- return this;
198
- }
199
- }, {
200
- key: "unsubscribeFromCommands",
201
- value: function unsubscribeFromCommands(typeId, deviceId, commandId, format, callback) {
202
- typeId = typeId || '+';
203
- deviceId = deviceId || '+';
204
- commandId = commandId || '+';
205
- format = format || '+';
206
- var topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/" + commandId + "/fmt/" + format;
207
- this._unsubscribe(topic, callback);
208
- return this;
209
- }
210
-
211
- // ==========================================================================
212
- // Device State Events
213
- // ==========================================================================
214
- }, {
215
- key: "subscribeToDeviceState",
216
- value: function subscribeToDeviceState(type, id, interfaceId, qos) {
217
- type = type || '+';
218
- id = id || '+';
219
- interfaceId = interfaceId || '+';
220
- qos = qos || 0;
221
- var topic = "iot-2/type/" + type + "/id/" + id + "/intf/" + interfaceId + "/evt/state";
222
- this.log.debug("[ApplicationClient:subscribeToDeviceState] Calling subscribe with QoS " + qos);
223
- this._subscribe(topic, qos);
224
- return this;
225
- }
226
- }, {
227
- key: "unsubscribeToDeviceState",
228
- value: function unsubscribeToDeviceState(type, id, interfaceId) {
229
- type = type || '+';
230
- id = id || '+';
231
- interfaceId = interfaceId || '+';
232
- var topic = "iot-2/type/" + type + "/id/" + id + "/intf/" + interfaceId + "/evt/state";
233
- this._unsubscribe(topic);
234
- return this;
235
- }
236
-
237
- // ==========================================================================
238
- // Device State Errors
239
- // ==========================================================================
240
- }, {
241
- key: "subscribeToDeviceErrors",
242
- value: function subscribeToDeviceErrors(type, id, qos) {
243
- type = type || '+';
244
- id = id || '+';
245
- qos = qos || 0;
246
- var topic = "iot-2/type/" + type + "/id/" + id + "/err/data";
247
- this.log.debug("[ApplicationClient:subscribeToDeviceErrors] Calling subscribe with QoS " + qos);
248
- this._subscribe(topic, qos);
249
- return this;
250
- }
251
- }, {
252
- key: "unsubscribeToDeviceErrors",
253
- value: function unsubscribeToDeviceErrors(type, id) {
254
- type = type || '+';
255
- id = id || '+';
256
- var topic = "iot-2/type/" + type + "/id/" + id + "/err/data";
257
- this._unsubscribe(topic);
258
- return this;
259
- }
260
-
261
- // ==========================================================================
262
- // Rule Triggers
263
- // ==========================================================================
264
- }, {
265
- key: "subscribeToRuleTriggers",
266
- value: function subscribeToRuleTriggers(interfaceId, ruleId, qos) {
267
- interfaceId = interfaceId || '+';
268
- ruleId = ruleId || '+';
269
- qos = qos || 0;
270
- var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/evt/trigger";
271
- this.log.debug("[ApplicationClient:subscribeToRuleTriggers] Calling subscribe with QoS " + qos);
272
- this._subscribe(topic, qos);
273
- return this;
274
- }
275
- }, {
276
- key: "unsubscribeToRuleTriggers",
277
- value: function unsubscribeToRuleTriggers(interfaceId, ruleId) {
278
- interfaceId = interfaceId || '+';
279
- ruleId = ruleId || '+';
280
- var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/evt/trigger";
281
- this._unsubscribe(topic);
282
- return this;
283
- }
284
-
285
- // ==========================================================================
286
- // Rule Trigger Errors
287
- // ==========================================================================
288
- }, {
289
- key: "subscribeToRuleErrors",
290
- value: function subscribeToRuleErrors(interfaceId, ruleId, qos) {
291
- interfaceId = interfaceId || '+';
292
- ruleId = ruleId || '+';
293
- qos = qos || 0;
294
- var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/err/data";
295
- this.log.debug("[ApplicationClient:subscribeToRuleErrors] Calling subscribe with QoS " + qos);
296
- this._subscribe(topic, qos);
297
- return this;
298
- }
299
- }, {
300
- key: "unsubscribeToRuleErrors",
301
- value: function unsubscribeToRuleErrors(interfaceId, ruleId) {
302
- interfaceId = interfaceId || '+';
303
- ruleId = ruleId || '+';
304
- var topic = "iot-2/intf/" + interfaceId + "/rule/" + ruleId + "/err/data";
305
- this._unsubscribe(topic);
306
- return this;
307
- }
308
-
309
- // ==========================================================================
310
- // Device Status
311
- // ==========================================================================
312
- }, {
313
- key: "subscribeToDeviceStatus",
314
- value: function subscribeToDeviceStatus(type, id, qos) {
315
- type = type || '+';
316
- id = id || '+';
317
- qos = qos || 0;
318
- var topic = "iot-2/type/" + type + "/id/" + id + "/mon";
319
- this.log.debug("[ApplicationClient:subscribeToDeviceStatus] Calling subscribe with QoS " + qos);
320
- this._subscribe(topic, qos);
321
- return this;
322
- }
323
- }, {
324
- key: "unsubscribeToDeviceStatus",
325
- value: function unsubscribeToDeviceStatus(type, id) {
326
- type = type || '+';
327
- id = id || '+';
328
- var topic = "iot-2/type/" + type + "/id/" + id + "/mon";
329
- this._unsubscribe(topic);
330
- return this;
331
- }
332
-
333
- // ==========================================================================
334
- // Application Status
335
- // ==========================================================================
336
- }, {
337
- key: "subscribeToAppStatus",
338
- value: function subscribeToAppStatus(id, qos) {
339
- id = id || '+';
340
- qos = qos || 0;
341
- var topic = "iot-2/app/" + id + "/mon";
342
- this.log.debug("[ApplicationClient:subscribeToAppStatus] Calling subscribe with QoS " + qos);
343
- this._subscribe(topic, qos);
344
- return this;
345
- }
346
- }, {
347
- key: "unsubscribeToAppStatus",
348
- value: function unsubscribeToAppStatus(id) {
349
- id = id || '+';
350
- var topic = "iot-2/app/" + id + "/mon";
351
- this._unsubscribe(topic);
352
- return this;
353
- }
354
- }]);
355
- }(_BaseClient2["default"]);
356
- ;
@@ -1,240 +0,0 @@
1
- "use strict";
2
-
3
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = void 0;
8
- var _BaseConfig2 = _interopRequireDefault(require("../BaseConfig"));
9
- var _loglevel = _interopRequireDefault(require("loglevel"));
10
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
12
- function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
13
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
14
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
15
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
16
- function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
17
- function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
18
- function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
19
- function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
20
- function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
21
- function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
22
- function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } /**
23
- *****************************************************************************
24
- Copyright (c) 2019 IBM Corporation and other Contributors.
25
- All rights reserved. This program and the accompanying materials
26
- are made available under the terms of the Eclipse Public License v1.0
27
- which accompanies this distribution, and is available at
28
- http://www.eclipse.org/legal/epl-v10.html
29
- *****************************************************************************
30
- *
31
- */
32
- var YAML = require('yaml');
33
- var fs = require('fs');
34
- var _require = require('uuid'),
35
- uuidv4 = _require.v4;
36
- var ApplicationConfig = exports["default"] = /*#__PURE__*/function (_BaseConfig) {
37
- function ApplicationConfig(identity, auth, options) {
38
- var _this;
39
- _classCallCheck(this, ApplicationConfig);
40
- _this = _callSuper(this, ApplicationConfig, [identity, auth, options]);
41
-
42
- // Authentication is not supported for quickstart
43
- // and not required when auth.useLtpa is set
44
- if (_this.auth != null && !_this.auth.useLtpa) {
45
- if (!("key" in _this.auth) || _this.auth.key == null) {
46
- throw new Error("Missing auth.key from configuration");
47
- }
48
- if (!("token" in _this.auth) || _this.auth.token == null) {
49
- throw new Error("Missing auth.token from configuration");
50
- }
51
- }
52
-
53
- // Set defaults for optional configuration
54
- if (_this.identity == null) {
55
- _this.identity = {};
56
- }
57
- if (!("appId" in _this.identity)) {
58
- _this.identity.appId = uuidv4();
59
- }
60
- if (!("sharedSubscription" in _this.options.mqtt)) {
61
- _this.options.mqtt.sharedSubscription = false;
62
- }
63
- return _this;
64
- }
65
- _inherits(ApplicationConfig, _BaseConfig);
66
- return _createClass(ApplicationConfig, [{
67
- key: "getOrgId",
68
- value: function getOrgId() {
69
- if (this.auth == null) {
70
- return "quickstart";
71
- } else if (this.auth.key) {
72
- return this.auth.key.split("-")[1];
73
- } else {
74
- return null;
75
- }
76
- }
77
-
78
- // Returns options.apiRoot if set, 'api/v0002' otherwiss
79
- }, {
80
- key: "getApiRoot",
81
- value: function getApiRoot() {
82
- return this.options.apiRoot || 'api/v0002';
83
- }
84
- }, {
85
- key: "getApiBaseUri",
86
- value: function getApiBaseUri() {
87
- return this.auth && this.auth.useLtpa ? "/".concat(this.getApiRoot()) : "https://".concat(this.getOrgId(), ".").concat(this.options.domain, "/").concat(this.getApiRoot());
88
- }
89
- }, {
90
- key: "getClientId",
91
- value: function getClientId() {
92
- var clientIdPrefix = "a";
93
- if (this.options.mqtt.sharedSubscription == true) {
94
- clientIdPrefix = "A";
95
- }
96
- return clientIdPrefix + ":" + this.getOrgId() + ":" + this.identity.appId;
97
- }
98
- }, {
99
- key: "getMqttUsername",
100
- value: function getMqttUsername() {
101
- return this.auth.key;
102
- }
103
- }, {
104
- key: "getMqttPassword",
105
- value: function getMqttPassword() {
106
- return this.auth.token;
107
- }
108
- }, {
109
- key: "getAdditionalHeaders",
110
- value: function getAdditionalHeaders() {
111
- return this.options && this.options.http ? this.options.http.additionalHeaders : [];
112
- }
113
- }, {
114
- key: "setAdditionalHeader",
115
- value: function setAdditionalHeader(key, value) {
116
- if (!this.options.http) this.options.http = {};
117
- if (!this.options.http.additionalHeaders) this.options.http.additionalHeaders = {};
118
- this.options.http.additionalHeaders[key] = value;
119
- }
120
- }], [{
121
- key: "parseEnvVars",
122
- value: function parseEnvVars() {
123
- // Auth
124
- var authKey = process.env.WIOTP_AUTH_KEY || null;
125
- var authToken = process.env.WIOTP_AUTH_TOKEN || null;
126
-
127
- // Also support WIOTP_API_KEY / WIOTP_API_TOKEN usage
128
- if (authKey == null && authToken == null) {
129
- authKey = process.env.WIOTP_API_KEY || null;
130
- authToken = process.env.WIOTP_API_TOKEN || null;
131
- }
132
-
133
- // Identity
134
- var appId = process.env.WIOTP_IDENTITY_APPID || uuidv4();
135
-
136
- // Options
137
- var domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
138
- var apiRoot = process.env.WIOTP_OPTIONS_API_ROOT || null;
139
- var logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
140
- var port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
141
- var transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
142
- var caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
143
- var cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
144
- var sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
145
- var keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
146
- var sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
147
- var verifyCert = process.env.WIOTP_OPTIONS_HTTP_VERIFY || "true";
148
-
149
- // TODO: add environment variable parsing for options.http.additionalHeaders
150
-
151
- // String to int conversions
152
- if (port != null) {
153
- port = parseInt(port);
154
- }
155
- sessionExpiry = parseInt(sessionExpiry);
156
- keepAlive = parseInt(keepAlive);
157
- var identity = {
158
- appId: appId
159
- };
160
- var options = {
161
- domain: domain,
162
- apiRoot: apiRoot,
163
- logLevel: logLevel,
164
- mqtt: {
165
- port: port,
166
- transport: transport,
167
- cleanStart: ["True", "true", "1"].includes(cleanStart),
168
- sessionExpiry: sessionExpiry,
169
- keepAlive: keepAlive,
170
- sharedSubscription: ["True", "true", "1"].includes(sharedSubs),
171
- caFile: caFile
172
- },
173
- http: {
174
- verify: ["True", "true", "1"].includes(verifyCert)
175
- }
176
- };
177
- var auth = null;
178
- // Quickstart doesn't support auth, so ensure we only add this if it's defined
179
- if (authToken != null) {
180
- auth = {
181
- key: authKey,
182
- token: authToken
183
- };
184
- }
185
- return new ApplicationConfig(identity, auth, options);
186
- }
187
- }, {
188
- key: "parseConfigFile",
189
- value: function parseConfigFile(configFilePath) {
190
- //Example Application Configuration File:
191
-
192
- /*
193
- identity:
194
- appId: myApp
195
- auth:
196
- key: a-23gh56-sdsdajhjnee
197
- token: Ab$76s)asj8_s5
198
- options:
199
- domain: internetofthings.ibmcloud.com
200
- apiRoot: 'api/platform'
201
- logLevel: error|warning|info|debug
202
- mqtt:
203
- instanceId: myInstance
204
- port: 8883
205
- transport: tcp
206
- cleanStart: false
207
- sessionExpiry: 3600
208
- keepAlive: 60
209
- caFile: /path/to/certificateAuthorityFile.pem
210
- http:
211
- verify: true
212
- additionalHeaders:
213
- X-Csrf-Token: sdfsdfsdf
214
- X-myheader: hello
215
- */
216
-
217
- var configFile = fs.readFileSync(configFilePath, 'utf8');
218
- var data = YAML.parse(configFile);
219
- if (!fs.existsSync(configFilePath)) {
220
- throw new Error("File not found");
221
- } else {
222
- try {
223
- var _configFile = fs.readFileSync(configFilePath, 'utf8');
224
- var data = YAML.parse(_configFile);
225
- } catch (err) {
226
- throw new Error("Error reading device configuration file: " + err.code);
227
- }
228
- }
229
- if ("options" in data & "logLevel" in data["options"]) {
230
- var validLevels = ["error", "warning", "info", "debug"];
231
- if (!validLevels.includes(data["options"]["logLevel"])) {
232
- throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug");
233
- }
234
- } else {
235
- data["options"]["logLevel"] = _loglevel["default"].GetLogger(data["options"]["logLevel"].toUpperCase());
236
- }
237
- return new ApplicationConfig(data['identity'], data['auth'], data['options']);
238
- }
239
- }]);
240
- }(_BaseConfig2["default"]);
@@ -1,20 +0,0 @@
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
- var _ApplicationClient = _interopRequireDefault(require("./ApplicationClient"));
19
- var _ApplicationConfig = _interopRequireDefault(require("./ApplicationConfig"));
20
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }