对getThingShadow的调用似乎没有在lambda函数中执行

对getThingShadow的调用似乎没有在lambda函数中执行,lambda,aws-lambda,alexa-skills-kit,aws-iot,Lambda,Aws Lambda,Alexa Skills Kit,Aws Iot,我正在开发一个lambda alexa技能处理程序,它与aws物联网中的一个物体阴影相集成。我的问题是下面对iotData.getThingShadow的调用似乎没有执行。回调函数中未生成任何控制台消息,且对象objState未填充。下面是代码片段 // get the shadow for the home-garage-door console.log('Getting thing shadow'); iotData.getThingShadow(params, fu

我正在开发一个lambda alexa技能处理程序,它与aws物联网中的一个物体阴影相集成。我的问题是下面对iotData.getThingShadow的调用似乎没有执行。回调函数中未生成任何控制台消息,且对象objState未填充。下面是代码片段

   // get the shadow for the home-garage-door
    console.log('Getting thing shadow');

    iotData.getThingShadow(params, function(err, data) {
        console.log('Inside of getThingShadow callback');
        if (err){
            //Handle the error here
            console.log('Problem getting the thing shadow');
            console.log(err, err.stack);
            responseString = "I had trouble contacting the garage door, sorry";
        }
        else {
            console.log("Data back from shadow", JSON.stringify(data));
            payload = data.payload;
            objState = JSON.parse(payload);
        }
    });

    console.log('After getting the Shadow'); 
下面是我测试时生成的日志条目。有人知道我可能做错了什么吗

2018-01-14T22:55:15.564Z    fbdde171-f97d-11e7-bc0b-116e508011bd    Getting thing shadow
2018-01-14T22:55:16.127Z    fbdde171-f97d-11e7-bc0b-116e508011bd    After getting the Shadow
2018-01-14T22:55:16.165Z    fbdde171-f97d-11e7-bc0b-116e508011bd    objState: undefined
这是完整的代码

/**
    Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

    Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at

        http://aws.amazon.com/apache2.0/

    or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

/**
 *
 */

/**
 * App ID for the skill
 */
 var APP_ID = 'amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'; 


/**
 * The AlexaSkill prototype and helper functions
 */
var AlexaSkill = require('./AlexaSkill');
var AWS = require('aws-sdk');

/**
 * GarageDoorIntentHandler is a child of AlexaSkill.
 * To read more about inheritance in JavaScript, see the link below.
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
 */
var GarageDoorIntentHandler = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
GarageDoorIntentHandler.prototype = Object.create(AlexaSkill.prototype);
GarageDoorIntentHandler.prototype.constructor = GarageDoorIntentHandler;

GarageDoorIntentHandler.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
    console.log("GarageDoorIntentHandler onSessionStarted requestId: " + sessionStartedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any initialization logic goes here
};

GarageDoorIntentHandler.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    console.log("GarageDoorIntentHandler onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
    var speechOutput = "Welcome to the Automated Garage door for Alexa, you can say open the garage door or close the garage door";
    var repromptText = "You can say open or close the garage door";
    response.ask(speechOutput, repromptText);
};

GarageDoorIntentHandler.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
    console.log("GarageDoorIntentHandler onSessionEnded requestId: " + sessionEndedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any cleanup logic goes here
};

