call method

Future call(
  1. dynamic req
)

Call the service with a request (req).

Implementation

Future call(dynamic req) {
  // The service can't be called if it's currently advertising.
  if (isAdvertised) return Future.value(false);
  // Set up the response receiver by filtering data from the ROS node by the ID generated.
  final callId = ros.requestServiceCaller(name);
  final receiver = ros.stream.where((message) => message['id'] == callId).map(
      (Map<String, dynamic> message) => message['result'] == null
          ? Future.error(message['values']!)
          : Future.value(message['values']));
  // Wait for the receiver to receive a single response and then return.
  final completer = Completer();

  listener = receiver.listen((d) {
    completer.complete(d);
    listener!.cancel();
  });
  // Actually send the request.
  ros.send(Request(
    op: 'call_service',
    id: callId,
    service: name,
    type: type,
    args: req,
  ));
  return completer.future;
}