From 6ea2d225bf1717ec2a8a48c17187ce26d04d380f Mon Sep 17 00:00:00 2001 From: Brian Beegan Date: Thu, 11 Apr 2024 11:17:40 -0400 Subject: [PATCH] make main fields private --- README.md | 29 ++++++++++++------------- example/reverse_beacon_example.dart | 28 ++++++++++++------------ lib/reverse_beacon.dart | 11 +++++----- lib/src/reverse_beacon_base.dart | 33 +++++++++++++++++++---------- test/reverse_beacon_test.dart | 5 +++-- 5 files changed, 58 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 402503b..bb95ca5 100644 --- a/README.md +++ b/README.md @@ -9,36 +9,35 @@ A simple dart library that transforms the reverse beacon telnet CW and DIGI serv import 'dart:io'; import 'package:reverse_beacon/reverse_beacon.dart'; -import 'package:reverse_beacon/src/band.dart'; -import 'package:reverse_beacon/src/cw_spot.dart'; -import 'package:reverse_beacon/src/exceptions.dart'; -import 'package:reverse_beacon/src/mode.dart'; -void main() async{ +void main() async { var rb = ReverseBeacon(); - try{ + try { await rb.connect(callsign: 'n1bri'); - } - on TelnetCommunicationException catch(_){ + } on TelnetCommunicationException catch (_) { print('Telnet Communication Exception'); exit(-1); - } - on InvalidCallsignException catch(_){ + } on InvalidCallsignException catch (_) { print('Invalid callsign'); exit(-1); - } - catch(ex){ + } catch (ex) { print('unknown issue'); exit(-1); } - rb.controller.stream.listen((spot) { + int spotCount = 0; + rb.listen((spot) { //filter - if(spot.band == Band.meters20 && spot.mode == Mode.cw){ - if ((spot as CWSpot).wpm == 15){ + if (spot.band == Band.meters20 && spot.mode == Mode.cw) { + if ((spot as CWSpot).wpm >= 15) { print(spot); + spotCount++; } } + if (spotCount == 5) { + rb.close(); + } }); } + ``` diff --git a/example/reverse_beacon_example.dart b/example/reverse_beacon_example.dart index 65fe64c..b8110e1 100644 --- a/example/reverse_beacon_example.dart +++ b/example/reverse_beacon_example.dart @@ -1,34 +1,32 @@ import 'dart:io'; import 'package:reverse_beacon/reverse_beacon.dart'; -import 'package:reverse_beacon/src/band.dart'; -import 'package:reverse_beacon/src/cw_spot.dart'; -import 'package:reverse_beacon/src/exceptions.dart'; -import 'package:reverse_beacon/src/mode.dart'; -void main() async{ +void main() async { var rb = ReverseBeacon(); - try{ + try { await rb.connect(callsign: 'n1bri'); - } - on TelnetCommunicationException catch(_){ + } on TelnetCommunicationException catch (_) { print('Telnet Communication Exception'); exit(-1); - } - on InvalidCallsignException catch(_){ + } on InvalidCallsignException catch (_) { print('Invalid callsign'); exit(-1); - } - catch(ex){ + } catch (ex) { print('unknown issue'); exit(-1); } - rb.controller.stream.listen((spot) { + int spotCount = 0; + rb.listen((spot) { //filter - if(spot.band == Band.meters20 && spot.mode == Mode.cw){ - if ((spot as CWSpot).wpm == 15){ + if (spot.band == Band.meters20 && spot.mode == Mode.cw) { + if ((spot as CWSpot).wpm >= 15) { print(spot); + spotCount++; } } + if (spotCount == 5) { + rb.close(); + } }); } diff --git a/lib/reverse_beacon.dart b/lib/reverse_beacon.dart index f607215..caef2b1 100644 --- a/lib/reverse_beacon.dart +++ b/lib/reverse_beacon.dart @@ -1,8 +1,9 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. + library; export 'src/reverse_beacon_base.dart'; - -// TODO: Export any libraries intended for clients of this package. +export 'src/cw_spot.dart'; +export 'package:reverse_beacon/reverse_beacon.dart'; +export 'package:reverse_beacon/src/band.dart'; +export 'package:reverse_beacon/src/exceptions.dart'; +export 'package:reverse_beacon/src/mode.dart'; diff --git a/lib/src/reverse_beacon_base.dart b/lib/src/reverse_beacon_base.dart index 1db61d6..1f0b4fd 100644 --- a/lib/src/reverse_beacon_base.dart +++ b/lib/src/reverse_beacon_base.dart @@ -15,20 +15,20 @@ const int digiPort = 7001; const int timeout = 25; class ReverseBeacon { - Socket? cwSocket; - Socket? digiSocket; - StreamController controller = StreamController.broadcast(); + Socket? _cwSocket; + Socket? _digiSocket; + final StreamController _controller = StreamController.broadcast(); Future connect({required callsign}) async { if (!isValidCallsign(callsign)) { throw InvalidCallsignException(); } - cwSocket = await Socket.connect(host, cwPort, + _cwSocket = await Socket.connect(host, cwPort, timeout: const Duration(seconds: timeout)); - digiSocket = await Socket.connect(host, digiPort, + _digiSocket = await Socket.connect(host, digiPort, timeout: const Duration(seconds: timeout)); - cwSocket?.listen(onError: (error) => throw TelnetCommunicationException(), + _cwSocket?.listen(onError: (error) => throw TelnetCommunicationException(), (event) { var spots = utf8 .decode(event) @@ -37,16 +37,16 @@ class ReverseBeacon { .toList(); if (spots[0] == "Please enter your call: ") { - cwSocket?.add(utf8.encode('$callsign\r\n')); + _cwSocket?.add(utf8.encode('$callsign\r\n')); } else { spots = spots.where((e) => e.startsWith('DX')).toList(); for (int i = 0; i < spots.length; i++) { - controller.add(CWSpot.fromTelnetText(spots[i])); + _controller.add(CWSpot.fromTelnetText(spots[i])); } } }); - digiSocket?.listen(onError: (error) => throw TelnetCommunicationException(), + _digiSocket?.listen(onError: (error) => throw TelnetCommunicationException(), (event) { var spots = utf8 .decode(event) @@ -55,13 +55,24 @@ class ReverseBeacon { .toList(); if (spots[0] == "Please enter your call: ") { - digiSocket?.add(utf8.encode('$callsign\r\n')); + _digiSocket?.add(utf8.encode('$callsign\r\n')); } else { spots = spots.where((e) => e.startsWith('DX')).toList(); for (int i = 0; i < spots.length; i++) { - controller.add(DigiSpot.fromTelnetText(spots[i])); + _controller.add(DigiSpot.fromTelnetText(spots[i])); } } }); } + + void listen(Function(Spot spot) onListen){ + _controller.stream.listen(onListen); + } + + void close() async{ + await _cwSocket?.close(); + await _digiSocket?.close(); + _controller.close(); + } + } diff --git a/test/reverse_beacon_test.dart b/test/reverse_beacon_test.dart index 33b8b11..196569a 100644 --- a/test/reverse_beacon_test.dart +++ b/test/reverse_beacon_test.dart @@ -1,5 +1,4 @@ import 'package:reverse_beacon/reverse_beacon.dart'; -import 'package:reverse_beacon/src/exceptions.dart'; import 'package:reverse_beacon/src/spot.dart'; import 'package:test/test.dart'; @@ -12,6 +11,8 @@ void main(){ test('valid call should emit spots', () async{ var tRb = ReverseBeacon(); await tRb.connect(callsign: 'n1bri'); - await expectLater(tRb.controller?.stream, emits(TypeMatcher())); + tRb.listen((spot) { + expect(spot, emits(TypeMatcher())); + }); }); } \ No newline at end of file