https://github.com/awslabs/aws-greengrass-labs-iot-pubsub-sdk-for-python

AWS IoT Greengrass PubSub SDK for Python

https://github.com/awslabs/aws-greengrass-labs-iot-pubsub-sdk-for-python

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.8%) to scientific vocabulary

Keywords

aws event-driven greengrassv2 iot mqtt pubsub sdk sdk-python
Last synced: 5 months ago · JSON representation

Repository

AWS IoT Greengrass PubSub SDK for Python

Basic Info
  • Host: GitHub
  • Owner: awslabs
  • License: mit-0
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 6.35 MB
Statistics
  • Stars: 11
  • Watchers: 13
  • Forks: 5
  • Open Issues: 0
  • Releases: 1
Topics
aws event-driven greengrassv2 iot mqtt pubsub sdk sdk-python
Created over 4 years ago · Last pushed over 2 years ago
Metadata Files
Readme Contributing License Code of conduct

README.md

AWS IoT Greengrass PubSub SDK for Python

This document provides information about the AWS IoT Greengrass PubSub SDK for Python.

If you have any issues or feature requests, please file an issue or pull request.

Note: This is a library based implementation and replacement for the AWS IoT Greengrass PubSub Framework previoulsy hosted in this repository.

Overview

The AWS IoT Greengrass PubSub SDK for Python provides an IoT PubSub application architecture delivered as a Python library to simplify and accelerate development of distributed IoT applications built on AWS IoT Greengrass Components. The SDK abstracts the AWS IoT Greengrass IPC and MQTT PubSub functionality and uses a data driven message format to route PubSub messages to user defined call-backs. This provides a Low/No-Code PubSub messaging service without the common design dependencies of distributed IoT applications.

pubsub-sdk-overview

For more details see the AWS IoT Greengrass PubSub SDK: * Developer Guide * API Docs * Samples * Detailed Deployment Workshop

Getting Started

The easiest way to get started is to build and deploy the AWS IoT Greengrass PubSub SDK component template described in the Samples directory.

For new users just starting out with AWS IoT Greengrass or those that want to see a more detailed example of using the SDK to deploy multiple components that we recommend running through the workshop Build a Distributed IoT Application with the AWS IoT Greengrass PubSub SDK where you build a Smart Factory IoT application using the AWS IoT Greengrass PubSub SDK.

Installation

Minimum Requirements

  • Python 3.6+

Install via an AWS IoT Greengrass Custom Component Recipe

