Use correct fetch for plugs
This commit is contained in:
parent
5dbc860cc3
commit
34702024b0
2 changed files with 42 additions and 27 deletions
|
@ -13,22 +13,39 @@ class Category {
|
||||||
static Future<List<Category>> fetch(String base_url) async {
|
static Future<List<Category>> fetch(String base_url) async {
|
||||||
final response = await http.get(Uri.parse("$base_url/devices"));
|
final response = await http.get(Uri.parse("$base_url/devices"));
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode != 200) {
|
||||||
|
throw Exception("Failed to fetch devices");
|
||||||
|
}
|
||||||
|
|
||||||
List<Category> categories = [];
|
List<Category> categories = [];
|
||||||
|
|
||||||
Map<String, List<Map<String, dynamic>>> json = jsonDecode(response.body);
|
Map<String, List<List<dynamic>>> json = jsonDecode(response.body);
|
||||||
|
|
||||||
for (MapEntry<String, List<Map<String, dynamic>>> category_entry
|
for (MapEntry<String, List<List<dynamic>>> category_entry in json.entries) {
|
||||||
in json.entries) {
|
|
||||||
Category category = Category(category_entry.key);
|
Category category = Category(category_entry.key);
|
||||||
|
|
||||||
for (Map<String, dynamic> device_map in category_entry.value) {
|
for (List<dynamic> device_info in category_entry.value) {
|
||||||
|
String device_id = device_info[0];
|
||||||
|
String? device_descriptor = device_info[1];
|
||||||
|
bool power_control = device_info[2];
|
||||||
|
|
||||||
|
final response =
|
||||||
|
await http.get(Uri.parse("$base_url/plug_state/$device_id"));
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw Exception("Failed to fetch plug_state for $device_id");
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> device_state = jsonDecode(response.body);
|
||||||
|
|
||||||
Device device = Device(
|
Device device = Device(
|
||||||
device_map["device_id"],
|
device_id,
|
||||||
device_map["device_descriptor"],
|
device_descriptor,
|
||||||
device_map["led"],
|
device_state["led"],
|
||||||
device_map["power"],
|
device_state["power"],
|
||||||
device_map["power_draw"]);
|
device_state["power_draw"],
|
||||||
|
power_control && device_state["power_draw"] < 15,
|
||||||
|
);
|
||||||
|
|
||||||
category.devices.add(device);
|
category.devices.add(device);
|
||||||
}
|
}
|
||||||
|
@ -37,9 +54,6 @@ class Category {
|
||||||
}
|
}
|
||||||
|
|
||||||
return categories;
|
return categories;
|
||||||
} else {
|
|
||||||
throw Exception("Failed to fetch devices");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +93,8 @@ class Device {
|
||||||
bool led_state;
|
bool led_state;
|
||||||
bool power_state;
|
bool power_state;
|
||||||
double power_draw;
|
double power_draw;
|
||||||
|
bool power_control;
|
||||||
|
|
||||||
Device(this.device_id, this.device_descriptor, this.led_state,
|
Device(this.device_id, this.device_descriptor, this.led_state,
|
||||||
this.power_state, this.power_draw);
|
this.power_state, this.power_draw, this.power_control);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||||
future: Category.fetch("smart.gavania.de"),
|
future: Category.fetch("smart.gavania.de"),
|
||||||
builder: (context, AsyncSnapshot<List<Category>> categories) {
|
builder: (context, AsyncSnapshot<List<Category>> categories) {
|
||||||
if (!categories.hasData) {
|
if (!categories.hasData) {
|
||||||
return const Scaffold();
|
throw Exception("categories not ready");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|
Loading…
Reference in a new issue