diff --git a/Commands/artifacts/hp.greengrass.Commands/1.0.0/Commands.py b/Commands/artifacts/hp.greengrass.Commands/1.0.0/Commands.py new file mode 100644 index 0000000..ada56e4 --- /dev/null +++ b/Commands/artifacts/hp.greengrass.Commands/1.0.0/Commands.py @@ -0,0 +1,65 @@ +import time +import traceback +import json +import awsiot.greengrasscoreipc +import awsiot.greengrasscoreipc.client as client +from awsiot.greengrasscoreipc.model import ( + IoTCoreMessage, + QOS, + SubscribeToIoTCoreRequest +) + +TIMEOUT = 10 + +ipc_client = awsiot.greengrasscoreipc.connect() + +def start(): + print("START START START") + +def stop(): + print("STOP STOP STOP") + +class StreamHandler(client.SubscribeToIoTCoreStreamHandler): + def __init__(self): + super().__init__() + + def on_stream_event(self, event: IoTCoreMessage) -> None: + try: + message = json.loads(str(event.message.payload, "utf-8")) + topic_name = event.message.topic_name + # Handle message. + operations = { + "start": start, + "stop": stop + } + print(f"running operation {message['operations']}") + operations[message["operations"]] + except: + traceback.print_exc() + + def on_stream_error(self, error: Exception) -> bool: + # Handle error. + return True # Return True to close stream, False to keep stream open. + + def on_stream_closed(self) -> None: + # Handle close. + pass + + +topic = "my/topic" +qos = QOS.AT_MOST_ONCE + +request = SubscribeToIoTCoreRequest() +request.topic_name = topic +request.qos = qos +handler = StreamHandler() +operation = ipc_client.new_subscribe_to_iot_core(handler) +future = operation.activate(request) +future.result(TIMEOUT) + +# Keep the main thread alive, or the process will exit. +while True: + time.sleep(10) + +# To stop subscribing, close the operation stream. +operation.close() \ No newline at end of file diff --git a/Commands/artifacts/hp.greengrass.Commands/1.0.0/requirements.txt b/Commands/artifacts/hp.greengrass.Commands/1.0.0/requirements.txt new file mode 100644 index 0000000..2020512 --- /dev/null +++ b/Commands/artifacts/hp.greengrass.Commands/1.0.0/requirements.txt @@ -0,0 +1,3 @@ +cbor2==4.1.2 +pycomm3==1.2.1 +awsiotsdk==1.7.1 diff --git a/Commands/artifacts/hp.greengrass.Commands/1.0.0/stream_manager_sdk.zip b/Commands/artifacts/hp.greengrass.Commands/1.0.0/stream_manager_sdk.zip new file mode 100644 index 0000000..1d75c71 Binary files /dev/null and b/Commands/artifacts/hp.greengrass.Commands/1.0.0/stream_manager_sdk.zip differ diff --git a/Commands/recipes/hp.greengrass.Commands.json b/Commands/recipes/hp.greengrass.Commands.json new file mode 100644 index 0000000..1f5f2da --- /dev/null +++ b/Commands/recipes/hp.greengrass.Commands.json @@ -0,0 +1,47 @@ +{ + "RecipeFormatVersion": "2020-01-25", + "ComponentName": "hp.greengrass.Commands", + "ComponentVersion": "1.0.0", + "ComponentDescription": "A AWS IOT Greengrass component for sending commands to devices", + "ComponentPublisher": "HenryPump", + "ComponentConfiguration": { + "DefaultConfiguration": { + "accessControl": { + "aws.greengrass.ipc.mqttproxy": { + "hp.greengrass.Commands:mqttproxy:1": { + "policyDescription": "Allows access to subscribe to all topics.", + "operations": [ + "aws.greengrass#SubscribeToIoTCore" + ], + "resources": [ + "*" + ] + } + } + } + } + }, + "Manifests": [ + { + "Platform": { + "os": "linux" + }, + "Lifecycle": { + "Install": "pip3 install -r {artifacts:path}/requirements.txt", + "Run": "python3 -u {artifacts:path}/Commands.py" + }, + "Artifacts": [ + { + "URI": "s3://hp-greengrass-components/artifacts/hp.greengrass.Commands/1.0.0/Commands.py" + }, + { + "URI": "s3://hp-greengrass-components/artifacts/hp.greengrass.Commands/1.0.0/stream_manager_sdk.zip", + "Unarchive": "ZIP" + }, + { + "URI": "s3://hp-greengrass-components/artifacts/hp.greengrass.Commands/1.0.0/requirements.txt" + } + ] + } + ] +} \ No newline at end of file