- Neueste
- Die meisten Stimmen
- Die meisten Kommentare
Which version of the Nucleus and the SDKs are you using? You need to use Nucleus >=2.6.0 and SDK >1.11.3 to get the BinaryMessage.context.topic
field. If you are using an older version, you can also remove the lines from the on_stream_event
function.
The component recipe should install the latest SDK, so the issue is likely with the Nucleus version.
You can either add the Nucleus explicitly to your deployment and set the version, or you could add the following to your recipe json file:
"ComponentDependencies": {
"aws.greengrass.Nucleus": {
"VersionRequirement": ">=2.6.0"
}
}
Cheers,
Massimiliano
Thank you Massimiliano, I am running Nucleus version 2.7.0. Ill try removing some of the lines in the stream function. Not sure how I check the sdk version, I installed it a while ago.
Sure! Here is the recipe, I did not see any authorization errors.
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.gateway.mqtt",
"ComponentVersion": "1.0.50",
"ComponentType": "aws.greengrass.generic",
"ComponentDescription": "MQTT",
"ComponentPublisher": "Mike Ratterman - RAT",
"ComponentConfiguration": {
"DefaultConfiguration": {
"accessControl": {
"aws.greengrass.ipc.pubsub": {
"com.gateway.mqtt:pubsub:1": {
"policyDescription": "Allows access to publish/subscribe to all topics.",
"operations": [
"aws.greengrass#PublishToTopic",
"aws.greengrass#SubscribeToTopic"
],
"resources": [
"*"
]
}
},
"aws.greengrass.ipc.mqttproxy": {
"com.gateway.mqtt:pubsub:mqttproxy:1": {
"policyDescription": "Allows access to publish/subscribe to all MQTT topics.",
"operations": [
"aws.greengrass#PublishToIoTCore",
"aws.greengrass#SubscribeToIoTCore"
],
"resources": [
"*"
]
}
}
},
"Message": "variable to pass"
}
},
"Manifests": [
{
"Platform": {
"os": "*"
},
"Name": "All",
"Lifecycle": {
"Install": {
"RequiresPrivilege": true,
"script": "pip3 install --user pyserial && pip3 install --user awsiotsdk"
},
"Run": {
"RequiresPrivilege": true,
"script": "python -u {artifacts:path}/mqtt.py '{configuration:/Message}'"
}
},
"Artifacts": [
{
"Uri": "s3://peamans-components/artifacts/com.gateway.mqtt/1.0.50/mqtt.py",
"Digest": "NHZfbjQh506Snaujb369+sXipgUTm+QHh12aalVuHtI=",
"Algorithm": "SHA-256",
"Unarchive": "NONE",
"Permission": {
"Read": "OWNER",
"Execute": "NONE"
}
}
]
}
],
"Lifecycle": {}
}
Also here is the code I am testing:
def on_stream_event(event: SubscriptionResponseMessage) -> None:
try:
message = str(event.binary_message.message, 'utf-8')
print("message:", message)
topic = event.binary_message.context.topic
print('Received new message on topic %s: %s' % (topic, message))
except:
traceback.print_exc()
# MQTT Bridge IPC connect
if Enable_GG:
qos = QOS.AT_LEAST_ONCE
TIMEOUT = 10
print("Connecting...")
try:
ipc_client = GreengrassCoreIPCClientV2()
# Use IPC client.
except Exception:
print('Exception occurred when using IPC.', file=sys.stderr)
traceback.print_exc()
exit(1)
## Modbus
modbus_topic = "modbus/request/PacketPower-41E4-0000-0000-0B23"
modbus_receive_topic = "modbus/response/PacketPower-41E4-0000-0000-0B23"
try:
_, operation = ipc_client.subscribe_to_topic(topic=modbus_receive_topic,
on_stream_event=on_stream_event,
on_stream_error=on_stream_error,
on_stream_closed=on_stream_closed)
print('Successfully subscribed to topic: ' + modbus_receive_topic)
except UnauthorizedError:
print('Unauthorized error while subscribing to topic: ' +
modbus_receive_topic, file=sys.stderr)
traceback.print_exc()
exit(1)
except Exception:
print('Exception occurred when subscribing to local topic.', file=sys.stderr)
traceback.print_exc()
exit(1)
message = {
"id": "TestRequest",
"function": "ReadInputRegisters",
"address": 0x0257,
"quantity": 0x00
}
message = json.dumps(message)
try:
publish_binary_message_to_topic(ipc_client, modbus_topic, message)
print('Successfully published to topic: ' + modbus_topic)
except Exception:
print('Exception occurred when using IPC.', file=sys.stderr)
traceback.print_exc()
exit(1)
I think I see the issue. I am sending IPC messages to a modbus device which is not returning the topic name, just the message.
2022-10-07T16:05:25.141Z [INFO] (Copier) com.gateway.mqtt: stdout. Successfully published to topic: modbus/request/PacketPower-41E4-0000-0000-0B23. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [INFO] (Copier) com.gateway.mqtt: stdout. message: {"type":"ReadInputRegisters","id":"TestRequest","bytes":[]}. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [INFO] (Copier) com.gateway.mqtt: stdout. event.binary_message: BinaryMessage(message=b'{"type":"ReadInputRegisters","id":"TestRequest","bytes":[]}'). {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [WARN] (Copier) com.gateway.mqtt: stderr. Traceback (most recent call last):. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [WARN] (Copier) com.gateway.mqtt: stderr. File "C:\greengrass\v2\packages\artifacts\com.gateway.mqtt\1.0.50\mqtt.py", line 36, in on_stream_event. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [WARN] (Copier) com.gateway.mqtt: stderr. topic = event.binary_message.context.topic. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
2022-10-07T16:05:25.172Z [WARN] (Copier) com.gateway.mqtt: stderr. AttributeError: 'BinaryMessage' object has no attribute 'context'. {scriptName=services.com.gateway.mqtt.lifecycle.Run.script, serviceName=com.gateway.mqtt, currentState=RUNNING}
Relevanter Inhalt
- AWS OFFICIALAktualisiert vor 2 Jahren
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor 10 Monaten
- AWS OFFICIALAktualisiert vor 3 Jahren
Can you please update your post with logs and the recipe you're using? Additionally, can you link to the example you're referring to so there's no confusion? Thanks!
As a sanity check, did you set an authorization policy in your recipe to allow IPC operations? e.g. https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-publish-subscribe.html#ipc-publish-subscribe-authorization-policy-examples
Are there any other errors in greengrass.log or your component log?