【问题标题】:Handling multiple events in long-lived Lambda Function for using AWS Greengrass在长期存在的 Lambda 函数中处理多个事件以使用 AWS Greengrass
【发布时间】: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


    【解决方案1】:

    如果您要使用长寿命的 lambda 函数,而不是在触发该函数时读取数据,您必须使用客户端每隔 30 秒左右依次访问您希望从中获取数据的所有设备影子每个设备的 .get_thing_shadow()。

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 2021-05-01
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2021-10-25
      • 2018-11-14
      • 1970-01-01
      相关资源
      最近更新 更多