Skip to content

Commit

Permalink
feat!: parse port from address argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dector committed Feb 18, 2024
1 parent e9dbd2d commit 53fd3ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## [0.0.6] - SNAPSHOT

### Breaking Changes
- `Wled()` constructor accepts port as a part of the string address:
e.g. `Wled('localhost:8080')`.

## Removed
- `port` parameter is removed from the `Wled()` constructor.


## [0.0.5] - 18 Feb 2024

Expand Down
36 changes: 31 additions & 5 deletions lib/src/wled.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ class Wled {

/// Create new WLED control.
///
/// [host] - is a hostname (e.g. `localhost`)
/// [address] - is a hostname (e.g. `localhost`)
/// or an IPv4 address (e.g. `192.168.1.123`).
/// [port] - (optional) connection port.
Wled(
this.host, {
this.port = 80,
/// Might optionally contain port - e.g. `localhost:1234`.
factory Wled(String address) {
final (host, port) = _parseHostAndPort(address);

return Wled._(
host: host,
port: port ?? 80,
);
}

Wled._({
required this.host,
required this.port,
});

/// Turn off LED.
Expand Down Expand Up @@ -100,3 +109,20 @@ enum _Op {
final String name;
final String value;
}

(String, int?) _parseHostAndPort(String address) {
var ip = address;
int? port;

final index = address.indexOf(':');
if (index != -1) {
ip = address.substring(0, index);

if (index < address.length) {
final parsed = int.tryParse(address.substring(index + 1));
if (parsed != null) port = parsed;
}
}

return (ip, port);
}

0 comments on commit 53fd3ed

Please sign in to comment.