处理长寿命Lambda函数中的多个事件以使用AWS Greengrass

时间:2017-07-24 12:25:01

标签: python-2.7 amazon-web-services lambda aws-lambda

对于我使用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        

2 个答案:

答案 0 :(得分:0)

很抱歉劫持你的帖子,但我正在开展一个类似的项目并正在寻求帮助。

我猜你当前的流程是让AWS_DEVICE通过MQTT将传感器数据发送到AWS_CORE并触发核心上的local_lambda函数发布到云端?

我无法理解如何从MQTT连接触发local_lambda函数。你能告诉我如何通过MQTT连接触发lambda吗?

答案 1 :(得分:0)

如果要使用长寿命lambda函数,而不是在触发函数时读入数据,则必须使用客户端每30秒左右依次说出要获取数据的所有设备阴影每个设备的.get_thing_shadow()。