【发布时间】:2017-07-24 12:25:00
【问题描述】:
对于我正在使用 AWS Greengrass 的项目,我需要读取传感器数据并在 Lambda 函数中对其进行处理。 我想出了如何通过 MQTT 发送传感器数据并将数据从 Lambda 函数发送到 AWS IoT Cloud 来调用 Lambda 函数。现在我需要一个长时间运行的 Lambda 函数来收集例如一些数据并将平均值发布到 AWS IoT Cloud 或其他 Greengrass 设备。
如何在一个 Lambda 函数中从同一订阅中收集例如 5 个数据点?函数处理程序似乎不适用于长期存在的函数。那么,lambda 函数是如何实现主题中有新值(新事件)的呢?
到目前为止我的代码:
# greengrassHelloWorld.py
# Demonstrates a simple publish to a topic using Greengrass core sdk
# Since the function is
# long-lived it will run forever when deployed to a Greengrass core. The handler
# will NOT be invoked in our example since the we are executing an infinite loop.
import time
import greengrasssdk
import platform
import json
# Creating a greengrass core sdk client
client = greengrasssdk.client('iot-data')
# When deployed to a Greengrass core, this code will be executed immediately
# as a long-lived lambda function. The code will enter the infinite while loop
# below.
def greengrass_mean():
while True:
#following line just for testing
client.publish(topic='data/mean', payload='Mean :'+str(i))
while i<5:
msg='{}'.format(event['temperatur'])
actualValue=int(msg)
client.publish(topic='data/actulValue', payload='actual value: '+msg)
value += actualValue
i = i+1
mean=actualValue/5
client.publish(topic='data/mean', payload='Mean: '+str(mean))
i = 0
# Execute the function above
greengrass_mean()
def function_handler(event, context):
return null
【问题讨论】:
标签: python-2.7 amazon-web-services lambda aws-lambda