-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
815 lines (591 loc) · 19.1 KB
/
index.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/***********
namespaces
********* */
const NAMESPACE_ALEXA = "Alexa";
const NAMESPACE_DISCOVERY = "Alexa.Discovery";
const NAMESPACE_POWER_CONTROL = "Alexa.PowerController";
const NAMESPACE_POWER_LEVEL_CONTROL = "Alexa.PowerLevelController";
const NAMESPACE_BRIGHTNESS_CONTROL = "Alexa.BrightnessController";
const NAMESPACE_COLOR_CONTROL = "Alexa.ColorController";
const NAMESPACE_COLOR_TEMPERATURE_CONTROL = "Alexa.ColorTemperatureController";
/***********
names
********* */
/* request names */
/* discovery */
const NAME_REQUEST_DISCOVER = "Discover";
/* control */
// power
const NAME_REQUEST_TURN_ON = "TurnOn";
const NAME_REQUEST_TURN_OFF = "TurnOff";
// power Level
const NAME_REQUEST_ADJUST_POWER_LEVEL = "AdjustPowerLevel";
const NAME_REQUEST_SET_POWER_LEVEL = "SetPowerLevel";
// brightness
const NAME_REQUEST_ADJUST_BRIGHTNESS = "AdjustBrightness";
const NAME_REQUEST_SET_BRIGHTNESS = "SetBrightness";
// color
const NAME_REQUEST_SET_COLOR = "SetColor";
// color temperature
const NAME_REQUEST_DECREASE_COLOR_TEMPERATURE = "DecreaseColorTemperature";
const NAME_REQUEST_INCREASE_COLOR_TEMPERATURE = "IncreaseColorTemperature";
const NAME_REQUEST_SET_COLOR_TEMPERATURE = "SetColorTemperature";
/* report */
const NAME_REQUEST_REPORT = "ReportState";
/* response names */
/* event */
// default
const NAME_RESPONSE = "Response";
// discovery
const NAME_RESPONSE_DISCOVER = "Discover.Response";
// error
const NAME_RESPONSE_ERROR = "ErrorResponse";
const NAME_ERROR_UNSUPPORTED_OPERATION = "UnsupportedOperationError";
const NAME_ERROR_UNEXPECTED_INFO = "UnexpectedInformationReceivedError";
/* properties */
// power
const NAME_RESPONSE_POWER = "powerState";
// power level
const NAME_RESPONSE_POWER_LEVEL = "SetPowerLevel";
// brightness
const NAME_RESPONSE_BRIGHTNESS = "brightness";
// color
const NAME_RESPONSE_COLOR = "color";
// color temperature
const NAME_RESPONSE_COLOR_TEMPERATURE = "colorTemperatureInKelvin";
/* report */
const NAME_RESPONSE_STATE_REPORT = "StateReport";
const NAME_RESPONSE_STATE_CHANGE_REPORT = "ChangeReport";
/* parameters */
// alexa smart light
const ALEXA_SL_POWER_ON = "ON";
const ALEXA_SL_POWER_OFF = "OFF";
// system light API
const SL_API_POWER_ON = "on";
const SL_API_POWER_OFF = "off";
const DEFAULT_POWER_LEVEL = 1000;
/***********
version
********* */
const PAYLOAD_VERSION = "3";
/***********
modules
********* */
const request = require("request");
const config = require("config.json")("./config/config.json");
const async = require("async");
// Temp Light's gateway IP address
const BASE_URL = config.sl.gw;
var requestedNamespace = "";
var requestedName = "";
// entry
exports.handler = function(event, context, callback){
console.log("handler");
log("Received Directive", event);
requestedNamespace = event.directive.header.namespace;
requestedName = event.directive.header.name;
try {
switch(requestedNamespace){
case NAMESPACE_ALEXA:
handleReportingState(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_DISCOVERY:
handleDiscovery(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_POWER_CONTROL:
handlePowerControl(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_POWER_LEVEL_CONTROL:
handlePowerLevelControl(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_BRIGHTNESS_CONTROL:
handleBrightnessControl(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_COLOR_CONTROL:
handleColorControl(event, function(error, directive){
callback(null, directive);
});
break;
case NAMESPACE_COLOR_TEMPERATURE_CONTROL:
handleColorTemperatureControl(event, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported namespace: " + requestedNamespace);
handleUnexpectedInfo(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
} catch(error){
log("Error", error);
}// try-catch
};// exports.handler
function handleReportingState(event, callback){
console.log("handleReportingState");
switch(requestedName){
case NAME_REQUEST_REPORT :
handleStateChangeReport(event, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported operation" + requestedName);
handleUnsupportedOperation(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
}// handleReportingState
function handleStateContextObject(event, callback) {
console.log("handleStateContextObject");
var response = require("./response_templates/report_state/context_object.json");
callback(null, response);
}// handleStateContextObject
function handleStateChangeReport(event, callback){
console.log("handleStateChangeReport");
var response = require("./response_templates/report_state/change_report.json");
callback(null, response);
}// handleStateChangeReport
function handleDiscovery(event, callback){
console.log("handleDiscovery");
async.parallel({
header: function(callback){
createHeader(NAMESPACE_DISCOVERY, NAME_RESPONSE_DISCOVER, null, function(error, header){
callback(null, header);
});
},
payload: function(callback){
createEndpoints(function(error, endpoints){
var payload = {};
payload.endpoints = endpoints;
callback(null, payload);
});
}
}, function(error, results){
const header = results.header;
const payload = results.payload;
async.waterfall([
function(callback){
createEvent(header, null, payload, function(error, event){
callback(null, event);
});
},
function(event, callback){
createDirective(null, event, function(error, directive){
callback(null, directive);
});
}
], function(error, result){
callback(null, result);
});
});
}// handleDiscovery
function handlePowerControl(event, callback){
console.log("handlePowerControl");
switch(requestedName){
case NAME_REQUEST_TURN_ON :
handlePower(event, SL_API_POWER_ON, function(error, directive){
callback(null, directive);
});
break;
case NAME_REQUEST_TURN_OFF :
handlePower(event, SL_API_POWER_OFF, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported operation" + requestedName);
handleUnsupportedOperation(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
}// handlePowerControl
function handlePower(event, onoff, callback){
console.log("handlePowerControlTurnOn");
// make query
var deviceId = event.directive.endpoint.endpointId;
const lightUrl = BASE_URL + "/device/" + deviceId + "/light";
var level = DEFAULT_POWER_LEVEL;
var body = {};
body.onoff = onoff;
body.level = level;
var data = {
url: lightUrl,
form: body
}
// request gateway
request.put(data, function(error, httpResponse, body){
// Make Alexa response
makeControlResponse(event, NAME_RESPONSE_POWER, onoff, function(error, response){
callback(null, response);
});
});
}// handlePower
function handlePowerLevelControl(event, callback){
console.log("handlePowerLevelControl");
switch(requestedName){
case NAME_REQUEST_ADJUST_POWER_LEVEL :
adjustPowerLevel(event, function(error, directive){
callback(null, directive);
});
break;
case NAME_REQUEST_SET_POWER_LEVEL :
setPowerLevel(event, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported operation" + requestedName);
handleUnsupportedOperation(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
}// handlePowerLevelControl
function adjustPowerLevel(event, callback){
console.log("adjustPowerLevel");
var directive = require("./response_templates/power_level/power_level.json");
callback(null, directive);
}// handlePowerLevelControl
function setPowerLevel(event, callback){
console.log("setPowerLevel");
// Request query
const deviceId = event.directive.endpoint.endpointId;
const lightUrl = BASE_URL + "/device/" + deviceId + "/light";
const onoff = SL_API_POWER_ON;
const level = event.directive.payload.powerLevel;
var body = {};
body.onoff = onoff;
body.level = level;
var data = {
url: lightUrl,
form: body
}
// request gateway
request.put(data, function(error, httpResponse, body){
// Make Alexa response
makeControlResponse(event, NAME_RESPONSE_POWER_LEVEL, level, function(error, response){
callback(null, response);
});
});
}// adjustPowerLevel
function handleBrightnessControl(event, callback){
console.log("handleBrightnessControl");
switch(requestedName){
case NAME_REQUEST_ADJUST_BRIGHTNESS :
adjustBrightness(event, function(error, directive){
callback(null, directive);
});
break;
case NAME_REQUEST_SET_BRIGHTNESS :
setBrightness(event, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported operation" + requestedName);
handleUnsupportedOperation(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
}// handleBrightnessControl
function adjustBrightness(event, callback){
console.log("adjustBrightness");
var response = require("./response_templates/brightness/brightness.json");
callback(null, response);
}// adjustBrightness
function setBrightness(event, callback){
console.log("setBrightness");
// Request query
const deviceId = event.directive.endpoint.endpointId;
const lightUrl = BASE_URL + "/device/" + deviceId + "/light";
const onoff = SL_API_POWER_ON;
const level = DEFAULT_POWER_LEVEL;
const brightness = event.directive.payload.brightness;
var body = {};
body.onoff = onoff;
body.level = level;
// brightness
body.brightness = brightness;
var data = {
url: lightUrl,
form: body
}
// request gateway
request.put(data, function(error, httpResponse, body){
// Make Alexa response
makeControlResponse(event, NAME_RESPONSE_BRIGHTNESS, brightness, function(error, response){
callback(null, response);
});
});
}// setBrightness
function handleColorControl(event, callback){
console.log("handleColorControl");
// Request query
const deviceId = event.directive.endpoint.endpointId;
const lightUrl = BASE_URL + "/device/" + deviceId + "/light";
const onoff = SL_API_POWER_ON;
const level = DEFAULT_POWER_LEVEL;
const color = event.directive.payload.color;
var body = {};
body.onoff = onoff;
body.level = level;
// color
body.hue = color.hue;
body.saturation = color.saturation;
body.brightness = color.brightness;
var data = {
url: lightUrl,
form: body
}
// request gateway
request.put(data, function(error, httpResponse, body){
// Make Alexa response
makeControlResponse(event, NAME_RESPONSE_COLOR, color, function(error, response){
callback(null, response);
});
});
}// handleColorControl
function handleColorTemperatureControl(event, callback){
console.log("handleColorTemperatureControl");
switch(requestedName){
case NAME_REQUEST_DECREASE_COLOR_TEMPERATURE :
decreaseColorTemperature(event, function(error, directive){
callback(null, directive);
});
break;
case NAME_REQUEST_INCREASE_COLOR_TEMPERATURE :
increaseColorTemperature(event, function(error, directive){
callback(null, directive);
});
break;
case NAME_REQUEST_SET_COLOR_TEMPERATURE :
setColorTemperature(event, function(error, directive){
callback(null, directive);
});
break;
default:
log("Error", "Unsupported operation" + requestedName);
handleUnsupportedOperation(event, function(error, directive){
callback(null, directive);
});
break;
}// switch
}// handleColorTemperatureControl
function decreaseColorTemperature(event, callback){
console.log("decreaseColorTemperature");
// TODO modify temporary response
var response = require("./response_templates/color_temperature/color_temperature.json");
callback(null, response);
}// decreaseColorTemperature
function increaseColorTemperature(event, callback){
console.log("increaseColorTemperature");
// TODO modify temporary response
var response = require("./response_templates/color_temperature/color_temperature.json");
callback(null, response);
}// increaseColorTemperature
function setColorTemperature(event, callback){
console.log("setColorTemperature");
// Request query
const deviceId = event.directive.endpoint.endpointId;
const lightUrl = BASE_URL + "/device/" + deviceId + "/light";
const onoff = SL_API_POWER_ON;
const level = DEFAULT_POWER_LEVEL;
const colorTemperatureInKelvin = event.directive.payload.colorTemperatureInKelvin;
var body = {};
body.onoff = onoff;
body.level = level;
// color
body.colorTemp = colorTemperatureInKelvin;
var data = {
url: lightUrl,
form: body
}
// request gateway
request.put(data, function(error, httpResponse, body){
// Make Alexa response
makeControlResponse(event, NAME_RESPONSE_COLOR_TEMPERATURE, colorTemperatureInKelvin, function(error, response){
callback(null, response);
});
});
}// setColorTemperature
function handleUnsupportedOperation(event, callback){
console.log("handleUnsupportedOperation");
const correlationToken = event.directive.header.correlationToken;
var endpoint = {};
var payload = {};
var context = {};
async.waterfall([
function(callback){
createHeader(NAMESPACE_POWER_CONTROL, NAME_ERROR_UNSUPPORTED_OPERATION, correlationToken, function(error, header){
callback(null, header);
});
},
function(header, callback){
createEvent(header, endpoint, payload, function(error, event){
callback(null, event);
});
},
function(event, callback){
createDirective(context, event, function(error, directive){
callback(null, directive);
});
}
], function(error, result){
callback(null, result);
});
}// handleUnsupportedOperation
function handleUnexpectedInfo(event, callback){
console.log("handleUnexpectedInfo");
const correlationToken = event.directive.header.correlationToken;
var payload = {
"faultingParameter" : requestedNamespace
};
var context = {};
async.waterfall([
function(callback){
createHeader(NAMESPACE_POWER_CONTROL, NAME_ERROR_UNEXPECTED_INFO, correlationToken, function(error, header){
callback(null, header);
});
},
function(header, callback){
createEvent(header, null, payload, function(error, event){
callback(null, event);
});
},
function(event, callback){
createDirective(context, event, function(error, directive){
callback(null, directive);
});
}
], function(error, result){
callback(null, result);
});
}// handleUnexpectedInfo
// support functions
var createMessageId = function(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c){
var r =(d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return(c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}// createMessageId
function createEndpoints(callback){
var endpoints = [];
// Test data
// Virtual Devices
const endpoint1 = require("./endpoint_templates/endpoint1.json");
const endpoint2 = require("./endpoint_templates/endpoint2.json");
const endpoint3 = require("./endpoint_templates/endpoint3.json");
const endpoint4 = require("./endpoint_templates/endpoint4.json");
const endpoint5 = require("./endpoint_templates/endpoint5.json");
const endpoint6 = require("./endpoint_templates/endpoint6.json");
const endpoint7 = require("./endpoint_templates/endpoint7.json");
endpoints.push(endpoint1, endpoint2, endpoint3, endpoint4, endpoint5, endpoint6, endpoint7);
//log("endpoints :", endpoints);
callback(null, endpoints);
}// createEndpoints
function createContext(event, name, value, callback){
var context = {};
var propertyArray = [];
var propertyObject = {};
propertyObject.namespace = requestedNamespace;
propertyObject.name = name;
propertyObject.value = value;
propertyObject.timeOfSample = new Date().toJSON();
propertyObject.uncertaintyInMilliseconds = 500;
propertyArray.push(propertyObject);
context.properties = propertyArray;
//log("context :", context);
callback(null, context);
}// createContext
function createHeader(namespace, name, correlationToken, callback){
var header = {
"messageId": createMessageId(),
"namespace": namespace,
"name": name,
"payloadVersion": PAYLOAD_VERSION
};
if(namespace != NAMESPACE_DISCOVERY){
header.correlationToken = correlationToken;
}
//log("header", header);
callback(null, header);
}// createHeader
function createEvent(header, endpoint, payload, callback){
var event = {};
event.header = header;
event.payload = payload;
if(endpoint === null){
}else{
event.endpoint = endpoint;
}
//log("event :", event);
callback(null, event);
}// createEvent
function createDirective(context, event, callback){
var directive = {};
directive.event = event;
if(context === null){
}else{
directive.context = context;
}
log("directive :", directive);
callback(null, directive);
}// createDirective
function makeControlResponse(event, responseName, value, callback){
const correlationToken = event.directive.header.correlationToken;
const endpoint = event.directive.endpoint;
var payload = {};
// Request query
const deviceId = endpoint.endpointId;
// Make Alexa response
async.parallel({
header: function(callback){
createHeader(NAMESPACE_ALEXA, NAME_RESPONSE, correlationToken, function(error, header){
callback(null, header);
});
},
context: function(callback){
createContext(event, responseName, value, function(error, context){
callback(null, context);
});
}
}, function(error, results){
const header = results.header;
const context = results.context;
async.waterfall([
function(callback){
createEvent(header, endpoint, payload, function(error, event){
callback(null, event);
});
},
function(event, callback){
createDirective(context, event, function(error, directive){
callback(null, directive);
});
}
], function(error, result){
callback(null, result);
});
});
}// makeControlResponse
var log = function(title, msg){
console.log('**** ' + title + ': ' + JSON.stringify(msg));
}// log