1717#
1818# This affects the generated template and the message type that will be published.
1919# Make sure it matches the actual message type you want to use.
20- from common_msgs .control_msgs .control_cmd_pb2 import ControlCommand as MessageType
20+
21+ from cyber .proto .unit_test_pb2 import ChatterBenchmark as MessageType
2122
2223# ========================================================
2324
2829)
2930
3031
32+ def fill_header (msg : Message ):
33+ """
34+ Fill the header fields of the message with default values.
35+ This is a placeholder function and should be customized based on your message type.
36+ """
37+ if hasattr (msg , "header" ):
38+ msg .header .timestamp_sec = time .time ()
39+ msg .header .sequence_num += 1
40+
41+
3142class ProtoTemplateGenerator :
3243 def __init__ (self , message_type : Type [Message ]):
3344 self .message_type = message_type
@@ -38,7 +49,7 @@ def generate_template(self, output_filepath: str):
3849 self ._fill_template_recursive (msg_instance )
3950
4051 template_str = text_format .MessageToString (
41- msg_instance , as_utf8 = True , indent = 2 , as_one_line = False
52+ msg_instance , as_utf8 = True , indent = 0 , as_one_line = False
4253 )
4354
4455 preamble = f"""# Protobuf Text Format Template for message: { self .message_type .DESCRIPTOR .full_name }
@@ -93,9 +104,8 @@ def _fill_template_recursive(self, msg_instance: Message):
93104 self ._get_placeholder_for_primitive (field .type )
94105 )
95106 elif field .type == descriptor .FieldDescriptor .TYPE_MESSAGE :
96- nested_msg = field .message_type ._concrete_class ()
97- self ._fill_template_recursive (nested_msg )
98- setattr (msg_instance , field .name , nested_msg )
107+ nested_instance = getattr (msg_instance , field .name )
108+ self ._fill_template_recursive (nested_instance )
99109 elif field .type == descriptor .FieldDescriptor .TYPE_ENUM :
100110 enum_desc = field .enum_type
101111 first_enum_value = enum_desc .values [0 ].number if enum_desc .values else 0
@@ -112,6 +122,8 @@ def _get_placeholder_for_primitive(self, field_type: int) -> Any:
112122
113123 if field_type == descriptor .FieldDescriptor .TYPE_STRING :
114124 return "PLACEHOLDER_STRING"
125+ elif field_type == descriptor .FieldDescriptor .TYPE_BYTES :
126+ return b""
115127 elif field_type in (
116128 descriptor .FieldDescriptor .TYPE_INT32 ,
117129 descriptor .FieldDescriptor .TYPE_INT64 ,
@@ -187,10 +199,12 @@ def publish_message(
187199 i , _ , _ = select .select ([sys .stdin ], [], [], None )
188200 if i :
189201 input_line = sys .stdin .readline ()
202+ fill_header (msg_to_publish )
190203 writer .write (msg_to_publish )
191204 logging .info (f"Topic '{ topic_name } ' message published." )
192205 else :
193206 while not cyber .is_shutdown ():
207+ fill_header (msg_to_publish )
194208 writer .write (msg_to_publish )
195209 if period > 0 :
196210 time .sleep (period )
0 commit comments