gpt4 book ai didi

node.js HTTPs 调用返回未定义。亚马逊AWS

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:29 25 4
gpt4 key购买 nike

我对 Node.js 完全陌生,但有以下代码,这些代码主要是通过剪切和粘贴编写的。它在 Amazon AWS 中运行,响应 Alexa 调用。我正在尝试阅读 thingspeak channel 来获取泳池的温度。该 URL 返回带有温度的 json。该 channel 由 esp8266 更新,该 esp8266 正在测量 pooltemp 并将其发布到 channel 。

不幸的是,我只得到了未定义的 pooltemp。我创建函数 getCall() 来从 thingspeak 获取温度。当我在 handlerinput 中使用变量 pooltemp 时,它是未定义的。其他一切工作正常。如果我在 handlerinput 中对 pooltemp 进行硬编码,我会得到我想要的响应。

它现在的工作方式如下:第一次运行 pooltemp 是未定义的。如果立即再运行一次,我会得到 pooltemp = 30(这是新的温度)。这至少表明对 thingspeak 的调用正在发挥作用。如果我在几分钟后再次运行它,它第一次又是未定义的。似乎 AWS 在函数完成后会保留该值一段时间。

/* eslint-disable  func-names */
/* eslint-disable no-console */

const Alexa = require('ask-sdk');

var https = require('https');
var pooltemp;

getCall();

//ThingSpeak Data Access

function getCall() {
var options = {
protocol: 'https:',
host: 'api.thingspeak.com',
path: '/channels/494722/feeds.json?api_key=9HILOGJ9P2HRDPNO&results=1',
method: 'GET'
};

var getReq = https.request(options, function(res) {
console.log("\nstatus code: ", res.statusCode);
var jsonData = '';
res.on('data', function(data) {
jsonData += data;
});
res.on('end', function() {
console.log('We have all the data');
var result = JSON.parse(jsonData);
console.log('data: ', result);
console.log('Pool temperature: ', result.feeds[0].field1);
// Save the latest pool temperature.
pooltemp = result.feeds[0].field1;
});
});

//end the request
getReq.end();
getReq.on('error', function(err) {
console.log("Error: ", err);
});
}


const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' ||
(request.type === 'IntentRequest' &&
request.intent.name === 'GetNewFactIntent');
},



handle(handlerInput) {


// var pooltemp = 22;
var factIndex;
const factArr = data;
if (pooltemp <= 15) {
factIndex = 0;
}
if (15 < pooltemp == pooltemp < 20) {
factIndex = 1;
}
if (20 <= pooltemp == pooltemp < 25) {
factIndex = 2;
}
if (pooltemp >= 25) {
factIndex = 3;
}


const randomFact = factArr[factIndex];
const speechOutput = 'Hold on a second, I will check for you.<break time="1s"/>Today it is ' + pooltemp + ' degrees in the pool. ' + randomFact;
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(SKILL_NAME, randomFact)
.getResponse();

},
};

const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' &&
request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(HELP_MESSAGE)
.reprompt(HELP_REPROMPT)
.getResponse();
},
};

const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.CancelIntent' ||
request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(STOP_MESSAGE)
.getResponse();
},
};

const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

return handlerInput.responseBuilder.getResponse();
},
};

const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);

return handlerInput.responseBuilder
.speak('Sorry, an error occurred.')
.reprompt('Sorry, an error occurred.')
.getResponse();
},
};

const SKILL_NAME = 'Space Facts';
const GET_FACT_MESSAGE = 'Here\'s today temperature: ';
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';

const data = [
'It is freezing cold. Swimming only for idiots',
'It is not for wimps, but you may swim. Do you dare go for it?',
'Which is a nice temperature for a Norwegian.',
'This is quite comfortable and a swim should be really nice ',

];

const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
HelpHandler,
ExitHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();

最佳答案

我已经检查过这一点并对 getCall() 函数进行了一些修改,它现在应该可以工作了:

function getCall() {
var options = {
protocol: 'https:',
host: 'api.thingspeak.com',
path: '/channels/494722/feeds.json?api_key=9HILOGJ9P2HRDPNO&results=1',
method: 'GET'
}

var getReq = https.request(options, function(res) {
console.log("\nstatus code: ", res.statusCode);
var jsonData = '';
res.on('data', function(data) {
jsonData += data;
});
res.on('end', function() {
console.log('We have all the data');
var result = JSON.parse(jsonData);
console.log('data: ', result);
console.log('Pool temperature: ', result.feeds[0].field1);
// Save the latest pool temperature.
poolTemp = result.feeds[0].field1;
});
});

//end the request
getReq.end();
getReq.on('error', function(err) {
console.log("Error: ", err);
});
}

getCall();

您也可以在脚本开头尝试此操作:

getCall();
setInterval(getCall, 10000);

这将每 10 秒刷新一次池温度。

关于node.js HTTPs 调用返回未定义。亚马逊AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com