advertise method

Future<void> advertise(
)

Implementation

Future<void> advertise(ServiceHandler handler) async {
  if (isAdvertised) return;
  // Send the advertise request.
  ros.send(Request(
    op: 'advertise_service',
    type: type,
    service: name,
  ));
  // Listen for requests, forward them to the handler and then
  // send the response back to the ROS node.
  _advertiser = ros.stream;
  _advertiser!.listen((Map<String, dynamic> message) async {
    if (message['service'] != name) {
      return;
    }
    Map<String, dynamic>? resp = await handler(message['args']);
    ros.send(Request(
      op: 'service_response',
      id: message['id'],
      service: name,
      values: resp ?? {},
      result: resp != null,
    ));
  });

  /*
  _advertiser = ros.stream
      .where((message) => message['service'] == name)
      .asyncMap((req) => handler(req['args'])!.then((resp) {
            ros.send(Request(
              op: 'service_response',
              id: req['id'],
              service: name,
              values: resp ?? {},
              result: resp != null,
            ));
          }));
          */
}