[英]I am trying to make a flutter app to control my Philips Hue lights and I'm having problems with hue_dart
I have Philips Hue lights and I want to be able to change the colour using Dart and Flutter.我有飞利浦 Hue 灯,我希望能够使用 Dart 和 Flutter 更改颜色。 I have tried using a library called
hue_dart
but when I run it as a Dart file, it can only control one light and I have to click the button on the Bridge every time.我曾尝试使用名为
hue_dart
的库,但是当我将其作为 Dart 文件运行时,它只能控制一盏灯,我每次都必须单击 Bridge 上的按钮。
Here is my code:这是我的代码:
import 'package:http/http.dart';
import 'package:hue_dart/hue_dart.dart';
main(List<String> arguments) async {
final client = Client();
final discovery = BridgeDiscovery(client);
List<DiscoveryResult> discoverResults = await discovery.automatic();
final discoveryResult = discoverResults.first;
var bridge = Bridge(client, discoveryResult.ipAddress);
final whiteListItem = await bridge.createUser('dart_hue#example');
bridge.username = whiteListItem.username;
List<Light> lights = await bridge.lights();
final light = lights.first.changeColor(red: 1.0, green: 0, blue: 1.0);
LightState state = lightStateForColorOnly(light);
state = state.rebuild(
(s) => s
..on = true
..brightness = 10,
);
await bridge.updateLightState(light.rebuild(
(l) => l..state = state.toBuilder(),
));
}
How would I make a user that persists?我将如何让用户持续存在? And one that can control an array of lights, not just the first one it returns?
还有一个可以控制一组灯,而不仅仅是它返回的第一个灯?
You have to click the Bridge button each time as you are creating a user that needs physical authorisation each time.每次创建需要物理授权的用户时,您都必须单击 Bridge 按钮。 Print the created username to display the randomly generated username that the Bridge has created.
打印创建的用户名以显示 Bridge 创建的随机生成的用户名。
final whiteListItem = await bridge.createUser('dart_hue#example');
print(whiteListItem);
bridge.username = whiteListItem.username;
Then you can get rid of the createUser step, and use that random string as the username as that is now an authorised user on the Bridge.然后您可以摆脱 createUser 步骤,并使用该随机字符串作为用户名,因为它现在是 Bridge 上的授权用户。
//final whiteListItem = await bridge.createUser('dart_hue#example');
bridge.username = 'the printed value'
As for the controlling one light aspect, you are only ever changing the state of the first light that is returned from the GET with lights.first
.至于控制一个灯光方面,您只需要更改从 GET 返回的第一个灯光的 state 和
lights.first
。 I haven't tried it but iterating through would be a good next step:我还没有尝试过,但迭代将是一个很好的下一步:
Set<Light> set = Set.from(lights);
set.forEach((element) =>
changeState(element)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.