Skip to content

Commit b0cffa6

Browse files
committed
feat: add on/off methods
1 parent afab963 commit b0cffa6

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
## 0.0.1
1+
## [0.0.2]
22

3-
New:
4-
- WLED API Controller.
5-
- Toggle method.
3+
### New
4+
- Turn on/off methods.
5+
6+
## [0.0.1] - 25 Jan 2024
7+
8+
### New
9+
- WLED API Controller.
10+
- Toggle method.

lib/wled.dart

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import 'dart:io' show HttpClient, HttpClientResponse;
22

3+
/// Use this class to send commands to WLED instance.
4+
///
5+
/// It implements simple stateless request-response mechanism
6+
/// and don't reuse connection between commands.
7+
/// Use it for one shot operations.
38
class Wled {
9+
/// Host
410
final String host;
511

6-
late final HttpClient _http;
12+
late final HttpClient _http = HttpClient();
713

814
/// Create new WLED control.
9-
Wled(this.host) {
10-
_http = HttpClient();
15+
///
16+
/// [host] - is a hostname (e.g. `localhost`)
17+
/// or an IPv4 address (e.g. `192.168.1.123`).
18+
Wled(this.host);
19+
20+
/// Turn off LED.
21+
Future<void> turnOff() async {
22+
await _request([_Op.turnOff]);
23+
}
24+
25+
/// Turn on LED.
26+
Future<void> turnOn() async {
27+
await _request([_Op.turnOn]);
1128
}
1229

1330
/// Toggle LED.
@@ -26,17 +43,21 @@ class Wled {
2643

2744
extension _OpExt on List<_Op> {
2845
String asParameters() {
29-
return map((it) => switch (it) {
30-
_Op.toggle => '&T=2',
31-
}).join();
46+
if (isEmpty) return '';
47+
48+
// ignore: prefer_interpolation_to_compose_strings
49+
return '&' + map((it) => '${it.name}=${it.value}').join('&');
3250
}
3351
}
3452

3553
enum _Op {
36-
toggle('&T=2'),
54+
turnOff('T', '0'),
55+
turnOn('T', '1'),
56+
toggle('T', '2'),
3757
;
3858

39-
const _Op(this.arg);
59+
const _Op(this.name, this.value);
4060

41-
final String arg;
61+
final String name;
62+
final String value;
4263
}

0 commit comments

Comments
 (0)