GarageDoorIntentHandler.prototype.intentHandlers = {

    // register custom intent handlers
    "moveDoorIntent": function (intent, session, response) {

        var responseString = ''; 
        var payload = {};
        var objState;

        var desiredDoorState = intent.slots.direction.value;

        var iotData = new AWS.IotData({endpoint: "XXXXXXXXXXXXXX.iot.us-east-1.amazonaws.com"});

        console.log('iotData: ' + JSON.stringify(iotData));

        var params = { "thingName" : "<My Thing Name Was Removed for Posting>" };


        console.log('Intent: ' + JSON.stringify(intent));

        // get the shadow for the home-garage-door
        console.log('Getting thing shadow');

        iotData.getThingShadow(params, function(err, data) {
            console.log('Inside of getThingShadow callback');
            if (err){
                //Handle the error here
                console.log('Problem getting the thing shadow');
                console.log(err, err.stack);
                responseString = "I had trouble contacting the garage door, sorry";
            }
            else {
                console.log("Data back from shadow", JSON.stringify(data));
                payload = data.payload;
                objState = JSON.parse(payload);
            }
        });

        console.log('After getting the Shadow');

        switch(desiredDoorState.toUpperCase()) {
            case 'OPEN':
                console.log('objState: ' + JSON.stringify(objState));
                switch (objState.state.reported.status.toUpperCase()) {
                    case 'OPEN': 
                        responseString = "I checked and the door is already open"; 
                        break;
                    case 'CLOSED':
                        responseString = "Ok, I am closing the garage door"; 
                        params.payload = buildObjectState('desired', 'doorActivated', true, 'string');
                        iotData.updateThingShadow(params, function(err, data) {
                            if (err){
                                //Handle the error here
                                console.log(err, err.stack);
                                responseString = "I had trouble doing that, sorry";
                            }
                            else {
                                responseString = "Ok, I opened the garage door";
                                console.log("Data back from shadow", data);
                            }
                        });
                }
                break;
            case 'CLOSE':
                break;
            default: 

        } // switch

        response.tellWithCard(responseString, "Open or Close the Garage Door", responseString);
    },

    "getDoorStateIntent": function (intent, session, response) {
        response.tellWithCard("You can say hello to me!", "You can say hello to me!");
    }
};


/**
*   Helper function to build the state object for the shadow document
**/

