Skip to main content

The adapter-python-library streamlines the development process for ClearBlade Adapters written in Python

Project description

adapter-python-library

The adapter-python-library streamlines the development process for ClearBlade Adapters written in Python. Most of the interaction with the ClearBlade Edge/Platform has been abstracted out, including Authentication, Adapter Configuration Collection, and MQTT Connection.

Usage

import time
import json
from clearblade_adapter_library import AdapterLibrary

ADAPTER_NAME = "my-new-adapter"

def main():
    # Initialize the adapter library with the adapter name
    adapter = AdapterLibrary(ADAPTER_NAME)

    # Parse command line arguments and environment variables
    adapter.parse_arguments()

    # Initialize all things ClearBlade, includes authenticating if needed, and fetching the
    # relevant adapter_config collection entry
    adapter_config = adapter.initialize_clearblade()

    # if your adapter config includes custom adapter settings, parse/validate them here

    # Connect MQTT, if your adapter needs to subscribe to a topic, provide it as the first
    # parameter, and a callback for when messages are received. If no need to subscribe,
    # simply provide an empty string and None
    # connect_MQTT will establish the MQTT connection
    # Topic structure is entirely up to your adapter's requirements
    adapter.connect_MQTT(topic=adapter_config["topic_root"] + "/commands", cb_message_handler=cb_message_handler)

    # kick off adapter specific things here

    # keep adapter executing indefinitely
    while True:
        time.sleep(1)

def cb_message_handler(client, message):
    # process incoming MQTT messages as needed here
    payload = json.loads(message.payload.decode("utf-8"))
    print("Message Received: ", str(payload))

if __name__ == "__main__":
    main()

Command Line Arguments & Environment Variables

All ClearBlade Adapters require a certain set of System specific variables to start and connect with the ClearBlade Platform/Edge. This library allows these to be passed in either by command line arguments, or environment variables. Note that command line arguments take precedence over environment variables.

Name CLI Flag Environment Variable Default
System Key --systemKey CB_SYSTEM_KEY N/A
Platform/Edge URL --platformURL N/A http://localhost:9000
Platform/Edge Messaging URL --messagingURL N/A localhost:1883
Device Service Account N/A CB_SERVICE_ACCOUNT N/A
Device Service Account Token N/A CB_SERVICE_ACCOUNT_TOKEN N/A
Log Level --logLevel N/A info
Adapter Config Collection Name --adapterConfigCollection N/A adapter_config

A System Key will always be required to start the adapter, and it's recommended to always use a Device Service Account & Token for Adapters. be used for any new adapters. If provided its value will be ignored.

Adapter Configuration & Settings

This library does require the use of an Adapter Configuration Collection. This allows you to easily provide configuration options to your adapter at runtime via this Collection, rather than command line arguments, environment variables, or hardcoded values in your adapter.

If your adapter does not have any specific settings it is still expected to have an entry in this collection, but adapter settings column can be left blank.

The default name for this Collection is adapter_config, and it's expected data structure is:

Column Name Column Type
adapter_name string
topic_root string
adapter_settings string

Logging

This adapter introduces basic logging levels that your adapter can leverage. The --logLevel flag controls the logging output of the adapter.

The supported log levels are debug, info, warn, error, and fatal.

MQTT Publishing

To publish MQTT messages from your adapter, use the publish method:

adapter.publish("topic/name", "message payload")

Fatal Errors

There are a few cases where this library will encounter a fatal error and cause your adapter to quit. A log with more information will be provided, but as a heads up these are the current cases that will cause the adapter to exit from within the library:

  1. If required arguments (System Key, authentication credentials etc.) are not provided
  2. If your adapter subscribes to a topic and the device account used by the adapter does not have subscribe permissions on this topic (a warning will be logged)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clearblade_adapter_library-1.0.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clearblade_adapter_library-1.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file clearblade_adapter_library-1.0.0.tar.gz.

File metadata

  • Download URL: clearblade_adapter_library-1.0.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.14.0-35-generic

File hashes

Hashes for clearblade_adapter_library-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8614de3e57cda9f7db730f2815ae86c143f096c94c6eee692440eef7b1ce85e8
MD5 d147fcbfd704f99fee0d4531b212e6db
BLAKE2b-256 e050ca8aefd369f8cbbf9c784d9ba2f60305349dffc2f2900486cee8798f242e

See more details on using hashes here.

File details

Details for the file clearblade_adapter_library-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for clearblade_adapter_library-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd3b7aa549415371b9f727c5312cf3917418866fae38f813589daca13e5d52b6
MD5 c7b22f57fab817b431a8362bdfe5a885
BLAKE2b-256 2eb832c55e3b3a65bb4acf02729b5b827cf3612fa67e8795c9c79e0ac19fcd04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page