To configure the AWS IoT Greengrass PubSub SDK to deploy with an AWS IoT Greengrass Component, update the recipe Lifecycle / Install event as per below: "Lifecycle": { "Install" : { "Timeout" : 300, "Script" : "python3 -m pip install awsgreengrasspubsubsdk" }, .....

A full example of this is given in the Greengrass component recipe example

Installing Manually

The AWS IoT Greengrass PubSub SDK provides functionality within an AWS IoT Greengrass component. It only has context within that framework and so for production solutions, its preferred to deploy the SDK with the component as described in the previous example. Manual installation examples given below are intended for development machines.

Note: If installing manually, do so as the user that will own the Greengrass component process (i.e: gcc_user by default).

Install from PyPI

python3 -m pip install awsgreengrasspubsubsdk

Install from source

git clone https://github.com/awslabs/aws-greengrass-labs-iot-pubsub-sdk-for-python.git python3 -m pip install ./aws-greengrass-pubsub-sdk-for-python

Usage and Code Examples

Once the AWS IoT Greengrass PubSub SDK is deployed with an AWS IoT Greengrass Component, you can route messages to your application logic via user defined Message Handlers that you register with the SDK

A production ready AWS IoT Greengrass component template and more detailed examples are provided in the Samples directory.

The SDK defines the following required message format (with more details provided in the Developer Guide { "sdk_version": "0.1.4", "message_id": "20220403170948930231", "status": 200, "route": "MyPubSubMessageHandler.pubsub_message_route_target", "message": { "my-message-param01": "param01", "my-message-param02": "param02" } } The SDK will route received PubSub messages to a user defined message handler class and method as provided in the message route value. The message route value is a Class.Method namespace representation of the desired message route target.

Using this approach, the AWS IoT Greengrass PubSub SDK routes PubSub messages to your application logic in easily defined message callbacks.

Initialising the SDK and Message Handler Classes

  1. Add a method that will be the route of last resort for received PubSub messages and the expected message parameters. ``` def defaultmessagehandler(self, protocol, topic, message_id, status, route, message):

# Process messages without a registered handler router target log.error('Received message to unknown route / message handler: {} - message: {}'.format(route, message))) ```

  1. Initialise the AWS IoT Greengrass PubSub SDK Client ``` # Import the AWS IoT Greengrass PubSub SDK from awsgreengrasspubsubsdk.pubsub_client import AwsGreengrassPubSubSdkClient

Declare the PubSub Base topic, this is used to build the default Ingress and Egress PubSub topics.

pubsubbasetopic = 'com.my_greengrass.application'

Initilise the AwsGreengrassPubSubSdkClient with the pubsubbasetopic and defaultmessagehandler

pubsubclient = AwsGreengrassPubSubSdkClient(pubsubbasetopic, defaultmessage_handler )

```

  1. Create one or more user defined message handler class/es with named functions to route PubSub messages with the expected parameters. This method allows easy separation of message processing functionality within your application. ``` class MyPubSubMessageHandler():

    def pubsubmessageroutetarget(self, protocol, topic, messageid, status, route, message):

    # Process messages with route = 'MyPubSubMessageHandler.pubsub_message_route_target'
    log.info('MyPubSubMessageHandler.pubsub_message_route_target received message: {}'.format(message))
    

    ```

  2. Register the message handler class/es with the PubSub SDK Client with the registermessagehandler call. my_pubsub_message_handler = MyPubSubMessageHandler() pubsub_client.register_message_handler(my_pubsub_message_handler)

  3. Activate the IPC and / or MQTT Protocols in the SDK: ```

    Activate IPC Protocol

    pubsubclient.activateipc_pubsub()

Acticate MQTT Protocol

pubsubclient.activatemqtt_pubsub() ```

  1. Complete!
    On completion of the above and once successfully deployed, your AWS IoT Greengrass component will listen on the default Ingress topic (base-pubsub-topic/THING_NAME/ingress) and any other user defined topics. The SDK will begin routing PubSub messages to your registered Message Handler classes as per the message route value. As describe above, any message received on the SDK subscribed topic with value route = MyPubSubMessageHandler.pubsubmessageroute_target will be automatically forwarded to the method provided in the example.

Note: If the route value does not match any route target methods, the message will be forwarded to the defaultmessagehandler class.

Subscribing to Custom Topics

The SDK provides a programmatic means of subscribing to user defined topics. You can subscribe to any topic that is permitted by the access policy provided in the component recipe. ``` my_topic = 'my/interesting/topic'

Subscribe to topic on IPC

pubsubclient.subscribeto_topic('ipc', topic)

Subscribe to topic on MQTT

pubsubclient.subscribeto_topic('mqtt', topic)

Subscribe to topic on IPC and MQTT

pubsubclient.subscribetotopic('ipcmqtt', topic)

```

If the protocol (IPC or MQTT) is activated, the SDK will subscribe to the topic and begin routig messages immediatly. If not, the subscription request will be stored and actioned when the selected protocol is activated.

Publishing Message to PubSub

The SDK provides a message formatter class to ensure consistent messages. See the message_formatter API Docs for more detail.

```

Initialise the PubSubMessageFormatter

from awsgreengrasspubsubsdk.messageformatter import PubSubMessageFormatter messageformatter = PubSubMessageFormatter()

Use the message formatter to create a well-formatted PubSub SDK response message

receiveroute = 'MyPubSubMessageHandler.pubsubmessageresponse' mymessage = { "my-message-param01": "param01", "my-message-param02": "param02" }

Defaults will be applied for parameteres not provided here. See API Docs.

sdkformatmsg = messageformatter.getmessage(route=receiveroute, message=mymessage)

Publish the message to IPC and MQTT.

pubsubclient.publishmessage('ipcmqtt', sdkformat_msg) ```

Installation Issues

  1. The AWS IoT Greengrass PubSub SDK (awsgreengrasspubsubsdk) installs awsiotsdk as a dependancy with the following listed Installation issues.

  2. If running the AWS IoT Greengrass component with root privileges, you will need to install in a python virtual environment by replacing the component recipe install policy with the below: "Install" : { "Timeout" : 300, "Script" : "python3 -m venv pubsubsdk; . pubsubsdk/bin/activate; pip3 install --upgrade pip; python3 -m pip install awsgreengrasspubsubsdk" }

Samples

A complete production ready AWS IoT Greengrass component template is provided in the Samples directory.

Getting Help

The best way to interact with our team is through GitHub. You can open an issue for guidance, bug reports, or feature requests.

Please make sure to check out our resources before opening an issue: * Developer Guide * API Docs * AWS IoT Greengrass PubSub SDK Deployment Workshop * AWS IoT Greengrass V2 Developer Guide * AWS IoT Core Documentation * AWS Dev Blog

Giving Feedback and Contributions

We need your help in making this SDK great. Please participate in the community and contribute to this effort by submitting issues, participating in discussion forums and submitting pull requests through the following channels.

AWS IoT Core Resources

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Owner

  • Name: Amazon Web Services - Labs
  • Login: awslabs
  • Kind: organization
  • Location: Seattle, WA

AWS Labs

GitHub Events

Total
Last Year

Committers

Last synced: 7 months ago

All Time
  • Total Commits: 17
  • Total Committers: 2
  • Avg Commits per committer: 8.5
  • Development Distribution Score (DDS): 0.059
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
dcolcott 5****t 16
Amazon GitHub Automation 5****o 1

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 2
  • Total pull requests: 0
  • Average time to close issues: 3 months
  • Average time to close pull requests: N/A
  • Total issue authors: 2
  • Total pull request authors: 0
  • Average comments per issue: 3.5
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • charlieharris1 (1)
  • dflatow (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 1,981 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 6
  • Total maintainers: 1
pypi.org: awsgreengrasspubsubsdk

AWS Greengrass IoT Pubsub SDK for Python

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 1,981 Last month
Rankings
Dependent packages count: 10.1%
Downloads: 12.0%
Forks count: 14.2%
Stargazers count: 19.4%
Average: 24.6%
Dependent repos count: 67.1%
Maintainers (1)
Last synced: 6 months ago

Dependencies

setup.py pypi
  • awsiotsdk *