From 6d70d890ea4a400c5209df4730cd9dfe327154c3 Mon Sep 17 00:00:00 2001 From: Frantisek Farkas Date: Wed, 7 Feb 2024 13:52:50 +0100 Subject: [PATCH 1/2] chore: update publisher.py to fire messages --- samples/snippets/publisher.py | 37 +++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/samples/snippets/publisher.py b/samples/snippets/publisher.py index e2c63556c..58ac63337 100644 --- a/samples/snippets/publisher.py +++ b/samples/snippets/publisher.py @@ -22,6 +22,7 @@ """ import argparse +import json def list_topics(project_id: str) -> None: @@ -93,14 +94,42 @@ def publish_messages(project_id: str, topic_id: str) -> None: # in the form `projects/{project_id}/topics/{topic_id}` topic_path = publisher.topic_path(project_id, topic_id) + # for n in range(1, 10): + # data_str = f"Message number {n}" + # # Data must be a bytestring + # data = data_str.encode("utf-8") + # # When you publish a message, the client returns a future. + # future = publisher.publish(topic_path, data) + # print(future.result()) + for n in range(1, 10): - data_str = f"Message number {n}" - # Data must be a bytestring - data = data_str.encode("utf-8") + # Create a message payload that adheres to your activityDetectedEvent schema + message_payload = { + "event_id": str(n), # Replace with a unique event ID + "device_adapter_type": "adapter_type", + "device_adapter_id": "adapter_id", + "device_id": "device_id_123", + "reason_type": "activity_detected", + "registered_objects": [ + { + "entity": "object_1", + "attributes": ["attr_1", "attr_2"] + }, + { + "entity": "object_2", + "attributes": ["attr_3", "attr_4"] + } + ] + } + + # Serialize the message payload to JSON + message_data = json.dumps(message_payload).encode("utf-8") + # When you publish a message, the client returns a future. - future = publisher.publish(topic_path, data) + future = publisher.publish(topic_path, data=message_data) print(future.result()) + print(f"Published messages to {topic_path}.") # [END pubsub_quickstart_publisher] # [END pubsub_publish] From 91a98cd088d2eb0ae9c5c0e3dcce907517c83506 Mon Sep 17 00:00:00 2001 From: Frantisek Farkas Date: Wed, 7 Feb 2024 14:20:04 +0100 Subject: [PATCH 2/2] chore: update publisher.py --- samples/snippets/publisher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/snippets/publisher.py b/samples/snippets/publisher.py index 58ac63337..98ac78611 100644 --- a/samples/snippets/publisher.py +++ b/samples/snippets/publisher.py @@ -31,7 +31,8 @@ def list_topics(project_id: str) -> None: from google.cloud import pubsub_v1 # TODO(developer) - # project_id = "your-project-id" + project_id = "cuidtech-local" + topic_id = "activity_detected" publisher = pubsub_v1.PublisherClient() project_path = f"projects/{project_id}"