【发布时间】:2019-09-28 18:39:20
【问题描述】:
我是 Lambda 的新手,我正在尝试编写一个函数,该函数将每 24 小时通过 SNS 发送一封具有流量的电子邮件。此 Lambda 函数由 IoT 规则触发,没有 24 小时限制,收件箱将充满电子邮件。在 CloudWatch 中,即使 email_sent 标志等于 1,看起来也正在发送电子邮件。所以这让我觉得我的括号和括号结构不正确?有人看到这段代码有什么问题吗?
var email_sent = 0; //Flag to determine if the email has been sent in the last 24 hours
var starttime = new Date(); //Date & time when the script starts running
....
exports.handler = (event, context, callback) => {
console.log('Start time.', starttime);
console.log('Email Sent Flag.', email_sent);
console.log('Received event:', event.my_volume, ' mL Volume'); //Fluid volume
var miliseconds = new Date() - starttime; //Calculate the time that has passed
console.log('Miliseconds.', miliseconds);
console.log(miliseconds/1000 + " Seconds.");
if (miliseconds => 86,400,000) { //Has 24 hours passed?
email_sent = 0; //set the email flag back to zero if the time has passed
}
// create/get topic
if (email_sent == 0) { //if the email flag is not set, setup topic and send the email
createTopic('aws-iot-button-sns-topic', (err, topicArn) => {
if (err) {
return callback(err);
}
console.log(`Publishing to topic ${topicArn}`);
// publish message
const params = {
Message: `The fluid level is low on your system: ${event.my_volume} mL.`,
Subject: `Low Fluid Level Alert - Knight`,
TopicArn: topicArn,
};
// result will go to function callback
SNS.publish(params, callback);
console.log('SNS Published.');
email_sent = 1; // after email is sent, reset the flag
starttime = new Date(); // reset the starttime after the email is sent
}
);
}
};
谢谢,
史蒂夫
【问题讨论】:
-
能否更详细地描述您的要求?是“每天一次,如果 IoT 显示低于 X 的值,则向 Amazon SNS 主题发送消息”?假设某些东西正在更新 IoT 中的值(可能是设备影子)是否正确?当前触发 Lambda 函数的是什么——物联网是否触发了它?告诉我们有关您的设置的更多信息将有助于我们为您提供建议。
-
我正在测量与 AWS IoT Core 服务通信的设备中的液位。如果液位低于阈值,我想使用触发 Lambda 函数的 IoT 规则发送警报。因为设备影子每隔几秒钟更新一次,所以它会在收件箱中充斥着警报。所以 AWS 支持说我需要使用 Lambda 函数来发送电子邮件并将其执行限制为每 24 小时一次。所以是的,每天一次,如果 IoT 显示的值低于 X,则向 Amazon SNS 主题发送一条消息。
-
您真的想每 24 小时发送一次警报,还是只发送一次警报就足够了(当液位低于阈值时)?我问这个是因为它可能值得将指标发布到 Amazon CloudWatch,然后使用警报触发 Amazon SNS 通知。另一方面,如果您确实希望每 24 小时发出一次警报,那么 AWS Lambda 函数将需要跟踪时间。
-
我只想在液位低于阈值时发送警报,因为这是有人去手动重新填充液体容器的信号。一旦液位低于阈值,警报需要限制为每 24 小时一次。否则,每次更新设备影子时,收件箱都会收到警报。
-
是的,但是如果级别下降到阈值以下并且发送了一条消息,并且级别仍然低于阈值,则需要再次发送通知,或者一条消息足够并且存在不需要重复发送消息吗? (如果一条消息就足够了,那么可以选择 CloudWatch。如果需要重复发送,则需要对解决方案应用额外的逻辑和历史跟踪。)
标签: node.js aws-lambda amazon-sns