function buildObjectState (section, key, value, format) {

    var objState =  {state: {}};

    objState.state[section] = {};
    objState.state[section][key] = value;


    if (format === 'object') {
        return objState;
    } else if (format === 'string') {
        return JSON.stringify(objState);
    } else return; 

}

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {

    console.log('received an event'); 
    // Create an instance of the garageDoorIntentHandler.
    var garageDoorIntentHandler = new GarageDoorIntentHandler();
    garageDoorIntentHandler.execute(event, context);
};
/**
版权所有2014-2015 Amazon.com,Inc.或其附属公司。版权所有。
根据Apache许可证2.0版(“许可证”)获得许可。除非遵守许可证,否则不得使用此文件。许可证副本位于
http://aws.amazon.com/apache2.0/
或在该文件附带的“许可证”文件中。本文件按“原样”分发,无任何明示或暗示的保证或条件。请参阅许可证,了解管理许可证下权限和限制的特定语言。
*/
/**
*
*/
/**
*技能的应用程序ID
*/
变量APP_ID='amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-xxxxxxxxxxxxxxxx';
/**
*AlexaSkill原型和助手函数
*/
变量AlexaSkill=require('./AlexaSkill');
var AWS=要求('AWS-sdk');
/**
*GarageDoorIntentHandler是一个技艺高超的孩子。
*要阅读有关JavaScript继承的更多信息,请参阅下面的链接。
*
*@见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
*/
var GarageDoorIntentHandler=函数(){
AlexaSkill.call(此,应用程序ID);
};
//扩展技能
GarageDoorIntentHandler.prototype=Object.create(AlexaSkill.prototype);
GarageDoorIntentHandler.prototype.constructor=GarageDoorIntentHandler;
GarageDoorIntentHandler.prototype.eventHandlers.onSessionStarted=函数(sessionStartedRequest,会话){
log(“GarageDoorIntentHandler OnSessionStartedRequestId:”+sessionStartedRequest.requestId
+“,sessionId:”+session.sessionId);
//这里有任何初始化逻辑
};
GarageDoorIntentHandler.prototype.eventHandlers.onLaunch=函数(启动请求、会话、响应){
log(“GarageDoorIntentHandler onLaunch requestId:+launchRequest.requestId+”,sessionId:+session.sessionId”);
var speechOutput=“欢迎来到Alexa的自动车库门,您可以说打开车库门或关闭车库门”;
var repromptText=“您可以说打开或关闭车库门”;
回答.提问(speechOutput,repromptText);
};
GarageDoorIntentHandler.prototype.eventHandlers.onSessionEnded=函数(SessionEndRequest,会话){
log(“GarageDoorIntentHandler onSessionEnded requestId:”+SessionEndRequest.requestId
+“,sessionId:”+session.sessionId);
//任何清理逻辑都在这里
};
GarageDoorIntentHandler.prototype.IntentHandler={
//注册自定义意图处理程序
“moveDoorIntent”:功能(意图、会话、响应){
var responseString='';
var有效载荷={};
叶酸变种;
var desiredDoorState=intent.slots.direction.value;
var iotData=new AWS.iotData({端点:“xxxxxxxxxxxxx.iot.us-east-1.amazonaws.com});
log('iotData:'+JSON.stringify(iotData));
var params={“thingName”:“};
log('Intent:'+JSON.stringify(Intent));
//为家庭车库门设置阴影
log('get thing shadow');
iotData.getThingShadow(参数,函数(err,data){
log('getThingShadow回调的内部');
如果(错误){
//在这里处理错误
log('problemgetthethingshadow');
日志(err,err.stack);
responseString=“我无法联系车库门,对不起”;
}
否则{
log(“从阴影中返回的数据”,JSON.stringify(数据));
有效载荷=data.payload;
objState=JSON.parse(有效负载);
}
});
log('在得到阴影之后');
开关(desiredDoorState.toUpperCase()){
案件“未结”:
log('objState:'+JSON.stringify(objState));
开关(objState.state.reported.status.toUpperCase()){
案件“未结”:
responseString=“我检查过了,门已经打开”;
打破
案件“结案”:
responseString=“好的,我正在关车库门”;
params.payload=buildObjectState(“所需”、“门已激活”、true、“字符串”);
iotData.updateThingShadow(参数,函数(错误,数据){
如果(错误){
//在这里处理错误
日志(err,err.stack);
responseString=“我做那件事有困难,对不起”;
}
否则{
responseString=“好的,我打开了车库门”;
log(“从阴影中返回的数据”,数据);
}
});
}
打破
案例“结束”:
打破
违约:
}//开关
响应。tellWithCard(响应命令,“打开或关闭车库门”,响应命令);
},
“getDoorStateIntent”:函数(意图、会话、响应){
tellWithCard(“你可以向我打招呼!”,“你可以向我打招呼!”);
}
};
/**
*帮助器函数,用于为卷影文档构建状态对象
**/
函数buildObjectState(节、键、值、格式){
var objState={state:{};
objS
// This is called BEFORE iotData.getThingShadow()
console.log('Getting thing shadow');

iotData.getThingShadow(params, function(err, data) {
    console.log('Inside of getThingShadow callback');
    if (err) {
        //Handle the error here
        console.log('Problem getting the thing shadow');
        console.log(err, err.stack);
        responseString = "I had trouble contacting the garage door, sorry";
    } else {
        console.log("Data back from shadow", JSON.stringify(data));
        payload = data.payload;
        // This is populated AFTER iotData.getThingShadow() finishes
        objState = JSON.parse(payload);
        // This is where you put the code that you want to happen AFTER 
        // iotData.getThingShadow() finishes.
        console.log(objState)
    }
});

// This will execute without waiting for iotData.getThingShadow() to finish
console.log('After getting the Shadow');

// This will be undefined since iotData.getThingShadow() did NOT return 
// yet and objState has NOT been populated
console.log(objState)
/Initialize Up Top of Script
AWS.config.region = process.env.IOT_REGION;
var IOT_Data = new AWS.IotData({endpoint: process.env.IOT_ENDPOINT});
var IOT_Thing = process.env.IOT_THING_NAME;
function getShadow(dataObj){
    IOT_Data.getThingShadow(dataObj, function (err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    })
}