@wiotp/sdk 0.7.8 → 0.8.0
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/dist/BaseClient.js +36 -82
- package/dist/BaseConfig.js +24 -36
- package/dist/api/ApiClient.js +60 -76
- package/dist/api/ApiErrors.js +29 -74
- package/dist/api/DscClient.js +77 -77
- package/dist/api/LecClient.js +17 -15
- package/dist/api/MgmtClient.js +17 -15
- package/dist/api/RegistryClient.js +12 -18
- package/dist/api/RulesClient.js +17 -15
- package/dist/api/StateClient.js +70 -132
- package/dist/application/ApplicationClient.js +53 -137
- package/dist/application/ApplicationConfig.js +43 -64
- package/dist/application/index.js +0 -3
- package/dist/bundled/wiotp-bundle.js +45644 -34207
- package/dist/bundled/wiotp-bundle.min.js +7 -6
- package/dist/device/DeviceClient.js +25 -54
- package/dist/device/DeviceConfig.js +37 -59
- package/dist/device/index.js +0 -3
- package/dist/gateway/GatewayClient.js +27 -61
- package/dist/gateway/GatewayConfig.js +22 -33
- package/dist/gateway/index.js +0 -3
- package/dist/index.js +14 -28
- package/dist/util.js +9 -15
- package/package.json +24 -24
- package/src/BaseClient.js +1 -1
- package/src/application/ApplicationConfig.js +1 -1
package/dist/BaseClient.js
CHANGED
|
@@ -4,85 +4,69 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _events = _interopRequireDefault(require("events"));
|
|
9
|
-
|
|
10
8
|
var _mqtt = _interopRequireDefault(require("mqtt"));
|
|
11
|
-
|
|
12
9
|
var _loglevel = _interopRequireDefault(require("loglevel"));
|
|
13
|
-
|
|
14
10
|
var _tinycache = _interopRequireDefault(require("tinycache"));
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
17
|
-
|
|
18
|
-
function _typeof(obj) { "@babel/helpers - typeof"; 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); }
|
|
19
|
-
|
|
12
|
+
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); }
|
|
20
13
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
|
-
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
29
|
-
|
|
30
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
31
|
-
|
|
32
|
-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
33
|
-
|
|
14
|
+
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, _toPropertyKey(descriptor.key), descriptor); } }
|
|
15
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
16
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
17
|
+
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); }
|
|
18
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
19
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
34
20
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
35
|
-
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
21
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
22
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
23
|
+
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
24
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } /**
|
|
25
|
+
*****************************************************************************
|
|
26
|
+
Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
|
|
27
|
+
All rights reserved. This program and the accompanying materials
|
|
28
|
+
are made available under the terms of the Eclipse Public License v1.0
|
|
29
|
+
which accompanies this distribution, and is available at
|
|
30
|
+
http://www.eclipse.org/legal/epl-v10.html
|
|
31
|
+
*****************************************************************************
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
var _require = require('uuid'),
|
|
35
|
+
uuidv4 = _require.v4;
|
|
36
|
+
var BaseClient = exports["default"] = /*#__PURE__*/function (_events$EventEmitter) {
|
|
43
37
|
_inherits(BaseClient, _events$EventEmitter);
|
|
44
|
-
|
|
45
|
-
var _super = _createSuper(BaseClient);
|
|
46
|
-
|
|
47
38
|
function BaseClient(config) {
|
|
48
39
|
var _this;
|
|
49
|
-
|
|
50
40
|
_classCallCheck(this, BaseClient);
|
|
51
|
-
|
|
52
|
-
_this = _super.call(this);
|
|
41
|
+
_this = _callSuper(this, BaseClient);
|
|
53
42
|
_this.log = _loglevel["default"];
|
|
54
|
-
|
|
55
43
|
_this.log.setDefaultLevel(config.options.logLevel);
|
|
56
|
-
|
|
57
44
|
_this.config = config;
|
|
58
45
|
_this.reconnectLog = 0;
|
|
59
46
|
_this.mqtt = null;
|
|
60
47
|
_this.lostConnectionLog = new _tinycache["default"]();
|
|
61
48
|
return _this;
|
|
62
49
|
}
|
|
63
|
-
|
|
64
50
|
_createClass(BaseClient, [{
|
|
65
51
|
key: "isConnected",
|
|
66
52
|
value: function isConnected() {
|
|
67
53
|
if (this.mqtt == null) {
|
|
68
54
|
return false;
|
|
69
55
|
}
|
|
70
|
-
|
|
71
56
|
return this.mqtt.connected;
|
|
72
57
|
}
|
|
73
58
|
}, {
|
|
74
59
|
key: "connect",
|
|
75
60
|
value: function connect() {
|
|
76
61
|
var _this2 = this;
|
|
77
|
-
|
|
78
62
|
if (this.mqtt != null) {
|
|
79
63
|
this.log.info("[BaseClient:connect] Reconnecting to " + this.config.getMqttHost() + " as " + this.config.getClientId());
|
|
80
64
|
this.mqtt.reconnect();
|
|
81
65
|
return;
|
|
82
66
|
}
|
|
83
|
-
|
|
84
67
|
this.log.info("[BaseClient:connect] Connecting to " + this.config.getMqttHost() + " as " + this.config.getClientId());
|
|
85
68
|
this.mqtt = _mqtt["default"].connect(this.config.getMqttHost(), this.config.getMqttConfig());
|
|
69
|
+
|
|
86
70
|
/* Events coming from mqtt
|
|
87
71
|
* Event 'connect' - Emitted on successful (re)connection (i.e. connack rc=0).
|
|
88
72
|
* Event 'reconnect' - Emitted when a reconnect starts.
|
|
@@ -97,74 +81,60 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
97
81
|
|
|
98
82
|
this.mqtt.on('connect', function () {
|
|
99
83
|
_this2.log.info("[BaseClient:onConnect] MQTT client is connected.");
|
|
84
|
+
_this2.emit('connect');
|
|
100
85
|
|
|
101
|
-
|
|
86
|
+
// less than 3 connect attempts you get put to a connect delay of 1 second
|
|
102
87
|
// after 3 connect attempts you get put to a connect delay of 2 seconds (3 seconds elapsed - 3 attempts @ 1 second intervals)
|
|
103
88
|
// after 6 connect attempts you get put to a connect delay of 5 seconds (3 + 6 seconds elapsed - 3 attempts @ 2 second intervals)
|
|
104
89
|
// after 9 connect attempts you get put to a connect delay of 20 seconds (3 + 6 + 15 seconds elapsed - 3 attempts @ 5 second intervals)
|
|
90
|
+
var connectionLostCount = _this2.lostConnectionLog.size;
|
|
105
91
|
|
|
106
|
-
|
|
107
|
-
var connectionLostCount = _this2.lostConnectionLog.size; // Default is 1 second reconnect period
|
|
108
|
-
|
|
92
|
+
// Default is 1 second reconnect period
|
|
109
93
|
var reconnectPeriod = 1000;
|
|
110
|
-
|
|
111
94
|
if (connectionLostCount >= 9) {
|
|
112
|
-
reconnectPeriod = 20000;
|
|
95
|
+
reconnectPeriod = 20000;
|
|
113
96
|
|
|
97
|
+
// Log this and raise the error EVERY time we reconnect under these conditions.
|
|
114
98
|
_this2.log.warn("[BaseClient:onOffline] This client is likely suffering from clientId stealing (where two connections try to use the same client Id).");
|
|
115
|
-
|
|
116
99
|
_this2.emit("error", "Exceeded 9 connection losses in a 5 minute period. Check for clientId conflict with another connection.");
|
|
117
100
|
} else if (connectionLostCount >= 6) {
|
|
118
101
|
reconnectPeriod = 5000;
|
|
119
102
|
} else if (connectionLostCount >= 3) {
|
|
120
103
|
reconnectPeriod = 2000;
|
|
121
104
|
}
|
|
122
|
-
|
|
123
105
|
if (reconnectPeriod != _this2.mqtt.options.reconnectPeriod) {
|
|
124
106
|
_this2.log.info("[BaseClient:onOffline] Client has lost connection " + connectionLostCount + " times during the last 5 minutes, reconnect delay adjusted to " + reconnectPeriod + " ms");
|
|
125
|
-
|
|
126
107
|
_this2.mqtt.options.reconnectPeriod = reconnectPeriod;
|
|
127
108
|
}
|
|
128
109
|
});
|
|
129
110
|
this.mqtt.on('reconnect', function () {
|
|
130
|
-
_this2.log.info("[BaseClient:onReconnect] MQTT client is reconnecting.");
|
|
111
|
+
_this2.log.info("[BaseClient:onReconnect] MQTT client is reconnecting.");
|
|
112
|
+
// this.log.debug("[BaseClient:onReconnect] Resubscribe topics:");
|
|
131
113
|
// this.log.debug(this.mqtt._resubscribeTopics);
|
|
132
|
-
|
|
133
|
-
|
|
134
114
|
_this2.emit('reconnect');
|
|
135
115
|
});
|
|
136
116
|
this.mqtt.on('close', function () {
|
|
137
117
|
_this2.log.info("[BaseClient:onClose] MQTT client connection was closed.");
|
|
138
|
-
|
|
139
118
|
_this2.emit('close');
|
|
140
119
|
});
|
|
141
120
|
this.mqtt.on('offline', function () {
|
|
142
121
|
var newId = uuidv4();
|
|
143
|
-
|
|
144
122
|
_this2.log.info("[BaseClient:onOffline] MQTT client connection is offline. [" + newId + "]");
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
123
|
+
_this2.emit('offline');
|
|
124
|
+
// Record the disconnect event for 5 minutes
|
|
149
125
|
_this2.lostConnectionLog.put(newId, '1', 300000);
|
|
150
|
-
|
|
151
126
|
var connectionLostCount = _this2.lostConnectionLog.size;
|
|
152
|
-
|
|
153
127
|
_this2.log.info("[BaseClient:onOffline] Connection losses in the last 5 minutes: " + connectionLostCount);
|
|
154
128
|
});
|
|
155
129
|
this.mqtt.on('error', function (error) {
|
|
156
130
|
_this2.log.error("[BaseClient:onError] " + error);
|
|
157
|
-
|
|
158
131
|
var errorMsg = '' + error;
|
|
159
|
-
|
|
160
132
|
if (errorMsg.indexOf('Not authorized') > -1) {
|
|
161
133
|
_this2.log.error("[BaseClient:onError] One or more configuration parameters are wrong. Modify the configuration before trying to reconnect.");
|
|
162
|
-
|
|
163
134
|
_this2.mqtt.end(false, function () {
|
|
164
135
|
_this2.log.info("[BaseClient:onError] Closed the MQTT connection due to client misconfiguration");
|
|
165
136
|
});
|
|
166
137
|
}
|
|
167
|
-
|
|
168
138
|
_this2.emit('error', error);
|
|
169
139
|
});
|
|
170
140
|
}
|
|
@@ -172,12 +142,10 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
172
142
|
key: "disconnect",
|
|
173
143
|
value: function disconnect() {
|
|
174
144
|
var _this3 = this;
|
|
175
|
-
|
|
176
145
|
if (this.mqtt == null) {
|
|
177
146
|
this.log.info("[BaseClient:disconnect] Client was never connected");
|
|
178
147
|
return;
|
|
179
148
|
}
|
|
180
|
-
|
|
181
149
|
this.mqtt.end(false, function () {
|
|
182
150
|
_this3.log.info("[BaseClient:disconnect] Closed the MQTT connection due to disconnect() call");
|
|
183
151
|
});
|
|
@@ -189,14 +157,11 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
189
157
|
this.emit('error', "[BaseClient:_subscribe] MQTT Client is not initialized - call connect() first");
|
|
190
158
|
return;
|
|
191
159
|
}
|
|
192
|
-
|
|
193
160
|
if (!this.mqtt.connected) {
|
|
194
161
|
this.emit('error', "[BaseClient:_subscribe] MQTT Client is not connected - call connect() first");
|
|
195
162
|
return;
|
|
196
163
|
}
|
|
197
|
-
|
|
198
164
|
QoS = QoS || 0;
|
|
199
|
-
|
|
200
165
|
callback = callback || function (err, granted) {
|
|
201
166
|
if (err == null) {
|
|
202
167
|
for (var index in granted) {
|
|
@@ -208,7 +173,6 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
208
173
|
this.emit("error", err);
|
|
209
174
|
}
|
|
210
175
|
}.bind(this);
|
|
211
|
-
|
|
212
176
|
this.log.debug("[BaseClient:_subscribe] Subscribing to topic " + topic + " with QoS " + QoS);
|
|
213
177
|
this.mqtt.subscribe(topic, {
|
|
214
178
|
qos: parseInt(QoS)
|
|
@@ -221,12 +185,10 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
221
185
|
this.emit('error', "[BaseClient:_unsubscribe] MQTT Client is not initialized - call connect() first");
|
|
222
186
|
return;
|
|
223
187
|
}
|
|
224
|
-
|
|
225
188
|
if (!this.mqtt.connected) {
|
|
226
189
|
this.emit('error', "[BaseClient:_unsubscribe] MQTT Client is not connected - call connect() first");
|
|
227
190
|
return;
|
|
228
191
|
}
|
|
229
|
-
|
|
230
192
|
callback = callback || function (err) {
|
|
231
193
|
if (err == null) {
|
|
232
194
|
this.log.debug("[BaseClient:_unsubscribe] Unsubscribed from: " + topic);
|
|
@@ -235,7 +197,6 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
235
197
|
this.emit("error", err);
|
|
236
198
|
}
|
|
237
199
|
}.bind(this);
|
|
238
|
-
|
|
239
200
|
this.log.debug("[BaseClient:_unsubscribe] Unsubscribe: " + topic);
|
|
240
201
|
this.mqtt.unsubscribe(topic, callback);
|
|
241
202
|
}
|
|
@@ -243,31 +204,24 @@ var BaseClient = /*#__PURE__*/function (_events$EventEmitter) {
|
|
|
243
204
|
key: "_publish",
|
|
244
205
|
value: function _publish(topic, msg, QoS, callback) {
|
|
245
206
|
QoS = QoS || 0;
|
|
246
|
-
|
|
247
207
|
if (this.mqtt == null) {
|
|
248
208
|
this.emit('error', "[BaseClient:_unsubscribe] MQTT Client is not initialized - call connect() first");
|
|
249
209
|
return;
|
|
250
210
|
}
|
|
251
|
-
|
|
252
211
|
if (!this.mqtt.connected) {
|
|
253
212
|
this.emit('error', "[BaseClient:_unsubscribe] MQTT Client is not connected - call connect() first");
|
|
254
213
|
return;
|
|
255
214
|
}
|
|
256
|
-
|
|
257
215
|
if ((_typeof(msg) === 'object' || typeof msg === 'boolean' || typeof msg === 'number') && !Buffer.isBuffer(msg)) {
|
|
258
216
|
// mqtt library does not support sending JSON/Boolean/Number data. So stringifying it.
|
|
259
217
|
// All JSON object, array will be encoded.
|
|
260
218
|
msg = JSON.stringify(msg);
|
|
261
219
|
}
|
|
262
|
-
|
|
263
220
|
this.log.debug("[BaseClient:_publish] Publish: " + topic + ", " + msg + ", QoS : " + QoS);
|
|
264
221
|
this.mqtt.publish(topic, msg, {
|
|
265
222
|
qos: parseInt(QoS)
|
|
266
223
|
}, callback);
|
|
267
224
|
}
|
|
268
225
|
}]);
|
|
269
|
-
|
|
270
226
|
return BaseClient;
|
|
271
|
-
}(_events["default"].EventEmitter);
|
|
272
|
-
|
|
273
|
-
exports["default"] = BaseClient;
|
|
227
|
+
}(_events["default"].EventEmitter);
|
package/dist/BaseConfig.js
CHANGED
|
@@ -4,13 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
7
|
+
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); }
|
|
8
8
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
|
|
9
|
+
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, _toPropertyKey(descriptor.key), descriptor); } }
|
|
10
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
11
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
|
+
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); }
|
|
14
13
|
/**
|
|
15
14
|
*****************************************************************************
|
|
16
15
|
Copyright (c) 2019 IBM Corporation and other Contributors.
|
|
@@ -21,13 +20,14 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
21
20
|
*****************************************************************************
|
|
22
21
|
*
|
|
23
22
|
*/
|
|
24
|
-
var BaseConfig = /*#__PURE__*/function () {
|
|
23
|
+
var BaseConfig = exports["default"] = /*#__PURE__*/function () {
|
|
25
24
|
function BaseConfig(identity, auth, options) {
|
|
26
25
|
_classCallCheck(this, BaseConfig);
|
|
27
|
-
|
|
28
26
|
this.identity = identity;
|
|
29
27
|
this.auth = auth;
|
|
30
|
-
this.options = options;
|
|
28
|
+
this.options = options;
|
|
29
|
+
|
|
30
|
+
// Validation for options common to all confiugration
|
|
31
31
|
|
|
32
32
|
if (this.options != null && "mqtt" in this.options) {
|
|
33
33
|
// validate port
|
|
@@ -35,56 +35,45 @@ var BaseConfig = /*#__PURE__*/function () {
|
|
|
35
35
|
if (isNaN(this.options.mqtt.port)) {
|
|
36
36
|
throw new Error("Optional setting options.mqtt.port must be a number if provided");
|
|
37
37
|
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
}
|
|
39
|
+
// Validate cleanStart
|
|
41
40
|
if ("cleanStart" in this.options.mqtt && typeof this.options.mqtt.cleanStart != "boolean") {
|
|
42
41
|
throw new Error("Optional setting options.mqtt.cleanStart must be a boolean if provided");
|
|
43
42
|
}
|
|
44
|
-
}
|
|
45
|
-
|
|
43
|
+
}
|
|
46
44
|
|
|
45
|
+
// Set defaults for optional configuration
|
|
47
46
|
if (this.options == null) {
|
|
48
47
|
this.options = {};
|
|
49
48
|
}
|
|
50
|
-
|
|
51
49
|
if (!("domain" in this.options) || this.options.domain == null) {
|
|
52
50
|
this.options.domain = "internetofthings.ibmcloud.com";
|
|
53
51
|
}
|
|
54
|
-
|
|
55
52
|
if (!("logLevel" in this.options) || this.options.logLevel == null) {
|
|
56
53
|
this.options.logLevel = "info";
|
|
57
54
|
}
|
|
58
|
-
|
|
59
55
|
if (!("mqtt" in this.options)) {
|
|
60
56
|
this.options.mqtt = {};
|
|
61
57
|
}
|
|
62
|
-
|
|
63
58
|
if (!("port" in this.options.mqtt) || this.options.mqtt.port == null) {
|
|
64
59
|
this.options.mqtt.port = 8883;
|
|
65
60
|
}
|
|
66
|
-
|
|
67
61
|
if (!("transport" in this.options.mqtt) || this.options.mqtt.transport == null) {
|
|
68
62
|
this.options.mqtt.transport = "tcp";
|
|
69
63
|
}
|
|
70
|
-
|
|
71
64
|
if (!("cleanStart" in this.options.mqtt)) {
|
|
72
65
|
this.options.mqtt.cleanStart = true;
|
|
73
66
|
}
|
|
74
|
-
|
|
75
67
|
if (!("sessionExpiry" in this.options.mqtt)) {
|
|
76
68
|
this.options.mqtt.sessionExpiry = 3600;
|
|
77
69
|
}
|
|
78
|
-
|
|
79
70
|
if (!("keepAlive" in this.options.mqtt)) {
|
|
80
71
|
this.options.mqtt.keepAlive = 60;
|
|
81
72
|
}
|
|
82
|
-
|
|
83
73
|
if (!("caFile" in this.options.mqtt)) {
|
|
84
74
|
this.options.mqtt.caFile = null;
|
|
85
75
|
}
|
|
86
76
|
}
|
|
87
|
-
|
|
88
77
|
_createClass(BaseConfig, [{
|
|
89
78
|
key: "getOrgId",
|
|
90
79
|
value: function getOrgId() {
|
|
@@ -128,8 +117,10 @@ var BaseConfig = /*#__PURE__*/function () {
|
|
|
128
117
|
// if connection is broken, queue outgoing QoS zero messages
|
|
129
118
|
resubscribe: true,
|
|
130
119
|
// if connection is broken and reconnects, subscribed topics are automatically subscribed again
|
|
120
|
+
|
|
131
121
|
clean: this.options.mqtt.cleanStart,
|
|
132
122
|
// set to false to receive QoS 1 and 2 messages while offline
|
|
123
|
+
|
|
133
124
|
// Authentication
|
|
134
125
|
username: this.getMqttUsername(),
|
|
135
126
|
password: this.getMqttPassword(),
|
|
@@ -137,40 +128,40 @@ var BaseConfig = /*#__PURE__*/function () {
|
|
|
137
128
|
// If you are using a self-signed certificate, pass the rejectUnauthorized: false option. Beware
|
|
138
129
|
// that you are exposing yourself to man in the middle attacks, so it is a configuration that
|
|
139
130
|
// is not recommended for production environments.
|
|
140
|
-
rejectUnauthorized: true
|
|
131
|
+
rejectUnauthorized: true
|
|
132
|
+
|
|
133
|
+
// MQTTv5 support doesn't work with Watson IoT Platform, so stick to default for now
|
|
141
134
|
// protocolId: "MQTT",
|
|
142
135
|
// protocolVersion: 5
|
|
143
|
-
|
|
144
136
|
};
|
|
145
137
|
return mqttConfig;
|
|
146
138
|
}
|
|
147
139
|
}, {
|
|
148
140
|
key: "getMqttHost",
|
|
149
141
|
value: function getMqttHost() {
|
|
150
|
-
var server = this.getOrgId() + ".messaging." + this.options.domain + ":" + this.options.mqtt.port;
|
|
142
|
+
var server = this.getOrgId() + ".messaging." + this.options.domain + ":" + this.options.mqtt.port;
|
|
151
143
|
|
|
144
|
+
// For unencrpyted ports
|
|
152
145
|
if (this.options.mqtt.port == 80 || this.options.mqtt.port == 1883) {
|
|
153
146
|
if (this.options.mqtt.transport == "tcp") {
|
|
154
147
|
return "tcp://" + server;
|
|
155
148
|
}
|
|
156
|
-
|
|
157
149
|
if (this.options.mqtt.transport == "websockets") {
|
|
158
150
|
return "ws://" + server;
|
|
159
151
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
152
|
+
}
|
|
162
153
|
|
|
154
|
+
// For encrypted ports
|
|
163
155
|
if (this.options.mqtt.port == 433 || this.options.mqtt.port == 8883) {
|
|
164
156
|
if (this.options.mqtt.transport == "tcp") {
|
|
165
157
|
return "ssl://" + server;
|
|
166
158
|
}
|
|
167
|
-
|
|
168
159
|
if (this.options.mqtt.transport == "websockets") {
|
|
169
160
|
return "wss://" + server;
|
|
170
161
|
}
|
|
171
|
-
}
|
|
172
|
-
|
|
162
|
+
}
|
|
173
163
|
|
|
164
|
+
// Default to something, but really shouldn't hit this scenario unless misconfigured
|
|
174
165
|
return "ssl://" + server;
|
|
175
166
|
}
|
|
176
167
|
}], [{
|
|
@@ -184,9 +175,6 @@ var BaseConfig = /*#__PURE__*/function () {
|
|
|
184
175
|
throw new Error("Sub class must implement parseConfigFile()");
|
|
185
176
|
}
|
|
186
177
|
}]);
|
|
187
|
-
|
|
188
178
|
return BaseConfig;
|
|
189
179
|
}();
|
|
190
|
-
|
|
191
|
-
exports["default"] = BaseConfig;
|
|
192
180
|
;
|