Skip to main content

Common driver handler for PPH

Project description

hub-driver-handler Package

standard file structure

+ drivers/
   + DRIVER_ID/
      + __init__.py
      + command.py
      + ANYFILES...
   + DRIVER_ID2/...
   + schema/
   + __init__.py
   + requirements.txt
+ .dockerignore
+ app.py
+ Dockerfile
  • drivers/DRIVER_ID/__init__.py is required and might be empty.
  • environment variable 'HUB_FUNCTION_NAME' is required to notify the results to the IoT-hub.
  • if 'HUB_FUNCTION_NAME' is not contains alias/version and your function invoked with alias/version, this will be added to invoke.
  • environment variable 'LOG_LEVEL' can change the default log level 'INFO'.

drivers/__init__.py

import drivers.{DRIVER_ID}.command
import drivers.{DRIVER_ID2}.command
...
  • add all drivers for import.

command.py

class handler(object):
    def __init__(self, event):
        # store required data from event value
    
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

    def __del__(self):
        pass

    def ANY_COMMAND(self):
        ...
        return result
    ...
  • implement your driver code in this file.
  • class name must be 'handler'.
  • event["request"]["command_id"] will be executed.

.dockerignore

**/__pycache__
**/.pytest_cache
*.pyc
  • this is the best practice.

app.py

from hub_driver_handler.handler import handler

def lambda_handler(event, context):
    return handler(event, context, result_root_key = None, post_function = None, invoke_on_error_only = False, validate_schema = True)
  • driver_id will be determined as below sequence.
    1. event.get('driver_id')
    2. os.environ.get('DRIVER_NAME')
    3. context.function_name

[previous format]

import os
from hub_driver_handler.handler import handler

def lambda_handler(event, context):
    driver_id = event.get('driver_id') or os.environ.get('DRIVER_NAME') or context.function_name
    return handler(event, driver_id, result_root_key = None, post_function = None, invoke_on_error_only = False, validate_schema = True)
  • this code expected 'driver_id' will be passed from the IoT-hub for multiple drivers support, but currently not.

  • set enviroment variable 'DRIVER_NAME' explicitly or set lambda function name as driver_id for now.

  • set 'result_root_key' if you want to add root key for results, ex: 'result_params'

  • 'date_time' and 'result_id' will be added if not present.

  • 'post_function' can modify the results from each driver as below:

    def post_function(result, event): result['cmd'] = event['request']['command_id'] ...

  • If only send the result to IoT-hub on error, set 'invoke_on_error_only' to True

  • SQS paylod by lambda trigger is also supported.

  • event data will be validated by JSON Schema file in 'drivers/DRIVER_ID/schema' folder, set 'validate_schema' to False if validation is not needed.

Dockerfile

FROM public.ecr.aws/lambda/python:3

COPY app.py ./
COPY drivers/ ./drivers

RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install hub-driver-handler -t .
RUN python3 -m pip install -r drivers/requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

requirements.txt

  • add required packages for all drivers

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

hub_driver_handler-0.0.14.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

hub_driver_handler-0.0.14-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file hub_driver_handler-0.0.14.tar.gz.

File metadata

  • Download URL: hub_driver_handler-0.0.14.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for hub_driver_handler-0.0.14.tar.gz
Algorithm Hash digest
SHA256 80b9f81ff77f012aee65a59f8660458e3f894e8c8aef4865fdce9458b88fbb11
MD5 d80a4f8993388d4c5b30f566f9102bac
BLAKE2b-256 34bd2d2552e7af485c19bc334e7869560854fc6cdcf9fc9b919a0aebd104c749

See more details on using hashes here.

File details

Details for the file hub_driver_handler-0.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for hub_driver_handler-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 88512cee0f717883a9fc16bcbce164aa38fee134ac3203bf2fe949224b78de26
MD5 a2c4053f03aeed0e86a6c0f33facf430
BLAKE2b-256 0d974e840389ab055ca41a560577855efc7d6e26a4e410d19d31f8065459b4b0

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