1

我试图从Firebase中的数据库中读取对象。我发现an example in GitHub using firebase-admin。我试图做类似的时尚。当我尝试在开发人员控制台中的服务模拟器上对其进行测试时,它无法从Firebase访问数据,并通过提及该服务已过期而终止。我不确定是否缺少任何步骤。在Alexa中访问Firebase数据库时服务超时

以下是东西,我没有测试它:

我禁用在火力控制台中的身份验证,并使其成为公众。 我也尝试过使用Firebase模块和Firebase管理模块,它的两个选项都是相同的情况。 将AWS控制台中的超时增加到2分钟,但没有帮助。

我也看了其他SO问题,但它没有帮助解决这个问题。

下面是index.js我的代码:

'use strict'; 
var Alexa = require('alexa-sdk'); 
// Initialize fireBase with a service account, granting admin privileges 
var firebaseAdmin = require("firebase-admin"); 
firebaseAdmin.initializeApp({ 
    credential: firebaseAdmin.credential.cert("firebase-auth.json"), 
    databaseURL: "https://alexa-595c9.firebaseio.com/" 
}); 

var APP_ID = 'amzn1.ask.skill.13638113-6557-4cff-84d2-6d46e7f7539e'; 

var dataRef = firebaseAdmin.database().ref("Data"); 

exports.handler = function(event, context, callback) { 
    var alexa = Alexa.handler(event, context); 
    alexa.APP_ID = APP_ID; 
    // To enable string internationalization (i18n) features, set a resources object. 
    alexa.resources = languageStrings; 
    alexa.registerHandlers(handlers); 
    alexa.execute(); 
}; 


var handlers = { 

    //Use LaunchRequest, instead of NewSession if you want to use the one-shot model 
    // Alexa, ask [my-skill-invocation-name] to (do something)... 
    'LaunchRequest': function() { 
     this.attributes['speechOutput'] = this.t("WELCOME_MESSAGE", this.t("SKILL_NAME")); 
     // If the user either does not reply to the welcome message or says something that is not 
     // understood, they will be prompted again with this text. 
     this.attributes['repromptSpeech'] = this.t("WELCOME_REPROMT"); 
     this.emit(':ask', this.attributes['speechOutput'], this.attributes['repromptSpeech']) 
    }, 
    'QueryIntent': function() { 

     var speechOutput 
     var itemSlot = this.event.request.intent.slots.Item; 
     var itemName; // name of dataRequested 
     if (itemSlot && itemSlot.value) { 
      itemName = itemSlot.value.toLowerCase(); 
     } 
     // Call to FireBase for data object 
     dataRef.once('value', function(snapshot) { 
      var firebaseData = snapshot.val(); 
      console.log('Connection success'); 
      console.log('snapshot:',snapshot,'firebasedata:',firebaseData); 
         var parsedItemName = parseSimilar(itemName); 

      switch(parsedItemName){ 
       case "temperature": 
        speechOutput = "The board's " + itemName + " sensor reads: " + firebaseData.temp + " degrees fahrenheit."; 
        break; 
        case "light": 
        speechOutput = "The board's " + itemName + " sensor reads: " + firebaseData.light + " lumens."; 
        break; 
       case "memory": 
        speechOutput = "The board is using " + firebaseData.mem.globalUsed + firebaseData.mem.mallocUsed + " out of " + firebaseData.mem.systemAvail + " total memory blocks. Malloc is using " + firebaseData.mem.mallocUsed + " blocks, global space is using " + firebaseData.mem.globalUsed + " blocks and there are " + firebaseData.mem.mallocUsed + " availble malloc blocks."; 
        break; 
       case "button": 
        speechOutput = "The button press status is as follows: Button 1: " + firebaseData.sw[0] + ", Button 2: " + firebaseData.sw[1] + ", Button 3: " + firebaseData.sw[2] + ", and Button 4: " + firebaseData.sw[3] + ". "; 
        break; 
       case "task": 
        speechOutput = "The top three tasks using the most CPU are as follows: " + firebaseData.task[0].name + ". with " + firebaseData.task[0].percent + " percent... " + firebaseData.task[1].name + ". with " + firebaseData.task[1].percent + " percent... and " + firebaseData.task[2].name + ". with " + firebaseData.task[2].percent + " percent. "; 
        break; 
       case "accelerometer": 
         speechOutput = "The accelerometer reads: X: " + firebaseData.x + ". Y: " + firebaseData.y + ". and, Z: " + firebaseData.z + ". "; 
         break; 
       case "last update time": 
         speechOutput = "The last update was recieved on <say-as interpret-as=\"date\">" + firebaseData.date + "</say-as> at " + "<say-as interpret-as=\"time\">"+ firebaseData.time.substring(0,5) + "</say-as> . "; 
        break; 

       default: 
        speechOutput = null; 
        break; 
      } 
      if (speechOutput !== null) { 
       this.attributes['speechOutput'] = speechOutput; 
       this.attributes['repromptSpeech'] = this.t("RECIPE_REPEAT_MESSAGE"); 
       this.emit(':askWithCard', this.attributes['speechOutput'], this.attributes['repromptSpeech'], cardTitle, speechOutput); 
      } else { 
       var speechOutput = this.t("RECIPE_NOT_FOUND_MESSAGE"); 
       var repromptSpeech = this.t("RECIPE_NOT_FOUND_REPROMPT"); 
       if (itemName) { 
        speechOutput += this.t("RECIPE_NOT_FOUND_WITH_ITEM_NAME", itemName); 
       } else { 
        speechOutput += this.t("RECIPE_NOT_FOUND_WITHOUT_ITEM_NAME"); 
       } 
       speechOutput += repromptSpeech; 

       this.attributes['speechOutput'] = speechOutput; 
       this.attributes['repromptSpeech'] = repromptSpeech; 

       this.emit(':ask', speechOutput, repromptSpeech); 
      } 
     }.bind(this)); 
    }, 

    'AMAZON.HelpIntent': function() { 
     this.attributes['speechOutput'] = this.t("HELP_MESSAGE"); 
     this.attributes['repromptSpeech'] = this.t("HELP_REPROMT"); 
     this.emit(':ask', this.attributes['speechOutput'], this.attributes['repromptSpeech']) 
    }, 
    'AMAZON.RepeatIntent': function() { 
     this.emit(':ask', this.attributes['speechOutput'], this.attributes['repromptSpeech']) 
    }, 
    'AMAZON.StopIntent': function() { 
     this.emit('SessionEndedRequest'); 
    }, 
    'AMAZON.CancelIntent': function() { 
     this.emit('SessionEndedRequest'); 
    }, 
    'SessionEndedRequest':function() { 
     this.emit(':tell', this.t("STOP_MESSAGE", this.t("SKILL_NAME"))); 
    } 
}; 

