Guest User

Untitled

a guest
Jan 22nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. exports.handler = function(request, context) {
  2. if (request.directive.header.namespace === 'Alexa.Discovery' && request.directive.header.name === 'Discover') {
  3. log("DEGUG:", "Discover request", JSON.stringify(request));
  4. handleDiscovery(request, context, "");
  5. }
  6. else if (request.directive.header.namespace === 'Alexa.PowerController') {
  7. if (request.directive.header.name === 'TurnOn' || request.directive.header.name === 'TurnOff') {
  8. log("DEBUG:", "TurnOn or TurnOff Request", JSON.stringify(request));
  9. handlePowerControl(request, context);
  10. }
  11. }
  12.  
  13. function handleDiscovery(request, context) {
  14. var payload = {
  15. "endpoints": [{
  16. "endpointId": "demo_id",
  17. "manufacturerName": "Smart Device Company",
  18. "friendlyName": "電気",
  19. "description": "smart switch",
  20. "displayCategories": ["SWITCH"],
  21. "cookie": {
  22. "key1": "arbitrary key/value pairs for skill to reference this endpoint.",
  23. "key2": "There can be multiple entries",
  24. "key3": "but they should only be used for reference purposes.",
  25. "key4": "This is not a suitable place to maintain current endpoint state."
  26. },
  27. "capabilities": [{
  28. "type": "AlexaInterface",
  29. "interface": "Alexa",
  30. "version": "3"
  31. },
  32. {
  33. "interface": "Alexa.PowerController",
  34. "version": "3",
  35. "type": "AlexaInterface",
  36. "properties": {
  37. "supported": [{
  38. "name": "powerState"
  39. }],
  40. "retrievable": true
  41. }
  42. }
  43. ]
  44. }]
  45. };
  46. var header = request.directive.header;
  47. header.name = "Discover.Response";
  48. log("DEBUG", "Discovery Response: ", JSON.stringify({ header: header, payload: payload }));
  49. context.succeed({ event: { header: header, payload: payload } });
  50. }
  51.  
  52. function log(message, message1, message2) {
  53. console.log(message + message1 + message2);
  54. }
  55.  
  56. function handlePowerControl(request, context) {
  57. // get device ID passed in during discovery
  58. var requestMethod = request.directive.header.name;
  59. // get user token pass in request
  60. var requestToken = request.directive.endpoint.scope.token;
  61. var powerResult;
  62.  
  63. if (requestMethod === "TurnOn") {
  64.  
  65. // Make the call to your device cloud for control
  66. // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
  67. powerResult = "ON";
  68. }
  69. else if (requestMethod === "TurnOff") {
  70. // Make the call to your device cloud for control and check for success
  71. // powerResult = stubControlFunctionToYourCloud(endpointId, token, request);
  72. powerResult = "OFF";
  73. }
  74.  
  75. var response = {
  76. "context": {
  77. "properties": [{
  78. "namespace": "Alexa.PowerController",
  79. "name": "powerState",
  80. "value": powerResult,
  81. "timeOfSample": "2017-02-03T16:20:50.52Z",
  82. "uncertaintyInMilliseconds": 500
  83. }]
  84. },
  85. "event": {
  86. "header": {
  87. "namespace": "Alexa",
  88. "name": "Response",
  89. "payloadVersion": "3",
  90. "messageId": "5f8a426e-01e4-4cc9-8b79-65f8bd0fd8a4",
  91. "correlationToken": "dFMb0z+PgpgdDmluhJ1LddFvSqZ/jCc8ptlAKulUj90jSqg=="
  92. },
  93. "payload": {}
  94. }
  95. };
  96.  
  97.  
  98. log("DEBUG", "Alexa.PowerController ", JSON.stringify(response));
  99. context.succeed(response);
  100. }
  101. };
Add Comment
Please, Sign In to add comment