@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.
- package/LICENSE +203 -203
- package/README.md +68 -68
- package/dist/BaseClient.js +259 -0
- package/dist/BaseConfig.js +194 -0
- package/dist/api/ApiClient.js +508 -0
- package/dist/api/ApiErrors.js +118 -0
- package/dist/api/DscClient.js +332 -0
- package/dist/api/LecClient.js +48 -0
- package/dist/api/MgmtClient.js +77 -0
- package/dist/api/RegistryClient.js +234 -0
- package/dist/api/RulesClient.js +105 -0
- package/dist/api/StateClient.js +738 -0
- package/dist/application/ApplicationClient.js +436 -0
- package/dist/application/ApplicationConfig.js +233 -0
- package/dist/application/index.js +23 -0
- package/dist/bundled/wiotp-bundle.js +35592 -0
- package/dist/bundled/wiotp-bundle.min.js +47 -0
- package/dist/device/DeviceClient.js +125 -0
- package/dist/device/DeviceConfig.js +216 -0
- package/dist/device/index.js +23 -0
- package/dist/gateway/GatewayClient.js +159 -0
- package/dist/gateway/GatewayConfig.js +52 -0
- package/dist/gateway/index.js +23 -0
- package/dist/index.js +55 -0
- package/dist/util.js +50 -0
- package/package.json +92 -84
- package/src/BaseClient.js +215 -215
- package/src/BaseConfig.js +157 -157
- package/src/api/ApiClient.js +454 -454
- package/src/api/ApiErrors.js +33 -33
- package/src/api/DscClient.js +164 -145
- package/src/api/LecClient.js +32 -32
- package/src/api/MgmtClient.js +57 -57
- package/src/api/RegistryClient.js +194 -194
- package/src/api/RulesClient.js +84 -84
- package/src/api/StateClient.js +650 -650
- package/src/application/ApplicationClient.js +348 -348
- package/src/application/ApplicationConfig.js +191 -191
- package/src/application/index.js +12 -12
- package/src/device/DeviceClient.js +78 -78
- package/src/device/DeviceConfig.js +175 -175
- package/src/device/index.js +14 -14
- package/src/gateway/GatewayClient.js +114 -114
- package/src/gateway/GatewayConfig.js +21 -21
- package/src/gateway/index.js +13 -13
- package/{index.js → src/index.js} +19 -19
- package/src/util.js +38 -38
- package/src/util/IoTFoundation.pem +0 -82
|
@@ -1,192 +1,192 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************************************************
|
|
3
|
-
Copyright (c) 2019 IBM Corporation and other Contributors.
|
|
4
|
-
All rights reserved. This program and the accompanying materials
|
|
5
|
-
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
-
which accompanies this distribution, and is available at
|
|
7
|
-
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
-
*****************************************************************************
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
import { default as BaseConfig } from '../BaseConfig';
|
|
12
|
-
import log from 'loglevel';
|
|
13
|
-
const YAML = require('yaml');
|
|
14
|
-
const fs = require('fs');
|
|
15
|
-
|
|
16
|
-
const uuidv4 = require('uuid/v4');
|
|
17
|
-
|
|
18
|
-
export default class ApplicationConfig extends BaseConfig{
|
|
19
|
-
constructor(identity, auth, options) {
|
|
20
|
-
super(identity, auth, options);
|
|
21
|
-
|
|
22
|
-
// Authentication is not supported for quickstart
|
|
23
|
-
// and not required when auth.useLtpa is set
|
|
24
|
-
if (this.auth != null && !this.auth.useLtpa) {
|
|
25
|
-
if (!("key" in this.auth) || this.auth.key == null) {
|
|
26
|
-
throw new Error("Missing auth.key from configuration");
|
|
27
|
-
}
|
|
28
|
-
if (!("token" in this.auth) || this.auth.token == null) {
|
|
29
|
-
throw new Error("Missing auth.token from configuration");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Set defaults for optional configuration
|
|
34
|
-
if (this.identity == null) {
|
|
35
|
-
this.identity = {};
|
|
36
|
-
}
|
|
37
|
-
if (!("appId" in this.identity)) {
|
|
38
|
-
this.identity.appId = uuidv4();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!("sharedSubscription" in this.options.mqtt)) {
|
|
42
|
-
this.options.mqtt.sharedSubscription = false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
getOrgId() {
|
|
47
|
-
if (this.auth == null) {
|
|
48
|
-
return "quickstart";
|
|
49
|
-
} else if (this.auth.key) {
|
|
50
|
-
return this.auth.key.split("-")[1];
|
|
51
|
-
} else {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
getApiBaseUri() {
|
|
57
|
-
return this.auth && this.auth.useLtpa
|
|
58
|
-
? `/api/v0002`
|
|
59
|
-
: `https://${this.getOrgId()}.${this.options.domain}/api/v0002`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
getClientId() {
|
|
63
|
-
let clientIdPrefix = "a";
|
|
64
|
-
if (this.sharedSubscription == true) {
|
|
65
|
-
clientIdPrefix = "A";
|
|
66
|
-
}
|
|
67
|
-
return clientIdPrefix + ":" + this.getOrgId() + ":" + this.identity.appId;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
getMqttUsername() {
|
|
71
|
-
return this.auth.key;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getMqttPassword() {
|
|
75
|
-
return this.auth.token;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static parseEnvVars() {
|
|
79
|
-
// Auth
|
|
80
|
-
let authKey = process.env.WIOTP_AUTH_KEY || null;
|
|
81
|
-
let authToken = process.env.WIOTP_AUTH_TOKEN || null;
|
|
82
|
-
|
|
83
|
-
// Also support WIOTP_API_KEY / WIOTP_API_TOKEN usage
|
|
84
|
-
if (authKey == null && authToken == null) {
|
|
85
|
-
authKey = process.env.WIOTP_API_KEY || null;
|
|
86
|
-
authToken = process.env.WIOTP_API_TOKEN || null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Identity
|
|
90
|
-
let appId = process.env.WIOTP_IDENTITY_APPID || uuidv4();
|
|
91
|
-
|
|
92
|
-
// Options
|
|
93
|
-
let domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
|
|
94
|
-
let logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
|
|
95
|
-
let port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
|
|
96
|
-
let transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
|
|
97
|
-
let caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
|
|
98
|
-
let cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
|
|
99
|
-
let sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
|
|
100
|
-
let keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
|
|
101
|
-
let sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
|
|
102
|
-
let verifyCert = process.env.WIOTP_OPTIONS_HTTP_VERIFY || "true";
|
|
103
|
-
|
|
104
|
-
// String to int conversions
|
|
105
|
-
if (port != null) {
|
|
106
|
-
port = parseInt(port);
|
|
107
|
-
}
|
|
108
|
-
sessionExpiry = parseInt(sessionExpiry);
|
|
109
|
-
keepAlive = parseInt(keepAlive)
|
|
110
|
-
|
|
111
|
-
let identity = {appId: appId};
|
|
112
|
-
let options = {
|
|
113
|
-
domain: domain,
|
|
114
|
-
logLevel: logLevel,
|
|
115
|
-
mqtt: {
|
|
116
|
-
port: port,
|
|
117
|
-
transport: transport,
|
|
118
|
-
cleanStart: (["True", "true", "1"].includes(cleanStart)),
|
|
119
|
-
sessionExpiry: sessionExpiry,
|
|
120
|
-
keepAlive: keepAlive,
|
|
121
|
-
sharedSubscription: (["True", "true", "1"].includes(sharedSubs)),
|
|
122
|
-
caFile: caFile,
|
|
123
|
-
},
|
|
124
|
-
http: {
|
|
125
|
-
verify: (["True", "true", "1"].includes(verifyCert))
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
let auth = null;
|
|
129
|
-
// Quickstart doesn't support auth, so ensure we only add this if it's defined
|
|
130
|
-
if (authToken != null) {
|
|
131
|
-
auth = {key: authKey, token: authToken};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return new ApplicationConfig(identity, auth, options);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
static parseConfigFile(configFilePath) {
|
|
138
|
-
|
|
139
|
-
//Example Application Configuration File:
|
|
140
|
-
|
|
141
|
-
/*
|
|
142
|
-
identity:
|
|
143
|
-
appId: myApp
|
|
144
|
-
auth:
|
|
145
|
-
key: a-23gh56-sdsdajhjnee
|
|
146
|
-
token: Ab$76s)asj8_s5
|
|
147
|
-
options:
|
|
148
|
-
domain: internetofthings.ibmcloud.com
|
|
149
|
-
logLevel: error|warning|info|debug
|
|
150
|
-
mqtt:
|
|
151
|
-
instanceId: myInstance
|
|
152
|
-
port: 8883
|
|
153
|
-
transport: tcp
|
|
154
|
-
cleanStart: false
|
|
155
|
-
sessionExpiry: 3600
|
|
156
|
-
keepAlive: 60
|
|
157
|
-
caFile: /path/to/certificateAuthorityFile.pem
|
|
158
|
-
http:
|
|
159
|
-
verify: true
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
163
|
-
var data = YAML.parse(configFile);
|
|
164
|
-
|
|
165
|
-
if(!fs.existsSync(configFilePath)) {
|
|
166
|
-
throw new Error("File not found");
|
|
167
|
-
}else
|
|
168
|
-
{try {
|
|
169
|
-
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
170
|
-
var data = YAML.parse(configFile);
|
|
171
|
-
} catch (err) {
|
|
172
|
-
throw new Error("Error reading device configuration file: " + err.code);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (("options" in data) & ("logLevel" in data["options"]))
|
|
178
|
-
{
|
|
179
|
-
var validLevels = ["error", "warning", "info", "debug"];
|
|
180
|
-
if (!(validLevels.includes(data["options"]["logLevel"])))
|
|
181
|
-
{
|
|
182
|
-
throw new Error("Optional setting options.logLevel must be one of error, warning, info, debug"
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
else
|
|
186
|
-
{
|
|
187
|
-
data["options"]["logLevel"] = log.GetLogger(data["options"]["logLevel"].toUpperCase());
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return new ApplicationConfig(data['identity'],data['auth'],data['options'])
|
|
191
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
*****************************************************************************
|
|
3
|
+
Copyright (c) 2019 IBM Corporation and other Contributors.
|
|
4
|
+
All rights reserved. This program and the accompanying materials
|
|
5
|
+
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
+
which accompanies this distribution, and is available at
|
|
7
|
+
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
+
*****************************************************************************
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
import { default as BaseConfig } from '../BaseConfig';
|
|
12
|
+
import log from 'loglevel';
|
|
13
|
+
const YAML = require('yaml');
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
|
|
16
|
+
const uuidv4 = require('uuid/v4');
|
|
17
|
+
|
|
18
|
+
export default class ApplicationConfig extends BaseConfig{
|
|
19
|
+
constructor(identity, auth, options) {
|
|
20
|
+
super(identity, auth, options);
|
|
21
|
+
|
|
22
|
+
// Authentication is not supported for quickstart
|
|
23
|
+
// and not required when auth.useLtpa is set
|
|
24
|
+
if (this.auth != null && !this.auth.useLtpa) {
|
|
25
|
+
if (!("key" in this.auth) || this.auth.key == null) {
|
|
26
|
+
throw new Error("Missing auth.key from configuration");
|
|
27
|
+
}
|
|
28
|
+
if (!("token" in this.auth) || this.auth.token == null) {
|
|
29
|
+
throw new Error("Missing auth.token from configuration");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Set defaults for optional configuration
|
|
34
|
+
if (this.identity == null) {
|
|
35
|
+
this.identity = {};
|
|
36
|
+
}
|
|
37
|
+
if (!("appId" in this.identity)) {
|
|
38
|
+
this.identity.appId = uuidv4();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!("sharedSubscription" in this.options.mqtt)) {
|
|
42
|
+
this.options.mqtt.sharedSubscription = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getOrgId() {
|
|
47
|
+
if (this.auth == null) {
|
|
48
|
+
return "quickstart";
|
|
49
|
+
} else if (this.auth.key) {
|
|
50
|
+
return this.auth.key.split("-")[1];
|
|
51
|
+
} else {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getApiBaseUri() {
|
|
57
|
+
return this.auth && this.auth.useLtpa
|
|
58
|
+
? `/api/v0002`
|
|
59
|
+
: `https://${this.getOrgId()}.${this.options.domain}/api/v0002`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getClientId() {
|
|
63
|
+
let clientIdPrefix = "a";
|
|
64
|
+
if (this.sharedSubscription == true) {
|
|
65
|
+
clientIdPrefix = "A";
|
|
66
|
+
}
|
|
67
|
+
return clientIdPrefix + ":" + this.getOrgId() + ":" + this.identity.appId;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getMqttUsername() {
|
|
71
|
+
return this.auth.key;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
getMqttPassword() {
|
|
75
|
+
return this.auth.token;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static parseEnvVars() {
|
|
79
|
+
// Auth
|
|
80
|
+
let authKey = process.env.WIOTP_AUTH_KEY || null;
|
|
81
|
+
let authToken = process.env.WIOTP_AUTH_TOKEN || null;
|
|
82
|
+
|
|
83
|
+
// Also support WIOTP_API_KEY / WIOTP_API_TOKEN usage
|
|
84
|
+
if (authKey == null && authToken == null) {
|
|
85
|
+
authKey = process.env.WIOTP_API_KEY || null;
|
|
86
|
+
authToken = process.env.WIOTP_API_TOKEN || null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Identity
|
|
90
|
+
let appId = process.env.WIOTP_IDENTITY_APPID || uuidv4();
|
|
91
|
+
|
|
92
|
+
// Options
|
|
93
|
+
let domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
|
|
94
|
+
let logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
|
|
95
|
+
let port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
|
|
96
|
+
let transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
|
|
97
|
+
let caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
|
|
98
|
+
let cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
|
|
99
|
+
let sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
|
|
100
|
+
let keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
|
|
101
|
+
let sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
|
|
102
|
+
let verifyCert = process.env.WIOTP_OPTIONS_HTTP_VERIFY || "true";
|
|
103
|
+
|
|
104
|
+
// String to int conversions
|
|
105
|
+
if (port != null) {
|
|
106
|
+
port = parseInt(port);
|
|
107
|
+
}
|
|
108
|
+
sessionExpiry = parseInt(sessionExpiry);
|
|
109
|
+
keepAlive = parseInt(keepAlive)
|
|
110
|
+
|
|
111
|
+
let identity = {appId: appId};
|
|
112
|
+
let options = {
|
|
113
|
+
domain: domain,
|
|
114
|
+
logLevel: logLevel,
|
|
115
|
+
mqtt: {
|
|
116
|
+
port: port,
|
|
117
|
+
transport: transport,
|
|
118
|
+
cleanStart: (["True", "true", "1"].includes(cleanStart)),
|
|
119
|
+
sessionExpiry: sessionExpiry,
|
|
120
|
+
keepAlive: keepAlive,
|
|
121
|
+
sharedSubscription: (["True", "true", "1"].includes(sharedSubs)),
|
|
122
|
+
caFile: caFile,
|
|
123
|
+
},
|
|
124
|
+
http: {
|
|
125
|
+
verify: (["True", "true", "1"].includes(verifyCert))
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
let auth = null;
|
|
129
|
+
// Quickstart doesn't support auth, so ensure we only add this if it's defined
|
|
130
|
+
if (authToken != null) {
|
|
131
|
+
auth = {key: authKey, token: authToken};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return new ApplicationConfig(identity, auth, options);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static parseConfigFile(configFilePath) {
|
|
138
|
+
|
|
139
|
+
//Example Application Configuration File:
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
identity:
|
|
143
|
+
appId: myApp
|
|
144
|
+
auth:
|
|
145
|
+
key: a-23gh56-sdsdajhjnee
|
|
146
|
+
token: Ab$76s)asj8_s5
|
|
147
|
+
options:
|
|
148
|
+
domain: internetofthings.ibmcloud.com
|
|
149
|
+
logLevel: error|warning|info|debug
|
|
150
|
+
mqtt:
|
|
151
|
+
instanceId: myInstance
|
|
152
|
+
port: 8883
|
|
153
|
+
transport: tcp
|
|
154
|
+
cleanStart: false
|
|
155
|
+
sessionExpiry: 3600
|
|
156
|
+
keepAlive: 60
|
|
157
|
+
caFile: /path/to/certificateAuthorityFile.pem
|
|
158
|
+
http:
|
|
159
|
+
verify: true
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
163
|
+
var data = YAML.parse(configFile);
|
|
164
|
+
|
|
165
|
+
if(!fs.existsSync(configFilePath)) {
|
|
166
|
+
throw new Error("File not found");
|
|
167
|
+
}else
|
|
168
|
+
{try {
|
|
169
|
+
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
170
|
+
var data = YAML.parse(configFile);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
throw new Error("Error reading device configuration file: " + err.code);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
if (("options" in data) & ("logLevel" in data["options"]))
|
|
178
|
+
{
|
|
179
|
+
var validLevels = ["error", "warning", "info", "debug"];
|
|
180
|
+
if (!(validLevels.includes(data["options"]["logLevel"])))
|
|
181
|
+
{
|
|
182
|
+
throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug")
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else
|
|
186
|
+
{
|
|
187
|
+
data["options"]["logLevel"] = log.GetLogger(data["options"]["logLevel"].toUpperCase());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return new ApplicationConfig(data['identity'],data['auth'],data['options'])
|
|
191
|
+
}
|
|
192
192
|
}
|
package/src/application/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************************************************
|
|
3
|
-
Copyright (c) 2019 IBM Corporation and other Contributors.
|
|
4
|
-
All rights reserved. This program and the accompanying materials
|
|
5
|
-
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
-
which accompanies this distribution, and is available at
|
|
7
|
-
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
-
*****************************************************************************
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export { default as ApplicationClient } from './ApplicationClient';
|
|
1
|
+
/**
|
|
2
|
+
*****************************************************************************
|
|
3
|
+
Copyright (c) 2019 IBM Corporation and other Contributors.
|
|
4
|
+
All rights reserved. This program and the accompanying materials
|
|
5
|
+
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
+
which accompanies this distribution, and is available at
|
|
7
|
+
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
+
*****************************************************************************
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export { default as ApplicationClient } from './ApplicationClient';
|
|
13
13
|
export { default as ApplicationConfig } from './ApplicationConfig';
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************************************************
|
|
3
|
-
Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
|
|
4
|
-
All rights reserved. This program and the accompanying materials
|
|
5
|
-
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
-
which accompanies this distribution, and is available at
|
|
7
|
-
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
-
*****************************************************************************
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
import { isDefined } from '../util';
|
|
12
|
-
import { default as BaseClient } from '../BaseClient';
|
|
13
|
-
import { default as DeviceConfig } from './DeviceConfig';
|
|
14
|
-
|
|
15
|
-
const WILDCARD_TOPIC = 'iot-2/cmd/+/fmt/+';
|
|
16
|
-
const CMD_RE = /^iot-2\/cmd\/(.+)\/fmt\/(.+)$/;
|
|
17
|
-
|
|
18
|
-
const util = require('util');
|
|
19
|
-
|
|
20
|
-
export default class DeviceClient extends BaseClient {
|
|
21
|
-
|
|
22
|
-
constructor(config){
|
|
23
|
-
if (!config instanceof DeviceConfig) {
|
|
24
|
-
throw new Error("Config must be an instance of DeviceConfig");
|
|
25
|
-
}
|
|
26
|
-
super(config);
|
|
27
|
-
|
|
28
|
-
this.log.debug("[DeviceClient:constructor] DeviceClient initialized for " + config.getClientId());
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
_commandSubscriptionCallback(err, granted) {
|
|
32
|
-
if (err == null) {
|
|
33
|
-
for (var index in granted) {
|
|
34
|
-
let grant = granted[index];
|
|
35
|
-
this.log.debug("[DeviceClient:connect] Subscribed to device commands on " + grant.topic + " at QoS " + grant.qos);
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
this.log.error("[DeviceClient:connect] Unable to establish subscription for device commands: " + err);
|
|
39
|
-
this.emit("error", new Error("Unable to establish subscription for device commands: " + err));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
connect(){
|
|
44
|
-
super.connect();
|
|
45
|
-
|
|
46
|
-
this.mqtt.on('connect', () => {
|
|
47
|
-
// On connect establish a subscription for commands sent to this device (but not if connecting to quickstart)
|
|
48
|
-
if(!this.config.isQuickstart()){
|
|
49
|
-
// You need to bind a particular this context to the method before you can use it as a callback
|
|
50
|
-
this.mqtt.subscribe(WILDCARD_TOPIC, { qos: 1 }, this._commandSubscriptionCallback.bind(this));
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
this.mqtt.on('message', (topic, payload) => {
|
|
55
|
-
this.log.debug("[DeviceClient:onMessage] Message received on topic : "+ topic + " with payload : "+ payload);
|
|
56
|
-
|
|
57
|
-
let match = CMD_RE.exec(topic);
|
|
58
|
-
|
|
59
|
-
if (match) {
|
|
60
|
-
this.emit('command', match[1], match[2], payload, topic);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
publishEvent(eventId, format, data, qos, callback){
|
|
66
|
-
qos = qos || 0;
|
|
67
|
-
|
|
68
|
-
if (!isDefined(eventId) || !isDefined(format)) {
|
|
69
|
-
this.log.error("[DeviceClient:publishEvent] Required params for publishEvent not present");
|
|
70
|
-
this.emit('error', "[DeviceClient:publishEvent] Required params for publishEvent not present");
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let topic = util.format("iot-2/evt/%s/fmt/%s", eventId, format);
|
|
75
|
-
this._publish(topic, data, qos, callback);
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
*****************************************************************************
|
|
3
|
+
Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
|
|
4
|
+
All rights reserved. This program and the accompanying materials
|
|
5
|
+
are made available under the terms of the Eclipse Public License v1.0
|
|
6
|
+
which accompanies this distribution, and is available at
|
|
7
|
+
http://www.eclipse.org/legal/epl-v10.html
|
|
8
|
+
*****************************************************************************
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
import { isDefined } from '../util';
|
|
12
|
+
import { default as BaseClient } from '../BaseClient';
|
|
13
|
+
import { default as DeviceConfig } from './DeviceConfig';
|
|
14
|
+
|
|
15
|
+
const WILDCARD_TOPIC = 'iot-2/cmd/+/fmt/+';
|
|
16
|
+
const CMD_RE = /^iot-2\/cmd\/(.+)\/fmt\/(.+)$/;
|
|
17
|
+
|
|
18
|
+
const util = require('util');
|
|
19
|
+
|
|
20
|
+
export default class DeviceClient extends BaseClient {
|
|
21
|
+
|
|
22
|
+
constructor(config){
|
|
23
|
+
if (!config instanceof DeviceConfig) {
|
|
24
|
+
throw new Error("Config must be an instance of DeviceConfig");
|
|
25
|
+
}
|
|
26
|
+
super(config);
|
|
27
|
+
|
|
28
|
+
this.log.debug("[DeviceClient:constructor] DeviceClient initialized for " + config.getClientId());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_commandSubscriptionCallback(err, granted) {
|
|
32
|
+
if (err == null) {
|
|
33
|
+
for (var index in granted) {
|
|
34
|
+
let grant = granted[index];
|
|
35
|
+
this.log.debug("[DeviceClient:connect] Subscribed to device commands on " + grant.topic + " at QoS " + grant.qos);
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
this.log.error("[DeviceClient:connect] Unable to establish subscription for device commands: " + err);
|
|
39
|
+
this.emit("error", new Error("Unable to establish subscription for device commands: " + err));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
connect(){
|
|
44
|
+
super.connect();
|
|
45
|
+
|
|
46
|
+
this.mqtt.on('connect', () => {
|
|
47
|
+
// On connect establish a subscription for commands sent to this device (but not if connecting to quickstart)
|
|
48
|
+
if(!this.config.isQuickstart()){
|
|
49
|
+
// You need to bind a particular this context to the method before you can use it as a callback
|
|
50
|
+
this.mqtt.subscribe(WILDCARD_TOPIC, { qos: 1 }, this._commandSubscriptionCallback.bind(this));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
this.mqtt.on('message', (topic, payload) => {
|
|
55
|
+
this.log.debug("[DeviceClient:onMessage] Message received on topic : "+ topic + " with payload : "+ payload);
|
|
56
|
+
|
|
57
|
+
let match = CMD_RE.exec(topic);
|
|
58
|
+
|
|
59
|
+
if (match) {
|
|
60
|
+
this.emit('command', match[1], match[2], payload, topic);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
publishEvent(eventId, format, data, qos, callback){
|
|
66
|
+
qos = qos || 0;
|
|
67
|
+
|
|
68
|
+
if (!isDefined(eventId) || !isDefined(format)) {
|
|
69
|
+
this.log.error("[DeviceClient:publishEvent] Required params for publishEvent not present");
|
|
70
|
+
this.emit('error', "[DeviceClient:publishEvent] Required params for publishEvent not present");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let topic = util.format("iot-2/evt/%s/fmt/%s", eventId, format);
|
|
75
|
+
this._publish(topic, data, qos, callback);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
}
|