Common driver handler for PPH-hub
Project description
hub-driver-handler Package
standard file structure
+ drivers/
+ __init__.py
+ requirements.txt
+ DRIVER_ID/
+ __init__.py
+ handler.py
+ ANYFILES...
+ DRIVER_ID2/...
+ .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.
- environment variable 'LOG_LEVEL' can change the default log level 'INFO'.
drivers/__init__.py
import drivers.{DRIVER_ID}.command
...
- add each drivers for import.
handler.py
class DRIVER_ID(object):
def __init__(self, event):
# store required data from event
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def __del__(self):
pass
def get_status(self):
...
return result
def ANY_COMMAND(self):
...
return result
...
- implement your driver code in this file.
- event["request"]["command_id"] will be invoked.
.dockerignore
**/__pycache__
**/.pytest_cache
*.pyc
- this is the best practice.
app.py
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)
-
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 pph on error, set 'invoke_on_error_only' to True
-
SQS paylod by lambda trigger is also supported.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hub_driver_handler-0.0.12.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ac5c5a6dc509e3ebc366894b6805d9d33bf093a9350a84cf5ef071f86551524 |
|
MD5 | 2390f6be5c0d50346595a00ab0351dce |
|
BLAKE2b-256 | 6bb5c42f3ed3b0a79a29310c41c9bdaf30ffd0f1079a4ecd46dc9ca017cd225d |
Hashes for hub_driver_handler-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38fda0c9fae2d6ce8da3248f314e3619d62bb0eb6cccc21381c6689755c7aea2 |
|
MD5 | 6ae26bb5c57ff7f9fc9dc4a8757ac634 |
|
BLAKE2b-256 | 8ec4aaa1ff36869818f3d052f01390d0a685fba8316a80db497843ae487e3297 |