-
Notifications
You must be signed in to change notification settings - Fork 12
/
mockedSample.js
432 lines (394 loc) · 19.2 KB
/
mockedSample.js
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* IoT Hub Raspberry Pi NodeJS - Microsoft Sample Code - Copyright (c) 2017 - Licensed MIT
*/
const Client = require('azure-iot-device').Client;
const Message = require('azure-iot-device').Message;
const Protocol = require('azure-iot-device-mqtt').Mqtt;
var rgbLed = require('led');
var buttonA = require('buttonA');
var buttonB = require('buttonB');
var Screen = require('oled');
var HTS221 = require('hts221');
var LSM6DSL = require('lsm6dsl');
var traceEvent = require('traceEvent');
const connectionString = '[CONNECTION_STRING_PLACE_HOLDER]';
var sendingMessage = false;
var messageId = 0;
var client, sensor;
var iot_event = "{\"topic\":\"[TOPIC_PLACE_HOLDER]\"}";
var iot_event_heartbeat = "{\"topic\":\"\"}";
var msgHeader = "";
var msgBody = "";
var msgStart = 0;
var app_status;
var shake_progress;
var hb_interval_ms = SystemTickCounterRead();
var tweet_timeout_ms;
var idx = 0;
const RGB_LED_BRIGHTNESS = 256;
const HEARTBEAT_INTERVAL = 300000;
const PULL_TIMEOUT = 15000;
const SCROLL_OFFSET = 16;
const MSG_HEADER_SIZE = 20;
const MSG_BODY_SIZE = 200;
var smallTweet =
[
0x00, 0x00, 0x38, 0xf0, 0xe0, 0xe0, 0xc0, 0xc0, 0xf0, 0xfc, 0xfa, 0xfa, 0xd8, 0xf0, 0xe0, 0x80,
0x40, 0x00,
0x00, 0x04, 0x08, 0x18, 0x19, 0x3b, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x1f, 0x1f, 0x0f, 0x07, 0x01,
0x01, 0x00,
];
var shakeImag1 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x9C, 0xDE, 0xEF, 0x77, 0x76, 0xB0, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0xE0, 0xF0, 0x70, 0x70, 0x70, 0x78, 0x38, 0x38, 0x78, 0xF8, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x06, 0x87, 0xC3, 0xF9, 0xFE, 0x7F, 0x7F, 0x7F, 0x73, 0xE3, 0xE1, 0xE1, 0xC1, 0x81, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x18, 0x1C, 0x1C, 0x0C, 0x0F, 0x8F, 0x8F, 0x8E, 0xFC, 0xFC, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x07, 0x01, 0x00, 0x00, 0x70, 0xF0, 0xE0, 0xE0, 0x61, 0x61, 0x63, 0xE3, 0xFF, 0x7E, 0x79, 0xFF, 0xCF, 0x86, 0xC7, 0xC7, 0xC7, 0xC3, 0xC3, 0xE3, 0x61, 0x63, 0x73, 0x7F, 0xFE, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xF7, 0xFF, 0x3F, 0x18, 0x00, 0x00, 0x00, 0x07, 0x7F, 0xFF, 0xFC, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1F, 0x3F, 0xB1, 0xF1, 0x70, 0x30, 0x30, 0x18, 0x18, 0x18, 0x1C, 0x1F, 0xFF, 0xF3, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x38, 0x7C, 0xFE, 0xFE, 0xEF, 0xC7, 0x83, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xFF, 0xFF, 0xFF, 0xF0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE3, 0xE7, 0xE6, 0xEE, 0xEC, 0xEE, 0xE6, 0x76, 0x7F, 0x7F, 0xBF, 0xCF, 0xF0, 0x7C, 0x3C, 0xC0, 0xF8, 0xFC, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF8, 0xF0, 0xF0, 0xF8, 0x7C, 0x3F, 0x1F, 0x07, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x09, 0x1D, 0x0E, 0x0F, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
var shakeImag2 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x9C, 0xDE, 0xEF, 0x77, 0x76, 0xB0, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0xE0, 0xF0, 0x70, 0x70, 0x70, 0x78, 0x38, 0x38, 0x78, 0xF8, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x06, 0x87, 0xC3, 0xF9, 0xFE, 0x7F, 0x7F, 0x7F, 0x73, 0xE3, 0xE1, 0xE1, 0xC1, 0x81, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x18, 0x1C, 0x1C, 0x0C, 0x0F, 0x8F, 0x8F, 0x8E, 0xFC, 0xFC, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x07, 0x01, 0x00, 0x00, 0x70, 0xF0, 0xE0, 0xE0, 0x61, 0x61, 0x63, 0xE3, 0xFF, 0x7E, 0x79, 0xFF, 0xCF, 0x86, 0xC7, 0xC7, 0xC7, 0xC3, 0xC3, 0xE3, 0x61, 0x63, 0x73, 0x7F, 0xFE, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xF7, 0xFF, 0x3F, 0x18, 0x00, 0x00, 0x00, 0x07, 0x7F, 0xFF, 0xFC, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1F, 0x3F, 0xB1, 0xF1, 0x70, 0x30, 0x30, 0x18, 0x18, 0x18, 0x1C, 0x1F, 0xFF, 0xF3, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x7C, 0xFE, 0xFE, 0xEF, 0xC7, 0x83, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xFF, 0xFF, 0xFF, 0xF0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE3, 0xE7, 0xE6, 0xEE, 0xEC, 0xEE, 0xE6, 0x76, 0x7F, 0x7F, 0xBF, 0xCF, 0xF0, 0x7C, 0x3C, 0xC0, 0xF8, 0xFC, 0x38, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF8, 0xF0, 0xF0, 0xF8, 0x7C, 0x3F, 0x1F, 0x07, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x09, 0x1D, 0x0E, 0x0F, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
];
var shakeImag3 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x9C, 0xDE, 0xEF, 0x77, 0x76, 0xB0, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0xE0, 0xF0, 0x70, 0x70, 0x70, 0x78, 0x38, 0x38, 0x78, 0xF8, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x06, 0x87, 0xC3, 0xF9, 0xFE, 0x7F, 0x7F, 0x7F, 0x73, 0xE3, 0xE1, 0xE1, 0xC1, 0x81, 0x00, 0xC0, 0xE0, 0xF0, 0x38, 0x18, 0x1C, 0x1C, 0x0C, 0x0F, 0x8F, 0x8F, 0x8E, 0xFC, 0xFC, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x07, 0x01, 0x00, 0x00, 0x70, 0xF0, 0xE0, 0xE0, 0x61, 0x61, 0x63, 0xE3, 0xFF, 0x7E, 0x79, 0xFF, 0xCF, 0x86, 0xC7, 0xC7, 0xC7, 0xC3, 0xC3, 0xE3, 0x61, 0x63, 0x73, 0x7F, 0xFE, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xF7, 0xFF, 0x3F, 0x18, 0x00, 0x00, 0x00, 0x07, 0x7F, 0xFF, 0xFC, 0xF0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1F, 0x3F, 0xB1, 0xF1, 0x70, 0x30, 0x30, 0x18, 0x18, 0x18, 0x1C, 0x1F, 0xFF, 0xF3, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x7C, 0xFE, 0xFE, 0xEF, 0xC7, 0x83, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xFF, 0xFF, 0xFF, 0xF0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE3, 0xE7, 0xE6, 0xEE, 0xEC, 0xEE, 0xE6, 0x76, 0x7F, 0x7F, 0xBF, 0xCF, 0xF0, 0x7C, 0x3C, 0xC0, 0xF8, 0xFC, 0x38,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF8, 0xF0, 0xF0, 0xF8, 0x7C, 0x3F, 0x1F, 0x07, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x09, 0x1D, 0x0E, 0x0F, 0x07, 0x03, 0x00, 0x00,
];
var imgCheckbox1 =
[
0x00, 0x00, 0xfc, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3f, 0x00, 0x00
];
var imgCheckbox2 =
[
0x00, 0x00, 0xfc, 0x04, 0x84, 0x84, 0x04, 0x04, 0x84, 0x84, 0x44, 0x24, 0x04, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x20, 0x20, 0x21, 0x23, 0x23, 0x21, 0x20, 0x20, 0x20, 0x20, 0x3f, 0x00, 0x00
];
var bigCry =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfc,
0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xe0, 0xf8, 0xfe, 0xff, 0xff, 0xe7, 0xc3, 0x81, 0x81, 0xc3, 0xe7, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xe7, 0xc3, 0x81, 0x81, 0xc3, 0xe7, 0xff, 0xff, 0xfe, 0xf8, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x07, 0x1f, 0x7f, 0xff, 0xff, 0xdf, 0x8f, 0x87, 0xc3, 0xe3, 0xf1, 0xf1, 0xf8, 0xf8,
0xf8, 0xf8, 0xf1, 0xf1, 0xe3, 0xc3, 0x87, 0x8f, 0xdf, 0xff, 0xff, 0x7f, 0x1f, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x0f, 0x1f, 0x1f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x1f, 0x1f, 0x0f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
var newTweet =
[
0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0xf0, 0xf0, 0xf8, 0xdc, 0xcc, 0xcc, 0xcc, 0xc0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x1f, 0xff, 0xff, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf0, 0xf0, 0xf8,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xf3, 0xff, 0xff, 0xfe, 0xfc, 0xe0,
0xe0, 0x60, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf3, 0xf3, 0xcf, 0xcf,
0xcf, 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x3f, 0x3f, 0x3f, 0x3f, 0xcf, 0xcf, 0xcf, 0xcf, 0xf3, 0xf3, 0x00, 0x00,
0x00, 0x00, 0x30, 0x70, 0xe0, 0xc0, 0xc0, 0xc1, 0xc3, 0xc7, 0xcf, 0xef, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x3f, 0x07,
0x03, 0x07, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xf3, 0xf3, 0xfc, 0xfc, 0xfe, 0xfe,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x07, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x07, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00
];
var listShake = [
shakeImag1, shakeImag2, shakeImag3, shakeImag1
];
function SystemTickCounterRead() {
return +new Date();
}
function DrawAppTitle(text) {
let sz = " " + text.substring(0, 32);
Screen.print(0, sz);
Screen.draw(0, 0, 18, 2, smallTweet);
}
function DrawShakeAnimation() {
idx = (idx + 1) % 4;
Screen.draw(35, 2, 35 + 54, 8, listShake[idx]);
}
function DrawCheckBox(line, col, status) {
if (status == 0) {
Screen.draw(col, line * 2, col + 16, line * 2 + 2, imgCheckbox1);
}
else {
Screen.draw(col, line * 2, col + 16, line * 2 + 2, imgCheckbox2);
}
}
function DrawTweetImage(line, col, status) {
if (status == 0) {
Screen.draw(col + 20, line * 2, col + 52, line * 2 + 4, bigCry);
}
else {
Screen.draw(col, line * 2, col + 76, line * 2 + 4, newTweet);
}
}
async function ShowShakeProgress() {
if (shake_progress == 1) {
DrawAppTitle("Processing...");
Screen.print(1, " DevKit");
Screen.print(2, " Azure");
Screen.print(3, " Twitter");
}
DrawCheckBox(1, 0, (shake_progress >= 1) ? 1 : 0);
DrawCheckBox(2, 0, (shake_progress >= 2) ? 1 : 0);
DrawCheckBox(3, 0, (shake_progress == 4) ? 1 : 0);
await delay(500);
}
function NoTweets() {
Screen.clean();
DrawAppTitle("No tweets...");
Screen.print(3, "Press A to Shake!");
DrawTweetImage(1, 20, 0);
// Turn off the RGB LED
rgbLed.setColor(0, 0, 0);
// Switch back to idle mode
app_status = 0;
}
function HeartBeat() {
let now = SystemTickCounterRead();
if (now - hb_interval_ms < HEARTBEAT_INTERVAL) {
return;
}
console.log(">>Heartbeat<<");
hb_interval_ms = SystemTickCounterRead();
// todo add user led
// var msgObj = new Message(iot_event);
// client.sendEvent(new Message(iot_event_heartbeat), async function (err) {
// if (err) {
// console.error('Failed to send message to Azure IoT Hub');
// } else {
// hb_interval_ms = SystemTickCounterRead();
// }
// });
}
async function blinkLED() {
rgbLed.turnOff();
rgbLed.setColor(66, 229, 244);
await delay(500);
rgbLed.turnOff();
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function min(a, b) {
return a < b ? a : b;
}
function ParseTweet(tweet, lenTweet) {
if (lenTweet == 0) {
msgHeader = "";
msgBody = "";
return false;
}
var message = tweet.getData().toString('utf-8');
var splitIndex = message.indexOf("\n");
if (splitIndex == -1) {
msgHeader = message;
} else {
msgHeader = message.substring(0, splitIndex);
msgBody = message.substring(splitIndex + 1);
}
msgHeader = msgHeader.substring(0, MSG_HEADER_SIZE);
msgBody = msgBody.substring(0, MSG_BODY_SIZE);
console.log(msgHeader);
if (msgHeader === "No new tweet.") {
// Not a tweet from Twitter, there must be something wrong on the Service side.
msgHeader = "";
msgBody = "";
return false;
}
else {
console.log(msgBody);
return true;
}
}
async function TwitterMessageCallback(tweet, lenTweet) {
if (app_status != 2 || !tweet) {
// Return if not under do work mode or the tweet is empty.
return;
}
await delay(3000);
if (ParseTweet(tweet, lenTweet)) {
// The tweet message is OK
shake_progress = 4;
}
else {
// Got message from Azure, but not a tweet from Twitter.
// There must be something wrong on the Service side.
shake_progress = 3;
}
}
async function setup() {
app_status = 0;
shake_progress = 0;
DrawAppTitle("IoT DevKit");
Screen.print(2, "Connecting...");
Screen.print(3, " > WiFi");
await delay(1500);
// todo turn on wifi led
Screen.print(3, " > LEDs");
rgbLed.turnOff();
await delay(100);
Screen.print(3, " > IoT Hub");
await delay(1500);
rgbLed.setColor(RGB_LED_BRIGHTNESS, 0, 0);
// TODO: send telemetry data
client = Client.fromConnectionString(connectionString, Protocol);
client.open(function (err) {
if (err) {
console.error('[IoT hub Client] Connect error: ' + err.message);
return;
}
client.on('message', TwitterMessageCallback);
sendingMessage = true;
hb_interval_ms = -(HEARTBEAT_INTERVAL); // Trigger heart beat immediately
HeartBeat();
rgbLed.turnOff();
// todo turn on azure led
Screen.print(1, "192.168.1.1");
Screen.print(2, "Press A to Shake!");
Screen.print(3, " ");
});
}
async function DoIdle() {
if (buttonA.getSwitch()) {
Screen.clean();
app_status = 1;
shake_progress = 0;
DrawAppTitle("Shake Shake!");
rgbLed.setColor(0, RGB_LED_BRIGHTNESS, 0);
LSM6DSL.clearDistance();
} else if (buttonB.getSwitch()) {
ScrollTweet();
}
HeartBeat();
}
async function DoShake() {
if (LSM6DSL.getDistance() > 600) {
app_status = 2;
shake_progress = 1;
// todo user led light on
rgbLed.setColor(RGB_LED_BRIGHTNESS, 0, 0);
ShowShakeProgress();
traceEvent('shake-success');
await delay(1000);
var msgObj = new Message(iot_event);
await delay(2000);
let promise = new Promise((resolve, reject) => {
client.sendEvent(msgObj, function (err) {
if (!err) {
if (shake_progress < 2) {
// Because the tweet may return quickly and the TwitterMessageCallback be executed before run to here,
// So check the shake_progress to avoid set the wrong value.
// IoT hub has got the message
shake_progress = 2;
}
// Update the screen
ShowShakeProgress();
// Start retrieving tweet timeout clock
tweet_timeout_ms = SystemTickCounterRead();
resolve();
}
else {
reject();
NoTweets();
}
// todo user led off
});
});
await promise;
} else {
DrawShakeAnimation();
}
}
async function DoWork() {
if (SystemTickCounterRead() - tweet_timeout_ms >= PULL_TIMEOUT) {
// Timeout
NoTweets();
}
if (shake_progress > 2) {
// Got the tweet message
// Update the progress
ShowShakeProgress();
if (shake_progress == 4) {
Screen.clean();
// Got the tweet
// Show the action UI and let user to choose read or shake again
DrawAppTitle("New tweet!");
Screen.print(3, "Press B to read!");
DrawTweetImage(1, 20, 1);
// Prepare for reading and scrolling
msgStart = -SCROLL_OFFSET;
// Set RGB LED to blue, means for reading
rgbLed.setColor(0, 0, RGB_LED_BRIGHTNESS);
// Switch back to idle mode
app_status = 0;
// Log shake succeed message
// LogShakeResult("ShakeShakeSucceed");
}
else {
// No tweet
NoTweets();
}
}
}
async function ScrollTweet() {
if (msgBody != "") {
if (msgStart < 0) {
// First time
DrawAppTitle(msgHeader);
msgStart = 0;
}
else {
msgStart += SCROLL_OFFSET;
if (msgStart >= msgBody.length) {
msgStart = 0;
}
}
// Clean the msg screen
Screen.print(1, " ");
Screen.print(2, " ");
Screen.print(3, " ");
// Update it
Screen.print(1, msgBody.substring(msgStart), true);
await delay(300);
}
}
async function loop() {
switch (app_status) {
case 0:
await DoIdle();
break;
case 1:
await DoShake();
break;
case 2:
await DoWork();
break;
}
await delay(100);
}
async function run() {
await setup();
while (true) {
await loop();
}
}
run();