Subscribe a message from a channel. Args: subscriber (aimrt_python_runtime.SubscriberRef): channel subscriber msg_type: protobuf message type or ROS2 message type callback (Callable): callback function Raises: ValueError: if the callback is invalid T
(subscriber: aimrt_python_runtime.SubscriberRef,
msg_type: google._upb._message.MessageMeta | Ros2MsgType,
callback: Callable)
| 147 | |
| 148 | |
| 149 | def Subscribe(subscriber: aimrt_python_runtime.SubscriberRef, |
| 150 | msg_type: google._upb._message.MessageMeta | Ros2MsgType, |
| 151 | callback: Callable): |
| 152 | """Subscribe a message from a channel. |
| 153 | |
| 154 | Args: |
| 155 | subscriber (aimrt_python_runtime.SubscriberRef): channel subscriber |
| 156 | msg_type: protobuf message type or ROS2 message type |
| 157 | callback (Callable): callback function |
| 158 | |
| 159 | Raises: |
| 160 | ValueError: if the callback is invalid |
| 161 | TypeError: if the message type is invalid |
| 162 | |
| 163 | Callback function signature (both for Protobuf and ROS2 messages): |
| 164 | - callback(msg) |
| 165 | - callback(ctx, msg) |
| 166 | """ |
| 167 | # Check callback signature |
| 168 | sig = inspect.signature(callback) |
| 169 | required_param_count = sum(1 for param in sig.parameters.values() if param.default == param.empty) |
| 170 | |
| 171 | if not (1 <= required_param_count <= 2): |
| 172 | raise ValueError("Invalid callback: expected 1 or 2 parameters, with at most one optional parameter") |
| 173 | |
| 174 | if isinstance(msg_type, google._upb._message.MessageMeta): |
| 175 | py_pb_ts = aimrt_python_runtime.PyPbTypeSupport() |
| 176 | py_pb_ts.SetTypeName(GetPbMessageTypeName(msg_type)) |
| 177 | py_pb_ts.SetSerializationTypesSupportedList(["pb", "json"]) |
| 178 | |
| 179 | def handle_callback(ctx_ref: aimrt_python_runtime.ContextRef, msg_buf: bytes): |
| 180 | try: |
| 181 | msg = _DeserializeProtobufMessage(msg_buf, ctx_ref.GetSerializationType(), msg_type) |
| 182 | if required_param_count == 1: |
| 183 | callback(msg) |
| 184 | else: |
| 185 | callback(ctx_ref, msg) |
| 186 | except Exception as e: |
| 187 | print(f"AimRT channel handle get exception, {e}", file=sys.stderr) |
| 188 | |
| 189 | subscriber.PbSubscribeWithCtx(py_pb_ts, handle_callback) |
| 190 | |
| 191 | elif check_is_valid_ros2_msg_type(msg_type): |
| 192 | from . import aimrt_python_runtime_ros2 as aimrt_py_ros2 |
| 193 | py_ros2_ts = aimrt_py_ros2.PyRos2TypeSupport(msg_type) |
| 194 | py_ros2_ts.SetTypeName(GetRos2MessageTypeName(msg_type)) |
| 195 | py_ros2_ts.SetSerializationTypesSupportedList(["ros2"]) |
| 196 | |
| 197 | def ros2_callback_wrapper(ctx_ref: aimrt_python_runtime.ContextRef, msg): |
| 198 | try: |
| 199 | if required_param_count == 1: |
| 200 | callback(msg) |
| 201 | else: |
| 202 | callback(ctx_ref, msg) |
| 203 | except Exception as e: |
| 204 | print(f"AimRT channel handle get exception, {e}", file=sys.stderr) |
| 205 | |
| 206 | aimrt_py_ros2.Ros2SubscribeWithCtx(subscriber, py_ros2_ts, msg_type, ros2_callback_wrapper) |
nothing calls this directly
no test coverage detected