zoukankan      html  css  js  c++  java
  • AWS IoT Greengrass 入门-模块3(第 2 部分):AWS IoT Greengrass 上的 Lambda 函数

    AWS IoT Greengrass 入门-模块3(第 2 部分):AWS IoT Greengrass 上的 Lambda 函数

    创建和打包Lambda函数

    1. 将 Lambda 函数代码下载到计算机(而不是 Greengrass 核心设备):

      ​ a. 在 Web 浏览器中,打开 GitHub 上的 greengrassHelloWorldCounter.py 文件。

      ​ b. 选择原始以打开未格式化版本的文件。

      ​ c. 使用 Ctrl + S(对于 Mac,则为 Command + S)保存 greengrassHelloWorldCounter.py 文件的副本。将该文件保存到包含 greengrasssdk 文件夹的文件夹。

    2. greengrassHelloWorldCounter.py 文件与开发工具包一起打包到 .zip 文件中,如模块 3(第 1 部分)中所述。将该程序包命名为 hello_world_counter_python_lambda.zip

    3. 在 Lambda 控制台中,创建一个名为 Greengrass_HelloWorld_Counter 的 Python 2.7 函数,如模块 3(第 1 部分)中所述。您可以使用现有角色。

    4. 上传 Lambda 函数部署软件包,保存,并发布第一个版本。如模块 3(第 1 部分)中所述。

      • 对于 (处理程序),输入 greengrassHelloWorldCounter.function_handler
    5. 为函数版本创建别名:

      ​ a. 在(操作)菜单中,选择(创建别名),然后设置以下值:

      • 对于 (名称),输入 GG_HW_Counter
      • 对于 (版本),选择 1
      • 选择 (创建)

      ​ b. 选择 (创建)

      别名可为您的 Lambda 函数创建单个实体,AWS IoT Greengrass 设备可以订阅该实体,而无需在每次修改函数时使用 Lambda 版本号更新订阅。

    为 AWS IoT Greengrass 配置长时间生存的 Lambda 函数

    现在,您已准备好为 AWS IoT Greengrass 配置您的 Lambda 函数。

    1. 在 AWS IoT 控制台中的 (Greengrass) 下,选择 (组),然后选择您在模块 2 中创建的组。

    2. 在组配置页面上,选择 (Lambdas),然后选择 (添加 Lambda)

    3. 将 Lambda 添加到 Greengrass 组页面上,选择使用现有 Lambda

    4. (使用现有 Lambda) 页面上,选择 Greengrass_HelloWorld_Counter,然后选择 Next (下一步)

    5. (选择 Lambda 版本) 页面上,选择 (别名: GG_HW_Counter),然后选择 Finish

    6. Lambdas 页面上,从 菜单中选择 (编辑配置)

    7. 在配置页面上,编辑以下属性:

      • 超时设置为 25 秒。此 Lambda 函数在每次调用前会休眠 20 秒。
      • 对于 Lambda 生命周期,选择 (使此函数长时间生存,保持其无限期运行)
      • 接受所有其他字段(例如 (运行方式)(容器化))的默认值。

    8. 选择 (更新)

    测试长时间生存的 Lambda 函数

    长时间生存的 Lambda 函数在 AWS IoT Greengrass Core 启动时自动启动(并在单个容器/沙盒中运行)。为函数处理程序的每次调用保留在函数处理程序外部定义的任何变量或预处理。函数处理程序的多次调用将排队,直到执行完前面的调用。

    以下代码来自 greengrassHelloWorldCounter.py。 此 Lambda 函数类似于此模块第 1 部分中的 greengrassHelloWorld.py 函数,但它在函数处理程序之外定义 my_counter 变量。

    #
    # Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    #
    
    # greengrassHelloWorldCounter.py
    # Demonstrates a simple publish to a topic using Greengrass core sdk
    # This lambda function will retrieve underlying platform information and send a hello world message along with the
    # platform information to the topic 'hello/world/counter' along with a counter to keep track of invocations.
    #
    # This Lambda function requires the AWS Greengrass SDK to run on Greengrass devices. This can be found on the AWS IoT Console.
    
    import greengrasssdk
    import platform
    import time
    import json
    
    # Creating a greengrass core sdk client
    client = greengrasssdk.client('iot-data')
    
    # Retrieving platform information to send from Greengrass Core
    my_platform = platform.platform()
    
    # Counter to keep track of invocations of the function_handler
    my_counter = 0
    
    
    def function_handler(event, context):
        global my_counter
        my_counter = my_counter + 1
        if not my_platform:
            client.publish(
                topic='hello/world/counter',
                payload=json.dumps({'message': 'Hello world! Sent from Greengrass Core.  Invocation Count: {}'.format(my_counter)})
            )
        else:
            client.publish(
                topic='hello/world/counter',
                payload=json.dumps({'message': 'Hello world! Sent from Greengrass Core running on platform: {}.  Invocation Count: {}'
                                    .format(my_platform, my_counter)})
            )
        time.sleep(20)
        return
    

    在该步骤中,您将创建允许 Lambda 函数和 AWS IoT 交换 MQTT 消息的订阅。然后,部署组并测试函数。

    1. 在组配置页面中,选择 (订阅),然后选择 (添加订阅)

    2. (选择源)下,选择 (Lambda) 选项卡,然后选择 Greengrass_HelloWorld_Counter

    3. (选择目标)下,依次选择(服务)选项卡、(IoT 云)(下一步)

    4. 对于 (主题筛选条件),输入 hello/world/counter。选择(下一步),然后选择(完成)

      此单个订阅仅在一个方向:从 Greengrass_HelloWorld_Counter Lambda 函数到 AWS IoT。要从云中调用(或触发)此 Lambda 函数,您必须创建反方向的订阅。

    5. 按照步骤 (1)(4) 添加另一个订阅,它使用以下值。此订阅允许 Lambda 函数从 AWS IoT 接收消息。此订阅在您从控制台向此函数发送一条测试消息后实施。

      • 对于源,选择服务,然后选择 IoT Cloud (IoT 云)
      • 对于目标,选择 Lambda,然后选择 Greengrass_HelloWorld_Counter
      • 对于主题筛选条件,输入 hello/world/counter/trigger

    6. 确保 AWS IoT Greengrass 守护程序正在运行。

      • 要检查守护程序是否正在运行,请执行以下操作:

        ps aux | grep -E 'greengrass.*daemon'
        

        如果输出包含 /greengrass/ggc/packages/1.9.4/bin/daemonroot 条目,则表示守护程序正在运行。

      • 要启动守护程序,请执行以下操作:

        cd /greengrass/ggc/core/
        sudo ./greengrassd start
        

      现在已准备好将 Lambda 函数和订阅配置部署到您的 AWS IoT Greengrass Core 设备。

    7. 在组配置页面上,从 (操作) 中选择 (部署)。 这会将组配置部署到您的 AWS IoT Greengrass Core 设备。

    8. 在您的部署完成后,返回到 AWS IoT 控制台主页并选择 (测试)

    9. 配置以下字段:

      • 对于订阅主题,输入 hello/world/counter
      • 对于服务质量,选择 0
      • 对于 (MQTT 负载显示),选择 (以字符串形式显示负载)

    10. 选择订阅主题

      与此模块的第 1 部分不同,在您订阅 hello/world/counter 之后,您不应看到任何消息。这是因为发布到 hello/world/counter 主题的 greengrassHelloWorldCounter.py 代码位于函数处理程序中,该函数处理程序仅在调用函数时才运行。

      在此模块中,您将 Greengrass_HelloWorld_Counter Lambda 函数配置当它在 hello/world/counter/trigger 主题上收到 MQTT 消息时被调用。

      Greengrass_HelloWorld_CounterIoT Cloud 订阅允许该函数在 hello/world/counter 主题上向 AWS IoT 发送消息。IoT CloudGreengrass_HelloWorld_Counter 订阅允许 AWS IoT 在 hello/world/counter/trigger 主题上向该函数发送消息。

      注意

      Greengrass_HelloWorld_Counter 忽略收到的消息的内容。它只运行 function_handler 中的代码,该代码向 hello/world/counter 主题发送消息。

    11. 要测试较长的生命周期,请通过向 hello/world/counter/trigger 主题发布消息来调用 Lambda 函数。

      每次将消息发布到 hello/world/counter/trigger 主题时,my_counter 变量都会递增。此调用计数显示在从 Lambda 函数发送的消息中。由于函数处理程序包含 20 秒休眠周期 (time.sleep(20)),反复触发处理程序队列将需要排队等候来自 AWS IoT Greengrass Core 的响应。

    测试按需 Lambda 函数

    按需 Lambda 函数在功能上与基于云的 AWS Lambda 函数类似。按需 Lambda 函数的多次调用可以并行运行。Lambda 函数调用创建单独的容器以处理调用,或者在资源允许时重复使用现有的容器。在创建容器时,不会保留在函数处理程序外部定义的任何变量或预处理。

    1. 在组配置页面上,选择 Lambdas

    2. 对于 Greengrass_HelloWorld_Counter Lambda 函数,选择 (编辑配置)

    3. (Lambda 生命周期) 下面,选择 (按需函数),然后选择 (更新)

    4. 在组配置页面上,从 (操作) 中选择 (部署)。 这会将组配置部署到您的 AWS IoT Greengrass Core 设备。

    5. 在您的部署完成后,返回到 AWS IoT 控制台主页并选择 (测试)

    6. 配置以下字段:

      • 对于订阅主题,输入 hello/world/counter
      • 对于服务质量,选择 0
      • 对于 MQTT payload display (MQTT 负载显示),选择 Display payloads as strings (以字符串形式显示负载)

    7. 选择订阅主题注意:在您订阅之后,您不应看到任何消息。

    8. 要测试按需生命周期,请通过向 hello/world/counter/trigger 主题发布消息来调用该函数。您可以使用默认消息。

      a. 快速选择 (发布到主题) 三次,每次都在五秒钟内按下按钮。

      每次发布都会调用函数处理程序并为每次调用创建一个容器。在您三次触发该函数时,不会为触发递增调用计数,因为每个按需 Lambda 函数都有自己的容器/沙盒。

      b. 等待大约 (30) 秒,然后选择 (发布到主题)。调用计数应递增到 (2)。这说明重复使用了从之前调用创建的容器,并且存储了函数处理程序外部的预处理变量。

  • 相关阅读:
    mojoportal学习——文章翻译之SmartCombo
    windows froms 程序打包 (转载)
    [笔记]LCD1602 Display Experiment
    [笔记] JLink V8固件烧录指导
    [笔记]NiosII之Count Binary
    [笔记]DE2115 LCD1602字符的显示
    [笔记]What is HDBaseT
    [笔记]Inrush Current Case
    [笔记]远传中继的实现
    [笔记]RunningLED Experiment
  • 原文地址:https://www.cnblogs.com/--Simon/p/11897134.html
Copyright © 2011-2022 走看看