Guest User

NEAR index.js

a guest
Feb 18th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. /**
  2. * Particle.io account information
  3. * Replace these with your particle.io device id and access token
  4. */
  5. var deviceId = "2f005e000f51353433323633";
  6. var accessToken = "7ad2586b5be6f770677d275352f38ac69b54d184";
  7.  
  8. /**
  9. * Particle.io cloud function
  10. * The Arduino sketch specifies the REST URI when it makes
  11. * the particle.function()
  12. * For example, particle.function("myFunction", myFunction)
  13. * would be specified as: var cloudName = "myFunction"
  14. * You can leave this "myFunction", or change it later
  15. * if you change the sketch code.
  16. */
  17. var cloudName = "myFunction";
  18.  
  19. /**
  20. * Update skillName and invocationName to match the values
  21. * that you specify in the Alexa Skill Kit.
  22. * These are only used in responses from Alexa.
  23. */
  24. var skillName = "Particle"
  25. var invocationName = "Double Stuff Oreo";
  26.  
  27. /**
  28. * App ID for the skill
  29. * Update and use this if/when you publish your skill publicly.
  30. * It's ok to leave this undefined until then.
  31. */
  32. var APP_ID = "amzn1.ask.skill.b909de57-f34c-49d2-81b0-da41264b5571"; //replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";
  33.  
  34. /**
  35. * The AlexaSkill prototype and helper functions
  36. * Particle is a child of AlexaSkill.
  37. */
  38. var http = require('https');
  39. var AlexaSkill = require('./AlexaSkill');
  40. var Particle = function () {
  41. AlexaSkill.call(this, APP_ID);
  42. };
  43.  
  44. // Extend AlexaSkill
  45. Particle.prototype = Object.create(AlexaSkill.prototype);
  46. Particle.prototype.constructor = Particle;
  47.  
  48. Particle.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
  49. console.log("Near onSessionStarted requestId: " + sessionStartedRequest.requestId + ", sessionId: " + session.sessionId);
  50. };
  51.  
  52. Particle.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
  53. console.log("Near onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
  54. var speechOutput = "Welcome to the Near emergency response demo.";
  55.  
  56. response.ask(speechOutput);
  57. };
  58.  
  59. Particle.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
  60. console.log("Near onSessionEnded requestId: " + sessionEndedRequest.requestId + ", sessionId: " + session.sessionId);
  61. };
  62.  
  63. Particle.prototype.intentHandlers = {
  64.  
  65. // Register custom intent handlers.
  66. // This simple skill only uses one, but more can be added.
  67. "ParticleIntent": function (intent, session, response) {
  68.  
  69. var requestURI = "/v1/devices/" + deviceId + "/" + cloudName;
  70.  
  71. var commandSlot = intent.slots.command;
  72. var command1 = commandSlot ? intent.slots.command.value : "";
  73. var speakText = "";
  74.  
  75. var command = "";
  76.  
  77. switch (command1) {
  78. case "on": case "off":
  79. case "temperature":
  80. case "fallen over":
  81. case "accelerometer":
  82. case "motion":
  83. case "nine one one":
  84. case "stroke":
  85. case "a stroke":
  86. command = command1;
  87. break;
  88. case "maybe":
  89. command = "cmdMaybe";
  90. response.tell("Maybe has been entered!");
  91. }
  92.  
  93. console.log("Command = " + command + ", command1 = " + command1);
  94.  
  95.  
  96. // if(command1 == "on"){
  97. // speakText = "Temperature is 69°";
  98.  
  99. // command = "on";
  100. // console.log("!!! cmd1 is 'on'");
  101. // } else if (command1 == "off") {
  102. // command = "off";
  103. // console.log("!!! cmd1 is 'off'");
  104. // } else if (command1 == "accelerometer" || command1 == "fallen over") {
  105. // speakText = "Checking patient's accelerometer";
  106. // command = "cmdAccelerometer";
  107. // console.log("!!! cmd1 is 'accelerometer'");
  108. // } else if (command1 == "temperature") {
  109. // command = "cmdTemperature";
  110.  
  111. // }
  112.  
  113. //response.tellWithCard("Your command is " + command, "Octothorpe" + "Command: " + command + ", Command1: " + command1);
  114.  
  115. // Verify that a command was specified.
  116. // We can extend this to prompt the user,
  117. // but let's keep this simple for now.
  118. if(command.length > 0) {
  119.  
  120. if (command == "accelerometer") {
  121. speakText = "On it, checking accelerometer...Attention! Patient has fallen over! Do not move patient. If patient cannot get up, administer first aid. Help is on the way.";
  122. console.log("!!! Accelerometer command triggered!");
  123. } else if (command == "stroke" || command == "a stroke") {
  124. speakText = "Call nine one one immediately. Stay with victim, do not give medicine or food. Help is on the way.";
  125. } else if (command == "nine one one") {
  126. speakText = "We working on connecting with emergency services";
  127. }
  128. else {
  129.  
  130. var rand = Math.random();
  131. if (rand < 0.25) {
  132. speakText = "Of course";
  133. } else if (rand < 0.5) {
  134. speakText = "Right away";
  135. } else if (rand < 0.75) {
  136. speakText = "You got it";
  137. } else {
  138. speakText = "As you wish";
  139. }
  140. }
  141.  
  142. var postData = "args=" + command;
  143. console.log("!!! Post data = " + postData);
  144.  
  145. response.tellWithCard(speakText, "Octothorpe Particle Reading", "Working: " + command);
  146.  
  147. makeParticleRequest(requestURI, postData, function(resp) {
  148. var json = JSON.parse(resp);
  149. console.log(command + ": " + json.return_value);
  150. //response.tellWithCard(skillName, invocationName, "Thing is " + command );
  151.  
  152. response.tellWithCard(speakText, "Octothorpe Near System", "Working: " + command );
  153. });
  154. } else {
  155. response.tellWithCard("I'm sorry Dave, I don't understand your command", "Octothorpe Particle Reading", "Hal meets GlaDos");
  156. }
  157. }, // ParticleIntent
  158.  
  159. "AMAZON.HelpIntent": function (intent, session, response) {
  160. response.ask("You can tell " + invocationName + " to turn on or off.");
  161. } // HelpIntent
  162. };
  163.  
  164. // Create the handler that responds to the Alexa Request.
  165. exports.handler = function (event, context) {
  166. var particleSkill = new Particle();
  167. particleSkill.execute(event, context);
  168. };
  169.  
  170. function makeParticleRequest(requestURI, postData, callback){
  171. var options = {
  172. hostname: "api.particle.io",
  173. port: 443,
  174. path: requestURI,
  175. method: 'POST',
  176. headers: {
  177. 'Content-Type': 'application/x-www-form-urlencoded',
  178. 'Authorization': 'Bearer ' + accessToken,
  179. 'Accept': '*.*'
  180. }
  181. };
  182.  
  183. var req = http.request(options, function(res) {
  184. console.log('STATUS: ' + res.statusCode);
  185. console.log('HEADERS: ' + JSON.stringify(res.headers));
  186.  
  187. var body = "";
  188.  
  189. res.setEncoding('utf8');
  190. res.on('data', function (chunk) {
  191. console.log('BODY: ' + chunk);
  192. body += chunk;
  193. });
  194.  
  195. res.on('end', function () {
  196. callback(body);
  197. });
  198. });
  199.  
  200. req.on('error', function(e) {
  201. console.log('problem with request: ' + e.message);
  202. });
  203.  
  204. // write data to request body
  205. req.write(postData.toString());
  206. req.end();
  207. }
Advertisement
Add Comment
Please, Sign In to add comment