added commands

This commit is contained in:
Nico Melone
2021-12-08 15:59:50 -06:00
parent 47e0ac9e10
commit 904145a7b9
4 changed files with 115 additions and 0 deletions

View File

@@ -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()

View File

@@ -0,0 +1,3 @@
cbor2==4.1.2
pycomm3==1.2.1
awsiotsdk==1.7.1

View File

@@ -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"
}
]
}
]
}