AttibuteError: binary_message has no attribute "context" in greengrass IPC V2

0

Hi,

I am getting an error using the stream handler example for local greengrass IPC V2 messaging.

The error is on the receiving message:

Attribute_Error: 'Binary_Message' object has no attribute 'context'

Thanks for your help!

asked 2 years ago240 views
3 Answers
0

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

AWS
EXPERT
answered 2 years ago
  • 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.

0

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)
answered 2 years ago
0

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}
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions