Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Particle.io account information
- * Replace these with your particle.io device id and access token
- */
- var deviceId = "2f005e000f51353433323633";
- var accessToken = "7ad2586b5be6f770677d275352f38ac69b54d184";
- /**
- * Particle.io cloud function
- * The Arduino sketch specifies the REST URI when it makes
- * the particle.function()
- * For example, particle.function("myFunction", myFunction)
- * would be specified as: var cloudName = "myFunction"
- * You can leave this "myFunction", or change it later
- * if you change the sketch code.
- */
- var cloudName = "myFunction";
- /**
- * Update skillName and invocationName to match the values
- * that you specify in the Alexa Skill Kit.
- * These are only used in responses from Alexa.
- */
- var skillName = "Particle"
- var invocationName = "Double Stuff Oreo";
- /**
- * App ID for the skill
- * Update and use this if/when you publish your skill publicly.
- * It's ok to leave this undefined until then.
- */
- var APP_ID = "amzn1.ask.skill.b909de57-f34c-49d2-81b0-da41264b5571"; //replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";
- /**
- * The AlexaSkill prototype and helper functions
- * Particle is a child of AlexaSkill.
- */
- var http = require('https');
- var AlexaSkill = require('./AlexaSkill');
- var Particle = function () {
- AlexaSkill.call(this, APP_ID);
- };
- // Extend AlexaSkill
- Particle.prototype = Object.create(AlexaSkill.prototype);
- Particle.prototype.constructor = Particle;
- Particle.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
- console.log("Near onSessionStarted requestId: " + sessionStartedRequest.requestId + ", sessionId: " + session.sessionId);
- };
- Particle.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
- console.log("Near onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
- var speechOutput = "Welcome to the Near emergency response demo.";
- response.ask(speechOutput);
- };
- Particle.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
- console.log("Near onSessionEnded requestId: " + sessionEndedRequest.requestId + ", sessionId: " + session.sessionId);
- };
- Particle.prototype.intentHandlers = {
- // Register custom intent handlers.
- // This simple skill only uses one, but more can be added.
- "ParticleIntent": function (intent, session, response) {
- var requestURI = "/v1/devices/" + deviceId + "/" + cloudName;
- var commandSlot = intent.slots.command;
- var command1 = commandSlot ? intent.slots.command.value : "";
- var speakText = "";
- var command = "";
- switch (command1) {
- case "on": case "off":
- case "temperature":
- case "fallen over":
- case "accelerometer":
- case "motion":
- case "nine one one":
- case "stroke":
- case "a stroke":
- command = command1;
- break;
- case "maybe":
- command = "cmdMaybe";
- response.tell("Maybe has been entered!");
- }
- console.log("Command = " + command + ", command1 = " + command1);
- // if(command1 == "on"){
- // speakText = "Temperature is 69°";
- // command = "on";
- // console.log("!!! cmd1 is 'on'");
- // } else if (command1 == "off") {
- // command = "off";
- // console.log("!!! cmd1 is 'off'");
- // } else if (command1 == "accelerometer" || command1 == "fallen over") {
- // speakText = "Checking patient's accelerometer";
- // command = "cmdAccelerometer";
- // console.log("!!! cmd1 is 'accelerometer'");
- // } else if (command1 == "temperature") {
- // command = "cmdTemperature";
- // }
- //response.tellWithCard("Your command is " + command, "Octothorpe" + "Command: " + command + ", Command1: " + command1);
- // Verify that a command was specified.
- // We can extend this to prompt the user,
- // but let's keep this simple for now.
- if(command.length > 0) {
- if (command == "accelerometer") {
- 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.";
- console.log("!!! Accelerometer command triggered!");
- } else if (command == "stroke" || command == "a stroke") {
- speakText = "Call nine one one immediately. Stay with victim, do not give medicine or food. Help is on the way.";
- } else if (command == "nine one one") {
- speakText = "We working on connecting with emergency services";
- }
- else {
- var rand = Math.random();
- if (rand < 0.25) {
- speakText = "Of course";
- } else if (rand < 0.5) {
- speakText = "Right away";
- } else if (rand < 0.75) {
- speakText = "You got it";
- } else {
- speakText = "As you wish";
- }
- }
- var postData = "args=" + command;
- console.log("!!! Post data = " + postData);
- response.tellWithCard(speakText, "Octothorpe Particle Reading", "Working: " + command);
- makeParticleRequest(requestURI, postData, function(resp) {
- var json = JSON.parse(resp);
- console.log(command + ": " + json.return_value);
- //response.tellWithCard(skillName, invocationName, "Thing is " + command );
- response.tellWithCard(speakText, "Octothorpe Near System", "Working: " + command );
- });
- } else {
- response.tellWithCard("I'm sorry Dave, I don't understand your command", "Octothorpe Particle Reading", "Hal meets GlaDos");
- }
- }, // ParticleIntent
- "AMAZON.HelpIntent": function (intent, session, response) {
- response.ask("You can tell " + invocationName + " to turn on or off.");
- } // HelpIntent
- };
- // Create the handler that responds to the Alexa Request.
- exports.handler = function (event, context) {
- var particleSkill = new Particle();
- particleSkill.execute(event, context);
- };
- function makeParticleRequest(requestURI, postData, callback){
- var options = {
- hostname: "api.particle.io",
- port: 443,
- path: requestURI,
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Authorization': 'Bearer ' + accessToken,
- 'Accept': '*.*'
- }
- };
- var req = http.request(options, function(res) {
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
- var body = "";
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- body += chunk;
- });
- res.on('end', function () {
- callback(body);
- });
- });
- req.on('error', function(e) {
- console.log('problem with request: ' + e.message);
- });
- // write data to request body
- req.write(postData.toString());
- req.end();
- }
Advertisement
Add Comment
Please, Sign In to add comment