var languageStrings = { 
    "en-US": { 
     "translation": { 
      "SKILL_NAME" : "MicroWatch", 
      "WELCOME_MESSAGE": "Welcome to %s. You can request SJOne board info by saying things like, 'what is the current temperature', or 'what is the current light value'... Now, what can I help you with?", 
      "WELCOME_REPROMT": "For a list of accepted commands, just say 'help'.", 
      "DISPLAY_CARD_TITLE": "%s - Value for %s.", 
      "HELP_MESSAGE": "You can say things like, 'what is the current temperature'... Or you can invoke the skill directly by saying 'Ask MicroWatch for the current temperature'... Now, what can I help you with?", 
      "HELP_REPROMT": "You can say things like, 'what is the current temperature', or you can say exit... Now, what can I help you with?", 
      "STOP_MESSAGE": "Thanks for using %s, Goodbye!", 
      "RECIPE_REPEAT_MESSAGE": "Try saying repeat.", 
      "RECIPE_NOT_FOUND_MESSAGE": "I'm sorry, I currently can't monitor ", 
      "RECIPE_NOT_FOUND_WITH_ITEM_NAME": "the %s. ", 
      "RECIPE_NOT_FOUND_WITHOUT_ITEM_NAME": "that parameter. ", 
      "RECIPE_NOT_FOUND_REPROMPT": "What else can I help with?" 
     } 
    } 
}; 

function parseSimilar(item){ 
    switch(item){ 
     case "task usage": 
      item = "task"; 
      break; 
     case "cpu usage": 
      item = "task"; 
      break; 
     case "cpu": 
      item = "task"; 
      break; 
     case "tasks": 
      item = "task"; 
      break; 
     case "top tasks": 
      item = "task"; 
      break; 
     case "top three tasks": 
      item = "task"; 
      break; 
     case "last update": 
      item = "last update time"; 
      break; 
     case "accel": 
      item = "accelerometer"; 
      break; 

     default: 
      break; 
    } 
    return item 
} 

请让我知道,如果没有人尝试过。期待听到您的建议。

+0

这是AWS lambda吗?你确定你正在使用lambda封装回调吗?发布整个lambda源码。如果节点事件循环停滞不前,您将收到超时。 – Mike

+0

在调用dataRef.once()之后,你期望发生什么?你获得了快照的价值,然后呢?它看起来不像你在这里发布你的整个功能。 –

+0

嗨@Mike,是它的一个lamba功能。我更新了完整的代码。不便之处敬请谅解。 – sur

回答

0

我不认为你曾经告诉过你的运行时间。你必须做下列之一:

  • 调用回调函数
  • 调用context.done功能
  • 调用context.succeed功能
  • 调用context.fail功能

请参阅:http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

您想要在代码完成后执行其中一项操作,而不是在机器人上执行代码你的顶级处理函数的tom。由于您使用的是异步调用,因此您需要在最后一个执行结束时调用它。

不要忘记错误情况,你应该在你停止运行的任何地方调用context.fail(someErr)或callback(someErr),因为错误通常意味着需要添加catch()处理程序。

+0

Hi @Mike,感谢您的及时回复。如果dataref.once方法返回错误以将此信息通知给调用者,我试图在成功时添加回调函数,并在回调函数中添加回调函数。但不幸的是,它没有奏效。 – sur