-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathindex.ts
399 lines (339 loc) · 14.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { mqtt, iotshadow } from 'aws-iot-device-sdk-v2';
import { stringify } from 'querystring';
import readline from 'readline';
import {once} from "events";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const prompt = (query: string) => new Promise((resolve) => rl.question(query, resolve));
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
type Args = { [index: string]: any };
const yargs = require('yargs');
// The relative path is '../../util/cli_args' from here, but the compiled javascript file gets put one level
// deeper inside the 'dist' folder
const common_args = require('../../../util/cli_args');
var shadow_value: unknown;
var shadow_property: string;
var shadow_update_complete = false;
yargs.command('*', false, (yargs: any) => {
common_args.add_direct_connection_establishment_arguments(yargs);
common_args.add_shadow_arguments(yargs);
}, main).parse();
async function sub_to_shadow_update(shadow: iotshadow.IotShadowClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function updateAccepted(error?: iotshadow.IotShadowError, response?: iotshadow.model.UpdateShadowResponse) {
if (response) {
if (response.clientToken !== undefined) {
console.log("Succcessfully updated shadow for clientToken: " + response.clientToken + ".");
}
else {
console.log("Succcessfully updated shadow.");
}
if (response.state?.desired !== undefined) {
console.log("\t desired state: " + stringify(response.state.desired));
}
if (response.state?.reported !== undefined) {
console.log("\t reported state: " + stringify(response.state.reported));
}
}
if (error || !response) {
console.log("Updated shadow is missing the target property.");
}
resolve(true);
}
function updateRejected(error?: iotshadow.IotShadowError, response?: iotshadow.model.ErrorResponse) {
if (response) {
console.log("Update request was rejected.");
}
if (error) {
console.log("Error occurred..")
}
reject(error);
}
console.log("Subscribing to Update events..");
const updateShadowSubRequest: iotshadow.model.UpdateNamedShadowSubscriptionRequest = {
shadowName: argv.shadow_property,
thingName: argv.thing_name
};
await shadow.subscribeToUpdateShadowAccepted(
updateShadowSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => updateAccepted(error, response));
await shadow.subscribeToUpdateShadowRejected(
updateShadowSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => updateRejected(error, response));
resolve(true);
}
catch (error) {
reject(error);
}
});
}
async function sub_to_shadow_get(shadow: iotshadow.IotShadowClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function getAccepted(error?: iotshadow.IotShadowError, response?: iotshadow.model.GetShadowResponse) {
if (response?.state) {
if (response?.state.delta) {
const value = response.state.delta;
if (value) {
console.log("Shadow contains delta value '" + stringify(value) + "'.");
change_shadow_value(shadow, argv, value);
}
}
if (response?.state.reported) {
const value_any: any = response.state.reported;
if (value_any) {
let found_property = false;
for (var prop in value_any) {
if (prop === shadow_property) {
found_property = true;
console.log("Shadow contains '" + prop + "'. Reported value: '" + String(value_any[prop]) + "'.");
break;
}
}
if (found_property === false) {
console.log("Shadow does not contain '" + shadow_property + "' property.");
}
}
}
}
if (error || !response) {
console.log("Error occurred..");
}
shadow_update_complete = true;
resolve(true);
}
function getRejected(error?: iotshadow.IotShadowError, response?: iotshadow.model.ErrorResponse) {
if (response) {
console.log("In getRejected response.");
}
if (error) {
console.log("Error occurred..");
}
shadow_update_complete = true;
reject(error);
}
console.log("Subscribing to Get events..");
const getShadowSubRequest: iotshadow.model.GetShadowSubscriptionRequest = {
thingName: argv.thing_name
};
await shadow.subscribeToGetShadowAccepted(
getShadowSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => getAccepted(error, response));
await shadow.subscribeToGetShadowRejected(
getShadowSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => getRejected(error, response));
resolve(true);
}
catch (error) {
reject(error);
}
});
}
async function sub_to_shadow_delta(shadow: iotshadow.IotShadowClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function deltaEvent(error?: iotshadow.IotShadowError, response?: iotshadow.model.GetShadowResponse) {
console.log("\nReceived shadow delta event.");
if (response?.clientToken != null) {
console.log(" ClientToken: " + response.clientToken);
}
if (response?.state !== null) {
let value_any: any = response?.state;
if (value_any === null || value_any === undefined) {
console.log("Delta reports that '" + shadow_property + "' was deleted. Resetting defaults..");
let data_to_send: any = {};
data_to_send[shadow_property] = argv.shadow_value;
change_shadow_value(shadow, argv, data_to_send);
}
else {
if (value_any[shadow_property] !== undefined) {
if (value_any[shadow_property] !== shadow_value) {
console.log("Delta reports that desired value is '" + value_any[shadow_property] + "'. Changing local value..");
let data_to_send: any = {};
data_to_send[shadow_property] = value_any[shadow_property];
change_shadow_value(shadow, argv, data_to_send);
}
else {
console.log("Delta did not report a change in '" + shadow_property + "'.");
}
}
else {
console.log("Desired value not found in delta. Skipping..");
}
}
}
else {
console.log("Delta did not report a change in '" + shadow_property + "'.");
}
resolve(true);
}
console.log("Subscribing to Delta events..");
const deltaShadowSubRequest: iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest = {
thingName: argv.thing_name
};
await shadow.subscribeToShadowDeltaUpdatedEvents(
deltaShadowSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => deltaEvent(error, response));
resolve(true);
}
catch (error) {
reject(error);
}
});
}
async function get_current_shadow(shadow: iotshadow.IotShadowClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
const getShadow: iotshadow.model.GetShadowRequest = {
thingName: argv.thing_name
}
shadow_update_complete = false;
console.log("Requesting current shadow state..");
shadow.publishGetShadow(
getShadow,
mqtt.QoS.AtLeastOnce);
await get_current_shadow_update_wait();
resolve(true);
}
catch (error) {
reject(error);
}
});
}
async function get_current_shadow_update_wait() {
// Wait until shadow_update_complete is true, showing the result returned
return await new Promise(resolve => {
const interval = setInterval(() => {
if (shadow_update_complete == true) {
resolve(true);
clearInterval(interval);
};
}, 200);
});
}
function change_shadow_value(shadow: iotshadow.IotShadowClient, argv: Args, new_value?: object) {
return new Promise(async (resolve, reject) => {
try {
if (typeof new_value !== 'undefined') {
let new_value_any: any = new_value;
let skip_send = false;
if (new_value_any !== null) {
if (new_value_any[shadow_property] == shadow_value) {
skip_send = true;
}
}
if (skip_send == false) {
if (new_value_any === null) {
shadow_value = new_value_any;
}
else {
shadow_value = new_value_any[shadow_property];
}
console.log("Changed local shadow value to '" + shadow_value + "'.");
var updateShadow: iotshadow.model.UpdateShadowRequest = {
state: {
desired: new_value,
reported: new_value
},
thingName: argv.thing_name
};
await shadow.publishUpdateShadow(
updateShadow,
mqtt.QoS.AtLeastOnce)
console.log("Update request published.");
}
}
}
catch (error) {
console.log("Failed to publish update request.")
reject(error);
}
resolve(true)
});
}
async function main(argv: Args) {
common_args.apply_sample_arguments(argv);
shadow_property = argv.shadow_property;
var connection;
var client5;
var shadow;
console.log("Connecting...");
if (argv.mqtt5) { // Build the mqtt5 client
client5 = common_args.build_mqtt5_client_from_cli_args(argv);
shadow = iotshadow.IotShadowClient.newFromMqtt5Client(client5);
const connectionSuccess = once(client5, "connectionSuccess");
client5.start();
await connectionSuccess;
console.log("Connected with Mqtt5 Client...");
} else { // Build the mqtt3 based connection
connection = common_args.build_connection_from_cli_args(argv);
shadow = new iotshadow.IotShadowClient(connection);
await connection.connect();
console.log("Connected with Mqtt3 Client...");
}
try {
await sub_to_shadow_update(shadow, argv);
await sub_to_shadow_get(shadow, argv);
await sub_to_shadow_delta(shadow, argv);
await get_current_shadow(shadow, argv);
await sleep(500); // wait half a second
// Take console input when this sample is not running in CI
if (argv.is_ci == false) {
while (true) {
const userInput = await prompt("Enter desired value: ");
if (userInput === "quit") {
break;
}
else {
let data_to_send: any = {};
if (userInput == "clear_shadow") {
data_to_send = null;
}
else if (userInput == "null") {
data_to_send[shadow_property] = null;
}
else {
data_to_send[shadow_property] = userInput;
}
await change_shadow_value(shadow, argv, data_to_send);
await get_current_shadow(shadow, argv);
}
}
}
// If this is running in CI, then automatically update the shadow
else {
var messages_sent = 0;
while (messages_sent < 5) {
let data_to_send: any = {}
data_to_send[shadow_property] = "Shadow_Value_" + messages_sent.toString()
await change_shadow_value(shadow, argv, data_to_send);
await get_current_shadow(shadow, argv);
messages_sent += 1
}
}
} catch (error) {
console.log(error);
}
console.log("Disconnecting..");
if (connection) {
await connection.disconnect();
} else {
let stopped = once(client5, "stopped");
client5.stop();
await stopped;
client5.close();
}
// force node to wait a second before quitting to finish any promises
await sleep(1000);
console.log("Disconnected");
// Quit NodeJS
process.exit(0);
}