在长期存在的 Lambda 函数中处理多个事件以使用 AWS Greengrass



对于一个项目,我正在使用 AWS Greengrass,需要读取传感器数据并在 Lambda 函数中进行处理。 我想出了如何通过 MQTT 发送传感器数据并将数据从 Lambda 函数发送到 AWS IoT 云来调用 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        

如果要使用长期存在的 lambda 函数,而不是在触发函数时读取数据,您必须使用每个设备的 client.get_thing_shadow(( 按顺序访问您希望从中获取数据的所有设备影子,例如每 30 秒左右。

最新更新