diff --git a/example/flutter_web_example/.gitignore b/example/flutter_web_example/.gitignore deleted file mode 100644 index 50602ac..0000000 --- a/example/flutter_web_example/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -# Files and directories created by pub -.dart_tool/ -.packages -# Remove the following pattern if you wish to check in your lock file -pubspec.lock - -# Conventional directory for build outputs -build/ - -# Directory created by dartdoc -doc/api/ diff --git a/example/flutter_web_example/analysis_options.yaml b/example/flutter_web_example/analysis_options.yaml deleted file mode 100644 index 4fce0e0..0000000 --- a/example/flutter_web_example/analysis_options.yaml +++ /dev/null @@ -1,13 +0,0 @@ -# Defines a default set of lint rules enforced for -# projects at Google. For details and rationale, -# see https://github.com/dart-lang/pedantic#enabled-lints. -include: package:pedantic/analysis_options.yaml - -# For lint rules and documentation, see http://dart-lang.github.io/linter/lints. -# Uncomment to specify additional rules. -# linter: -# rules: -# - camel_case_types - -analyzer: - exclude: [build/**] diff --git a/example/flutter_web_example/lib/main.dart b/example/flutter_web_example/lib/main.dart deleted file mode 100644 index 49d839c..0000000 --- a/example/flutter_web_example/lib/main.dart +++ /dev/null @@ -1,94 +0,0 @@ -import 'package:flutter_web/material.dart'; -import 'package:roslib/roslib.dart'; - -void main() => runApp(MyApp()); - -class MyApp extends StatelessWidget { - // This widget is the root of your application. - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Roslib Example', - theme: ThemeData( - primarySwatch: Colors.blue, - ), - home: MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key) {} - - final String title; - - @override - _MyHomePageState createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - Ros ros; - Topic chatter; - - @override - void initState() { - ros = Ros(url: 'ws://127.0.0.1:9090'); - chatter = Topic( - ros: ros, name: '/chatter', type: "std_msgs/String", reconnectOnClose: true, queueLength: 10, queueSize: 10); - super.initState(); - } - - void initConnection() async { - ros.connect(); - await chatter.subscribe(); - setState(() {}); - } - - void destroyConnection() async { - await chatter.unsubscribe(); - await ros.close(); - setState(() {}); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text('Roslib Example'), - ), - body: StreamBuilder( - stream: ros.statusStream, - builder: (context, snapshot) { - return Center( - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - StreamBuilder( - stream: chatter.subscription, - builder: (context2, snapshot2) { - if (snapshot2.hasData) { - return Text('${snapshot2.data['msg']}'); - } else { - return CircularProgressIndicator(); - } - }, - ), - ActionChip( - label: Text(snapshot.data == Status.CONNECTED ? 'DISCONNECT' : 'CONNECT'), - backgroundColor: snapshot.data == Status.CONNECTED ? Colors.green[300] : Colors.grey[300], - onPressed: () { - if (snapshot.data != Status.CONNECTED) { - this.initConnection(); - } else { - this.destroyConnection(); - } - }, - ), - ], - ), - ); - }), - ); - } -} diff --git a/example/flutter_web_example/pubspec.yaml b/example/flutter_web_example/pubspec.yaml deleted file mode 100644 index cfba3fb..0000000 --- a/example/flutter_web_example/pubspec.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: flutter_web_example -description: An app built using Flutter for web - -environment: - # You must be using Flutter >=1.5.0 or Dart >=2.3.0 - sdk: ">=2.3.0-dev.0.1 <3.0.0" - -dependencies: - flutter_web: any - flutter_web_ui: any - roslib: ^0.0.3 - web_socket_channel: any - -dev_dependencies: - build_runner: ^1.4.0 - build_web_compilers: ^2.0.0 - pedantic: ^1.0.0 - -dependency_overrides: - flutter_web: - git: - url: https://github.com/flutter/flutter_web - path: packages/flutter_web - flutter_web_ui: - git: - url: https://github.com/flutter/flutter_web - path: packages/flutter_web_ui - roslib: - path: ../../ diff --git a/example/flutter_web_example/web/index.html b/example/flutter_web_example/web/index.html deleted file mode 100644 index b54ed98..0000000 --- a/example/flutter_web_example/web/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/example/flutter_web_example/web/main.dart b/example/flutter_web_example/web/main.dart deleted file mode 100644 index b85a3e7..0000000 --- a/example/flutter_web_example/web/main.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:flutter_web_ui/ui.dart' as ui; -import 'package:flutter_web_example/main.dart' as app; - -main() async { - await ui.webOnlyInitializePlatform(); - app.main(); -} diff --git a/lib/core/service.dart b/lib/core/service.dart index 7f1c05a..b5d6ea3 100644 --- a/lib/core/service.dart +++ b/lib/core/service.dart @@ -25,7 +25,7 @@ class Service { String type; /// Advertiser that is listened to for service requests when advertising. - Stream _advertiser; + StreamSubscription _advertiser; /// Checks whether or not the service is currently advertising. bool get isAdvertised => _advertiser != null; @@ -70,16 +70,18 @@ class Service { // Listen for requests, forward them to the handler and then // send the response back to the ROS node. _advertiser = ros.stream - .where((message) => message['service'] == name) + .where((message) => + message['service'] == name && message['op'] == 'call_service') .asyncMap((req) => handler(req['args']).then((resp) { ros.send(Request( op: 'service_response', - id: req.id, + id: req['id'], service: name, values: resp ?? {}, result: resp != null, )); - })); + })) + .listen(null); } // Stop advertising the service. @@ -89,6 +91,7 @@ class Service { op: 'unadvertise_service', service: name, )); + _advertiser?.cancel(); _advertiser = null; } } diff --git a/test/roslib_test.dart b/test/roslib_test.dart index eef596f..86e3db1 100644 --- a/test/roslib_test.dart +++ b/test/roslib_test.dart @@ -71,6 +71,25 @@ void main() { expect(resp, {'sum': 3}); }); + test('host a service', () async { + final server = Service( + ros: ros, + name: '/add_two_ints_dart', + type: 'rospy_tutorials/AddTwoInts', + ); + server.advertise((request) async { + return {'sum': request['a'] + request['b']}; + }); + final client = Service( + ros: ros, + name: '/add_two_ints_dart', + type: 'rospy_tutorials/AddTwoInts', + ); + final req = {'a': 1, 'b': 2}; + final resp = await client.call(req); + expect(resp, {'sum': 3}); + }); + test('get and set a param value', () async { final param = Param( ros: ros,