@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,176 +1,176 @@
|
|
|
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
|
-
|
|
17
|
-
export default class DeviceConfig extends BaseConfig{
|
|
18
|
-
constructor(identity, auth, options) {
|
|
19
|
-
super(identity, auth, options);
|
|
20
|
-
|
|
21
|
-
//Validate the arguments
|
|
22
|
-
if (this.identity == null) {
|
|
23
|
-
throw new Error("Missing identity from configuration");
|
|
24
|
-
}
|
|
25
|
-
if (!("orgId" in this.identity) || this.identity.orgId == null) {
|
|
26
|
-
throw new Error("Missing identity.orgId from configuration");
|
|
27
|
-
}
|
|
28
|
-
if (!("typeId" in this.identity) || this.identity == null) {
|
|
29
|
-
throw new Error("Missing identity.typeId from configuration");
|
|
30
|
-
}
|
|
31
|
-
if (!("deviceId" in this.identity) || this.identity.deviceId == null) {
|
|
32
|
-
throw new Error("Missing identity.deviceId from configuration");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Authentication is not supported for quickstart
|
|
36
|
-
if (this.identity.orgId == "quickstart") {
|
|
37
|
-
if (this.auth != null) {
|
|
38
|
-
throw new Error("Quickstart service does not support device authentication");
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
if (this.auth == null) {
|
|
42
|
-
throw new Error("Missing auth from configuration");
|
|
43
|
-
}
|
|
44
|
-
if (!("token" in this.auth) || this.auth.token == null) {
|
|
45
|
-
throw new Error("Missing auth.token from configuration");
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
getOrgId() {
|
|
52
|
-
return this.identity.orgId;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
getClientId() {
|
|
56
|
-
return "d:" + this.identity.orgId + ":" + this.identity.typeId + ":" + this.identity.deviceId;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getMqttUsername() {
|
|
60
|
-
return "use-token-auth";
|
|
61
|
-
}
|
|
62
|
-
getMqttPassword() {
|
|
63
|
-
return this.auth.token;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
static parseEnvVars() {
|
|
67
|
-
|
|
68
|
-
//Identity
|
|
69
|
-
let orgId = process.env.WIOTP_IDENTITY_ORGID || null;
|
|
70
|
-
let typeId = process.env.WIOTP_IDENTITY_TYPEID || null;
|
|
71
|
-
let deviceId = process.env.WIOTP_IDENTITY_DEVICEID || null;
|
|
72
|
-
|
|
73
|
-
// Auth
|
|
74
|
-
let authToken = process.env.WIOTP_AUTH_TOKEN || null;
|
|
75
|
-
|
|
76
|
-
// Also support WIOTP_API_TOKEN usage
|
|
77
|
-
if (authToken == null) {
|
|
78
|
-
authToken = process.env.WIOTP_API_TOKEN || null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Options
|
|
82
|
-
let domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
|
|
83
|
-
let logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
|
|
84
|
-
let port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
|
|
85
|
-
let transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
|
|
86
|
-
let caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
|
|
87
|
-
let cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
|
|
88
|
-
let sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
|
|
89
|
-
let keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
|
|
90
|
-
let sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
|
|
91
|
-
|
|
92
|
-
// String to int conversions
|
|
93
|
-
if (port != null) {
|
|
94
|
-
port = parseInt(port);
|
|
95
|
-
}
|
|
96
|
-
sessionExpiry = parseInt(sessionExpiry);
|
|
97
|
-
keepAlive = parseInt(keepAlive)
|
|
98
|
-
|
|
99
|
-
let identity = {orgId:orgId, typeId: typeId, deviceId:deviceId};
|
|
100
|
-
let options = {
|
|
101
|
-
domain: domain,
|
|
102
|
-
logLevel: logLevel,
|
|
103
|
-
mqtt: {
|
|
104
|
-
port: port,
|
|
105
|
-
transport: transport,
|
|
106
|
-
cleanStart: (["True", "true", "1"].includes(cleanStart)),
|
|
107
|
-
sessionExpiry: sessionExpiry,
|
|
108
|
-
keepAlive: keepAlive,
|
|
109
|
-
sharedSubscription: (["True", "true", "1"].includes(sharedSubs)),
|
|
110
|
-
caFile: caFile,
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
let auth = null;
|
|
114
|
-
// Quickstart doesn't support auth, so ensure we only add this if it's defined
|
|
115
|
-
if (authToken != null) {
|
|
116
|
-
auth = {token: authToken};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return new DeviceConfig(identity, auth, options);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
static parseConfigFile(configFilePath) {
|
|
123
|
-
|
|
124
|
-
//Example Device Configuration File:
|
|
125
|
-
|
|
126
|
-
/*
|
|
127
|
-
identity:
|
|
128
|
-
orgId: org1id
|
|
129
|
-
typeId: raspberry-pi-3
|
|
130
|
-
deviceId: 00ef08ac05
|
|
131
|
-
auth:
|
|
132
|
-
token: Ab$76s)asj8_s5
|
|
133
|
-
options:
|
|
134
|
-
domain: internetofthings.ibmcloud.com
|
|
135
|
-
logLevel: error|warning|info|debug
|
|
136
|
-
mqtt:
|
|
137
|
-
port: 8883
|
|
138
|
-
transport: tcp
|
|
139
|
-
cleanStart: true
|
|
140
|
-
sessionExpiry: 3600
|
|
141
|
-
keepAlive: 60
|
|
142
|
-
caFile: /path/to/certificateAuthorityFile.pem
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
146
|
-
var data = YAML.parse(configFile);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if(!fs.existsSync(configFilePath)) {
|
|
150
|
-
throw new Error("File not found");
|
|
151
|
-
}else
|
|
152
|
-
{try {
|
|
153
|
-
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
154
|
-
var data = YAML.parse(configFile);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
throw new Error("Error reading device configuration file: " + err.code);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (("options" in data) & ("logLevel" in data["options"]))
|
|
162
|
-
{
|
|
163
|
-
var validLevels = ["error", "warning", "info", "debug"];
|
|
164
|
-
if (!(validLevels.includes(data["options"]["logLevel"])))
|
|
165
|
-
{
|
|
166
|
-
throw new Error("Optional setting options.logLevel must be one of error, warning, info, debug"
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
else
|
|
170
|
-
{
|
|
171
|
-
data["options"]["logLevel"] = log.GetLogger(data["options"]["logLevel"].toUpperCase());
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return new DeviceConfig(data['identity'],data['auth'],data['options'])
|
|
175
|
-
}
|
|
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
|
+
|
|
17
|
+
export default class DeviceConfig extends BaseConfig{
|
|
18
|
+
constructor(identity, auth, options) {
|
|
19
|
+
super(identity, auth, options);
|
|
20
|
+
|
|
21
|
+
//Validate the arguments
|
|
22
|
+
if (this.identity == null) {
|
|
23
|
+
throw new Error("Missing identity from configuration");
|
|
24
|
+
}
|
|
25
|
+
if (!("orgId" in this.identity) || this.identity.orgId == null) {
|
|
26
|
+
throw new Error("Missing identity.orgId from configuration");
|
|
27
|
+
}
|
|
28
|
+
if (!("typeId" in this.identity) || this.identity.typeId == null) {
|
|
29
|
+
throw new Error("Missing identity.typeId from configuration");
|
|
30
|
+
}
|
|
31
|
+
if (!("deviceId" in this.identity) || this.identity.deviceId == null) {
|
|
32
|
+
throw new Error("Missing identity.deviceId from configuration");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Authentication is not supported for quickstart
|
|
36
|
+
if (this.identity.orgId == "quickstart") {
|
|
37
|
+
if (this.auth != null) {
|
|
38
|
+
throw new Error("Quickstart service does not support device authentication");
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
if (this.auth == null) {
|
|
42
|
+
throw new Error("Missing auth from configuration");
|
|
43
|
+
}
|
|
44
|
+
if (!("token" in this.auth) || this.auth.token == null) {
|
|
45
|
+
throw new Error("Missing auth.token from configuration");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getOrgId() {
|
|
52
|
+
return this.identity.orgId;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getClientId() {
|
|
56
|
+
return "d:" + this.identity.orgId + ":" + this.identity.typeId + ":" + this.identity.deviceId;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getMqttUsername() {
|
|
60
|
+
return "use-token-auth";
|
|
61
|
+
}
|
|
62
|
+
getMqttPassword() {
|
|
63
|
+
return this.auth.token;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static parseEnvVars() {
|
|
67
|
+
|
|
68
|
+
//Identity
|
|
69
|
+
let orgId = process.env.WIOTP_IDENTITY_ORGID || null;
|
|
70
|
+
let typeId = process.env.WIOTP_IDENTITY_TYPEID || null;
|
|
71
|
+
let deviceId = process.env.WIOTP_IDENTITY_DEVICEID || null;
|
|
72
|
+
|
|
73
|
+
// Auth
|
|
74
|
+
let authToken = process.env.WIOTP_AUTH_TOKEN || null;
|
|
75
|
+
|
|
76
|
+
// Also support WIOTP_API_TOKEN usage
|
|
77
|
+
if (authToken == null) {
|
|
78
|
+
authToken = process.env.WIOTP_API_TOKEN || null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Options
|
|
82
|
+
let domain = process.env.WIOTP_OPTIONS_DOMAIN || null;
|
|
83
|
+
let logLevel = process.env.WIOTP_OPTIONS_LOGLEVEL || "info";
|
|
84
|
+
let port = process.env.WIOTP_OPTIONS_MQTT_PORT || null;
|
|
85
|
+
let transport = process.env.WIOTP_OPTIONS_MQTT_TRANSPORT || null;
|
|
86
|
+
let caFile = process.env.WIOTP_OPTIONS_MQTT_CAFILE || null;
|
|
87
|
+
let cleanStart = process.env.WIOTP_OPTIONS_MQTT_CLEANSTART || "true";
|
|
88
|
+
let sessionExpiry = process.env.WIOTP_OPTIONS_MQTT_SESSIONEXPIRY || 3600;
|
|
89
|
+
let keepAlive = process.env.WIOTP_OPTIONS_MQTT_KEEPALIVE || 60;
|
|
90
|
+
let sharedSubs = process.env.WIOTP_OPTIONS_MQTT_SHAREDSUBSCRIPTION || "false";
|
|
91
|
+
|
|
92
|
+
// String to int conversions
|
|
93
|
+
if (port != null) {
|
|
94
|
+
port = parseInt(port);
|
|
95
|
+
}
|
|
96
|
+
sessionExpiry = parseInt(sessionExpiry);
|
|
97
|
+
keepAlive = parseInt(keepAlive)
|
|
98
|
+
|
|
99
|
+
let identity = {orgId:orgId, typeId: typeId, deviceId:deviceId};
|
|
100
|
+
let options = {
|
|
101
|
+
domain: domain,
|
|
102
|
+
logLevel: logLevel,
|
|
103
|
+
mqtt: {
|
|
104
|
+
port: port,
|
|
105
|
+
transport: transport,
|
|
106
|
+
cleanStart: (["True", "true", "1"].includes(cleanStart)),
|
|
107
|
+
sessionExpiry: sessionExpiry,
|
|
108
|
+
keepAlive: keepAlive,
|
|
109
|
+
sharedSubscription: (["True", "true", "1"].includes(sharedSubs)),
|
|
110
|
+
caFile: caFile,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
let auth = null;
|
|
114
|
+
// Quickstart doesn't support auth, so ensure we only add this if it's defined
|
|
115
|
+
if (authToken != null) {
|
|
116
|
+
auth = {token: authToken};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return new DeviceConfig(identity, auth, options);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static parseConfigFile(configFilePath) {
|
|
123
|
+
|
|
124
|
+
//Example Device Configuration File:
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
identity:
|
|
128
|
+
orgId: org1id
|
|
129
|
+
typeId: raspberry-pi-3
|
|
130
|
+
deviceId: 00ef08ac05
|
|
131
|
+
auth:
|
|
132
|
+
token: Ab$76s)asj8_s5
|
|
133
|
+
options:
|
|
134
|
+
domain: internetofthings.ibmcloud.com
|
|
135
|
+
logLevel: error|warning|info|debug
|
|
136
|
+
mqtt:
|
|
137
|
+
port: 8883
|
|
138
|
+
transport: tcp
|
|
139
|
+
cleanStart: true
|
|
140
|
+
sessionExpiry: 3600
|
|
141
|
+
keepAlive: 60
|
|
142
|
+
caFile: /path/to/certificateAuthorityFile.pem
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
146
|
+
var data = YAML.parse(configFile);
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
if(!fs.existsSync(configFilePath)) {
|
|
150
|
+
throw new Error("File not found");
|
|
151
|
+
}else
|
|
152
|
+
{try {
|
|
153
|
+
const configFile = fs.readFileSync(configFilePath, 'utf8');
|
|
154
|
+
var data = YAML.parse(configFile);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
throw new Error("Error reading device configuration file: " + err.code);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
if (("options" in data) & ("logLevel" in data["options"]))
|
|
162
|
+
{
|
|
163
|
+
var validLevels = ["error", "warning", "info", "debug"];
|
|
164
|
+
if (!(validLevels.includes(data["options"]["logLevel"])))
|
|
165
|
+
{
|
|
166
|
+
throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug")
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
{
|
|
171
|
+
data["options"]["logLevel"] = log.GetLogger(data["options"]["logLevel"].toUpperCase());
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return new DeviceConfig(data['identity'],data['auth'],data['options'])
|
|
175
|
+
}
|
|
176
176
|
}
|
package/src/device/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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 DeviceClient } from './DeviceClient';
|
|
13
|
-
export { default as DeviceConfig } from './DeviceConfig';
|
|
14
|
-
|
|
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 DeviceClient } from './DeviceClient';
|
|
13
|
+
export { default as DeviceConfig } from './DeviceConfig';
|
|
14
|
+
|
|
@@ -1,114 +1,114 @@
|
|
|
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 { default as BaseClient } from '../BaseClient';
|
|
12
|
-
import { default as GatewayConfig } from './GatewayConfig';
|
|
13
|
-
|
|
14
|
-
const CMD_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/cmd\/(.+)\/fmt\/(.+)$/;
|
|
15
|
-
|
|
16
|
-
const util = require('util');
|
|
17
|
-
|
|
18
|
-
export default class GatewayClient extends BaseClient {
|
|
19
|
-
|
|
20
|
-
constructor(config){
|
|
21
|
-
if (!config instanceof GatewayConfig) {
|
|
22
|
-
throw new Error("Config must be an instance of GatewayConfig");
|
|
23
|
-
}
|
|
24
|
-
if (config.isQuickstart()) {
|
|
25
|
-
throw new Error('[GatewayClient:constructor] Quickstart not supported in Gateways');
|
|
26
|
-
}
|
|
27
|
-
super(config);
|
|
28
|
-
|
|
29
|
-
this.log.debug("[GatewayClient:constructor] GatewayClient initialized for " + config.getClientId());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
connect(){
|
|
34
|
-
super.connect();
|
|
35
|
-
|
|
36
|
-
this.mqtt.on('connect', () => {
|
|
37
|
-
// This gateway client implemention does not automatically subscribe to any commands!?
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
this.mqtt.on('message', (topic, payload) => {
|
|
41
|
-
this.log.debug("[GatewayClient:onMessage] Message received on topic : "+ topic + " with payload : "+ payload);
|
|
42
|
-
|
|
43
|
-
let match = CMD_RE.exec(topic);
|
|
44
|
-
if(match){
|
|
45
|
-
this.emit('command', match[1], match[2], match[3], match[4], payload, topic);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
publishEvent(eventId, format, payload, qos, callback){
|
|
52
|
-
return this._publishEvent(this.config.identity.typeId, this.config.identity.deviceId, eventId, format, payload, qos, callback);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
publishDeviceEvent(typeid, deviceId, eventid, format, payload, qos, callback){
|
|
57
|
-
return this._publishEvent(typeId, deviceId, eventid, format, payload, qos, callback);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
_publishEvent(typeId, deviceId, eventId, format, data, qos, callback){
|
|
62
|
-
let topic = util.format("iot-2/type/%s/id/%s/evt/%s/fmt/%s", typeId, deviceId, eventId, format);
|
|
63
|
-
qos = qos || 0;
|
|
64
|
-
this._publish(topic, data, qos, callback);
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
subscribeToDeviceCommands(typeId, deviceId, commandId, format, qos, callback){
|
|
70
|
-
typeId = typeId || '+';
|
|
71
|
-
deviceId = deviceId || '+';
|
|
72
|
-
commandId = commandid || '+';
|
|
73
|
-
format = format || '+';
|
|
74
|
-
qos = qos || 0;
|
|
75
|
-
|
|
76
|
-
let topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
77
|
-
this._subscribe(topic,qos, callback);
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
unsubscribeFromDeviceCommands(typeId, deviceId, commandId, format, callback){
|
|
83
|
-
typeId = typeId || '+';
|
|
84
|
-
deviceId = deviceId || '+';
|
|
85
|
-
commandId = commandId || '+';
|
|
86
|
-
format = format || '+';
|
|
87
|
-
|
|
88
|
-
let topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
89
|
-
this._unsubscribe(topic, callback);
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
subscribeToCommands(commandId, format, qos, callback){
|
|
95
|
-
commandId = commandId || '+';
|
|
96
|
-
format = format || '+';
|
|
97
|
-
qos = qos || 0;
|
|
98
|
-
|
|
99
|
-
let topic = "iot-2/type/" + this.config.identity.typeId + "/id/" + this.config.identity.deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
100
|
-
this._subscribe(topic, qos, callback);
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
unsubscribeFromCommands(commandId, format, callback){
|
|
106
|
-
commandId = commandId || '+';
|
|
107
|
-
format = format || '+';
|
|
108
|
-
|
|
109
|
-
let topic = "iot-2/type/" + this.config.identity.typeId + "/id/" + this.config.identity.deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
110
|
-
this._unsubscribe(topic, callback);
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
}
|
|
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 { default as BaseClient } from '../BaseClient';
|
|
12
|
+
import { default as GatewayConfig } from './GatewayConfig';
|
|
13
|
+
|
|
14
|
+
const CMD_RE = /^iot-2\/type\/(.+)\/id\/(.+)\/cmd\/(.+)\/fmt\/(.+)$/;
|
|
15
|
+
|
|
16
|
+
const util = require('util');
|
|
17
|
+
|
|
18
|
+
export default class GatewayClient extends BaseClient {
|
|
19
|
+
|
|
20
|
+
constructor(config){
|
|
21
|
+
if (!config instanceof GatewayConfig) {
|
|
22
|
+
throw new Error("Config must be an instance of GatewayConfig");
|
|
23
|
+
}
|
|
24
|
+
if (config.isQuickstart()) {
|
|
25
|
+
throw new Error('[GatewayClient:constructor] Quickstart not supported in Gateways');
|
|
26
|
+
}
|
|
27
|
+
super(config);
|
|
28
|
+
|
|
29
|
+
this.log.debug("[GatewayClient:constructor] GatewayClient initialized for " + config.getClientId());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
connect(){
|
|
34
|
+
super.connect();
|
|
35
|
+
|
|
36
|
+
this.mqtt.on('connect', () => {
|
|
37
|
+
// This gateway client implemention does not automatically subscribe to any commands!?
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this.mqtt.on('message', (topic, payload) => {
|
|
41
|
+
this.log.debug("[GatewayClient:onMessage] Message received on topic : "+ topic + " with payload : "+ payload);
|
|
42
|
+
|
|
43
|
+
let match = CMD_RE.exec(topic);
|
|
44
|
+
if(match){
|
|
45
|
+
this.emit('command', match[1], match[2], match[3], match[4], payload, topic);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
publishEvent(eventId, format, payload, qos, callback){
|
|
52
|
+
return this._publishEvent(this.config.identity.typeId, this.config.identity.deviceId, eventId, format, payload, qos, callback);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
publishDeviceEvent(typeid, deviceId, eventid, format, payload, qos, callback){
|
|
57
|
+
return this._publishEvent(typeId, deviceId, eventid, format, payload, qos, callback);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
_publishEvent(typeId, deviceId, eventId, format, data, qos, callback){
|
|
62
|
+
let topic = util.format("iot-2/type/%s/id/%s/evt/%s/fmt/%s", typeId, deviceId, eventId, format);
|
|
63
|
+
qos = qos || 0;
|
|
64
|
+
this._publish(topic, data, qos, callback);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
subscribeToDeviceCommands(typeId, deviceId, commandId, format, qos, callback){
|
|
70
|
+
typeId = typeId || '+';
|
|
71
|
+
deviceId = deviceId || '+';
|
|
72
|
+
commandId = commandid || '+';
|
|
73
|
+
format = format || '+';
|
|
74
|
+
qos = qos || 0;
|
|
75
|
+
|
|
76
|
+
let topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
77
|
+
this._subscribe(topic,qos, callback);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
unsubscribeFromDeviceCommands(typeId, deviceId, commandId, format, callback){
|
|
83
|
+
typeId = typeId || '+';
|
|
84
|
+
deviceId = deviceId || '+';
|
|
85
|
+
commandId = commandId || '+';
|
|
86
|
+
format = format || '+';
|
|
87
|
+
|
|
88
|
+
let topic = "iot-2/type/" + typeId + "/id/" + deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
89
|
+
this._unsubscribe(topic, callback);
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
subscribeToCommands(commandId, format, qos, callback){
|
|
95
|
+
commandId = commandId || '+';
|
|
96
|
+
format = format || '+';
|
|
97
|
+
qos = qos || 0;
|
|
98
|
+
|
|
99
|
+
let topic = "iot-2/type/" + this.config.identity.typeId + "/id/" + this.config.identity.deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
100
|
+
this._subscribe(topic, qos, callback);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
unsubscribeFromCommands(commandId, format, callback){
|
|
106
|
+
commandId = commandId || '+';
|
|
107
|
+
format = format || '+';
|
|
108
|
+
|
|
109
|
+
let topic = "iot-2/type/" + this.config.identity.typeId + "/id/" + this.config.identity.deviceId + "/cmd/"+ commandId + "/fmt/" + format;
|
|
110
|
+
this._unsubscribe(topic, callback);
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
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 DeviceConfig } from '../device/DeviceConfig';
|
|
12
|
-
|
|
13
|
-
export default class GatewayConfig extends DeviceConfig{
|
|
14
|
-
constructor(identity, auth, options) {
|
|
15
|
-
super(identity, auth, options);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
getClientId() {
|
|
19
|
-
return "g:" + this.identity.orgId + ":" + this.identity.typeId + ":" + this.identity.deviceId;
|
|
20
|
-
}
|
|
21
|
-
|
|
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 DeviceConfig } from '../device/DeviceConfig';
|
|
12
|
+
|
|
13
|
+
export default class GatewayConfig extends DeviceConfig{
|
|
14
|
+
constructor(identity, auth, options) {
|
|
15
|
+
super(identity, auth, options);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getClientId() {
|
|
19
|
+
return "g:" + this.identity.orgId + ":" + this.identity.typeId + ":" + this.identity.deviceId;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
22
|
};
|