def _message_req(self, msg_name, cb, params=None): bind_id = None def _cb(sender, msg): if bind_id is not None: self._ivy.unsubscribe(bind_id) cb(sender, msg) req_id = self._get_req_id() req_regex = '^{} ([^ ]* +{}( .*|$))'.format(req_id, msg_name) bind_id = self._ivy.subscribe(_cb, req_regex) req_msg = PprzMessage('ground', '{}_REQ'.format(msg_name)) if params is not None: req_msg.set_values(params) #FIXME we shouldn't use directly Ivy, but pprzlink python API is not supporting the request id for now IvySendMsg('pprz_connect {} {} {}'.format( req_id, req_msg.name, req_msg.payload_to_ivy_string()))
def send_request(self, class_name, request_name, callback, **request_extra_data): """ Send a data request message and passes the result directly to the callback method. :return: Number of clients this message was sent to. :rtype: int :param class_name: Message class, the same as :ref:`PprzMessage.__init__` :param request_name: Request name (without the _REQ suffix) :param callback: Callback function that accepts two parameters: 1. aircraft id as int 2. The response message :param request_extra_data: Payload that will be sent with the request if any :type class_name: str :type request_name: str :type callback: Callable[[str, PprzMessage], Any] :type request_extra_data: Dict[str, Any] :raises: ValueError: if msg was invalid or `sender_id` not provided for telemetry messages :raises: RuntimeError: if the server is not running """ new_id = RequestUIDFactory.generate_uid() regex = r"^((\S*\s*)?%s %s %s( .*|$))" % (new_id, class_name, request_name) def data_request_callback(ac_id, request_id, msg): try: callback(ac_id, msg) except Exception as e: raise e finally: self.unsubscribe(binding_id) binding_id = self.subscribe(data_request_callback, regex) request_message = PprzMessage(class_name, "%s_REQ" % request_name) for k, v in request_extra_data.items(): request_message.set_value_by_name(k, v) data_request_message = ' '.join( (self.agent_name, new_id, request_message.name, request_message.payload_to_ivy_string())) return self.send(data_request_message)