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