【发布时间】:2020-08-10 21:48:53
【问题描述】:
我是 nodejs 和学习的新手,但不知道为什么我的辅助函数不起作用。 本质上,这是通常有效的示例 alexa lambda 函数的一部分。 如果我将 MQTTcode 留在 Intent 处理程序中,则 MQTT 操作有效,但我需要将其移出到代码主体中,以便我可以从其他代码函数调用 MQTT 操作。
这个 sn-p 中有几个“测试”函数无法工作,可能是因为我不欣赏将代码移出 Intent 函数的正确方法。
我也很不清楚处理程序..(实际上是多个处理程序)代码 sn-p 中有两个处理程序..它不会引起问题,但我希望有两个 lambda 触发器(问- sdk 和智能家居),每个都调用自己的处理程序 - 不确定这是否可能。
var APP_ID = "amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // input the axela skill ID
var AWS = require('aws-sdk');
var Alexa = require("alexa-sdk");
AWS.config.region = "us-east-1";
var iotData = new AWS.IotData({endpoint: "xxxxxxxxxxx.iot.us-east-1.amazonaws.com"}); // input the AWS thing end point
var topic = "esp32/sub"; //input the topic that the device is subscribed to
// Handler for Generic Event handling accepts both SmartHomeand ask-sdk events
// But only works when the handler below is removed.
exports.handler = async function (event, context) {
// Dump the request for logging - check the CloudWatch logs
console.log("index.handler request -----");
console.log(JSON.stringify(event));
if (context !== undefined) {
console.log("index.handler context -----");
console.log(JSON.stringify(context));
}
switchon(); // test call of standalone MQTTfunction ( doesn't work)
};
// Remove this function and the Smarthome Test works.
// But is needed for the ask-sdk events ( Smarthome events fail )
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
console.log("index.handler comment -----");
};
//*********************************
// Helper code examples to functionalise the MQTT switch on
// NONE OF THESE WORK WHEN CALLED
function switchon3(){
var dataObj = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(dataObj, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
function switchon (error, data){
var params = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(params, (error, data)=>{
if (!error){this.emit(':tell', 'Robert, well done its Switched On');
}else{this.emit(':tell', 'Oh dear MQTT returned a switch on error')}
});
}
// End of helper examples
//*********************************
//********* THE PROPER CODE ************************
var handlers = {
'LaunchRequest': function () {
this.emit(':tell', 'Hello. Skill four here. How may I help you?');
},
'SwitchOnIntent': function () {
// None of the example function calls work here
// switchon3();
// this.emit(':tell', 'Test Switch On'); // needs this line to work
// The following original example code DOES work
var params = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(params, (error, data)=>{
if (!error){this.emit(':tell', 'Robert, well done its Switched On');
}else{this.emit(':tell', 'Oh dear MQTT returned a switch on error')}
});
},
已编辑...
不,Tommy,这不是太基本,感谢您的帮助。我实际上是在尝试让 lambda 接受来自两个 AWS 触发器的输入。 1.自定义技能的ASK-API 2. 智能家居触发器。 我不确定这两个触发器是否需要单独的处理函数,或者,如果我怀疑,使用 smarthome 触发器会使使用以某种方式调用注册 Intent 函数的 ask-api 方法无效, 到达的 json 的格式明显不同于两种触发器类型,我很欣赏可以在 lambda 中手动执行所有 alexa 自定义技能解析。 那么我的问题是..如果从自定义技能开始,如果我添加一个 smarthome 触发器,那么使用 ask-api 注册所有函数调用将变得无效,因为处理 ask-api 事件的一个处理程序也不能处理智能家居指令。
在整理之后,试图“带出” MQTT 调用,该调用在最初编码的 Intent 函数中工作,但如果我尝试将它们放入单独的函数调用中,则会失败。 忍耐一下……我知道我想做什么……只是还不太懂这门语言。
【问题讨论】:
标签: node.js aws-lambda alexa