Table of Contents
uProtocol CloudEvents is a common message envelope that could be used to carry way to represent uProtocol transport layer information UUri
(source), UPayload
, and UAttributes
. CloudEvents
are used by a number of Device-2-Cloud and Cloud-2-Device based transports such as MQTT and HTTP, however it could also be used by any transport (ex. Binder).
uri = UUri(entity=UEntity(name="body.access"),
resource=UResource(name="door", instance="front_left", message="Door"))
source= LongUriSerializer().serialize(uri)
ce_proto = CloudEvent(spec_version="1.0", source="https://example.com", id="hello", type="example.demo",
proto_data=any_pb2.Any())
any_obj = any_pb2.Any()
any_obj.Pack(ce_proto)
proto_payload = any_obj
u_cloud_event_attributes = UCloudEventAttributesBuilder().with_hash("somehash").with_priority(
UPriority.UPRIORITY_CS1).with_ttl(3).build()
cloud_event = CloudEventFactory.publish(source, proto_payload, u_cloud_event_attributes)
# test all attributes
assertEqual("1.0", UCloudEvent.get_specversion(cloud_event))
assertIsNotNone(UCloudEvent.get_id(cloud_event))
assertEqual(source, UCloudEvent.get_source(cloud_event))
assertEqual(UCloudEvent.get_event_type(UMessageType.UMESSAGE_TYPE_PUBLISH),
UCloudEvent.get_type(cloud_event))
assertNotIn("sink", cloud_event.get_attributes())
assertEqual("somehash", UCloudEvent.get_hash(cloud_event))
assertEqual(UPriority.Name(UPriority.UPRIORITY_CS1), UCloudEvent.get_priority(cloud_event))
assertEqual(3, UCloudEvent.get_ttl(cloud_event))
assertEqual(proto_payload.SerializeToString(), cloud_event.get_data())