-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5805da8
commit 27e3197
Showing
2 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Any | ||
from abc import ABC, abstractmethod | ||
|
||
from rclpy.serialization import serialize_message, deserialize_message | ||
|
||
class Serializer(ABC): | ||
"""Serializes and deserializes ROS messages for transmission over MQTT.""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def serialize(message: Any) -> bytes: | ||
""" | ||
Serialize the provided ROS message to a bytes for MQTT. | ||
Parameters | ||
---------- | ||
message : Any | ||
the ROS message to serialize | ||
Returns | ||
------- | ||
bytes | ||
the bytes formed by serializing the ROS message | ||
""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def deserialize(serialized_message: bytes, message_type: Any) -> Any: | ||
""" | ||
Deserialize the provided bytes into a ROS message of the provided type. | ||
Parameters | ||
---------- | ||
serialized_message : bytes | ||
the bytes generated by serializing a ROS message | ||
message_type : Any | ||
the type of the message to create | ||
Returns | ||
------- | ||
Any | ||
the ROS message formed by deserializing the bytes | ||
""" | ||
|
||
|
||
class ROSDefaultSerializer(Serializer): | ||
"""Serialize and deserialize messages using the default ROS message serializer.""" | ||
|
||
@staticmethod | ||
def serialize(message: Any) -> bytes: | ||
return serialize_message(message) | ||
|
||
@staticmethod | ||
def deserialize(serialized_message: bytes, message_type: Any) -> Any: | ||
return deserialize_message(serialized_message, message_type) |