百度智能云

All Product Document

          Intelligent Edge

          Customize Function Running Packet and Distribute It to Edge Node

          Typically, we need to refer to the third-party dependency during the writing of functions. This section introduces how to upload the code and third-party dependency to BOS and introduces this function into the function application.

          The following takes Python3.6 as an example for illustration.

          Create Configuration Item

          Code development

          Before the code is uploaded to the cloud, we need to develop and test the code on the development machine.

          We write the case to get the headers information of https://cloud.baidu.com/ , and create a new folder with the code file naming index.pyas follows:

          #!/usr/bin/env python3
          # -*-coding: utf-8 -*-
          
          import requests
          
          def handler(event, context):
            """
            data: {"action": "A"}
            """
            if 'action' in event:
              if event['action'] == 'A':
                r = requests.get('https://cloud.baidu.com/')
                if str(r.status_code) == '200':
                  event['info'] = dict(r.headers)
                else:
                  event['info'] = 'exception found'
              else:      event['info'] = 'action error'
            else:
              event['error'] = 'action not found'
          
            return event

          You can see that we use the requests library. Next, we need to download the requests source code to this folder and type the codes in the command line:

          pip3 download requests

          image.png

          After downloading, we find that several compressed packages end with .whl in the folder.

          image.png

          Decompress the .whl compressed package to get the source code.

          unzip -d . "*.whl"

          image.png

          In this way, we get the source code, but we do not need the compressed packages and .dist-info. To minimize the file size, we can delete these files by typing the following codes in the command line:

          rm -rf *.whl *.dist-info

          image.png

          Then, we can carry out a simple test to view index.py whether outputs what we want correctly.

          Create a test.py file in this folder, whose code is shown as follows:

          import index
          
          event = {}
          event['action'] = "A"
          print(index.handler(event, ""))

          You can see that it outputs the handler content we want correctly. After the test, we can delete this file.

          image.png

          Compress index.py and 5 source code folders into the ZIP package, and then upload it to BOS.

          image.png

          Create configuration item

          We need to create the "Configuration Item" to import the function uploaded to BOS in the previous step.

          We need to set the configuration item function tag to indicate that this is a function configuration item that can be referenced by the function application.

          image.png

          Then select "Import File", select the file uploaded to BOS at just, select "Decompress", and click "OK" to complete the import operation.

          image.png

          Application Creation

          Create the function application, and select the configuration item created in the previous step.

          image.png

          Create Node

          Next, we configure the edge rule modules baetyl-broker and baetyl-rule to call the functions above.

          Create the node on the Create Node interface.

          image.png

          And bind the applications created in the previous step to this node.

          image.png

          Configure baetyl-rule

          Select the module baetyl-rule on the "Application Deployment" menu bar, and click "View".

          image.png

          Then click "Data Volume", and select the first configuration item for configuration.

          image.png

          The configuration of baetyl-rule is shown as follows:

          rules:
            - name: rule1
              source:
                topic: broker/topic1
              target:
                topic: broker/topic2
              function:
                name: bie-python/process
          
          logger:
            level: debug
            encoding: console

          The full path of function is [Service Name/Function Entry] and the full call path of python function in the above process is bie-python/process.

          Configure baetyl-broker

          Configure the baetyl-broker by follow the steps above. The configuration files are shown as follows:

          listeners:
            - address: tcp://0.0.0.0:8004
          principals:
            - username: test
              password: hahaha
              permissions:
                - action: pub
                  permit: ["#"]
                - action: sub
                  permit: ["#"]
          
          logger:
            level: info

          Add the port 8004 on the Service Configuration interface of baetyl-broekr, and expose the service to the host.

          image.png

          After successful configuration, we can see the following phenomena when kubectl is executed on the end side.

          image.png

          End side verification

          After the above services are deployed to the end side, we send the message to the baetyl-broker with the MQTT Box for testing.

          Firstly, we connect it with the MQTT Box, and the connection information is shown as follows:

          image.png

          After successful connection, it is shown as follows:

          image.png

          Our input case is the {"action":"A"} result as shown in the figure below, and the output result is the header content of the request <25>https: //cloud.baidu.com/</25>.

          image.png

          Previous
          Configure Several Functions in CFC and Distribute Them to Edge Node
          Next
          Acquire Temperature and Humudity Sensor Using Modbus