HomeServer/frontend/lib/devices.dart

101 lines
2.6 KiB
Dart
Raw Normal View History

2023-10-05 12:54:11 +00:00
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Category {
final String name;
List<Device> devices;
Category(this.name) : devices = [];
static Future<List<Category>> fetch(String base_url) async {
final response = await http.get(Uri.parse("$base_url/devices"));
2023-10-06 07:15:17 +00:00
if (response.statusCode != 200) {
throw Exception("Failed to fetch devices");
}
List<Category> categories = [];
Map<String, List<List<dynamic>>> json = jsonDecode(response.body);
2023-10-05 12:54:11 +00:00
2023-10-06 07:15:17 +00:00
for (MapEntry<String, List<List<dynamic>>> category_entry in json.entries) {
Category category = Category(category_entry.key);
2023-10-06 06:50:31 +00:00
2023-10-06 07:15:17 +00:00
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];
2023-10-06 06:50:31 +00:00
2023-10-06 07:15:17 +00:00
final response =
await http.get(Uri.parse("$base_url/plug_state/$device_id"));
2023-10-06 06:50:31 +00:00
2023-10-06 07:15:17 +00:00
if (response.statusCode != 200) {
throw Exception("Failed to fetch plug_state for $device_id");
2023-10-06 06:50:31 +00:00
}
2023-10-06 07:15:17 +00:00
Map<String, dynamic> device_state = jsonDecode(response.body);
Device device = Device(
device_id,
device_descriptor,
device_state["led"],
device_state["power"],
device_state["power_draw"],
power_control && device_state["power_draw"] < 15,
);
category.devices.add(device);
2023-10-06 06:50:31 +00:00
}
2023-10-05 12:54:11 +00:00
2023-10-06 07:15:17 +00:00
categories.add(category);
2023-10-05 12:54:11 +00:00
}
2023-10-06 07:15:17 +00:00
return categories;
2023-10-05 12:54:11 +00:00
}
}
2023-10-06 06:50:31 +00:00
class CategoryWidget extends StatelessWidget {
Category category;
CategoryWidget({super.key, required this.category});
@override
Widget build(BuildContext context) {
var list = category.devices
.map((device) => TableRow(children: [
Text(device.device_descriptor ?? device.device_id),
Text(device.led_state.toString()),
Text(device.power_state.toString()),
Text("${device.power_draw.toString()} W")
]))
.toList();
list.insert(
0,
const TableRow(children: [
Text("Name"),
Text("LED"),
Text("Power"),
Text("Power Draw"),
]));
return Column(
children: <Widget>[Text(category.name), Table(children: list)]);
}
}
2023-10-05 12:54:11 +00:00
class Device {
2023-10-06 06:50:31 +00:00
final String device_id;
String? device_descriptor;
2023-10-05 12:54:11 +00:00
bool led_state;
bool power_state;
double power_draw;
2023-10-06 07:15:17 +00:00
bool power_control;
2023-10-05 12:54:11 +00:00
2023-10-06 06:50:31 +00:00
Device(this.device_id, this.device_descriptor, this.led_state,
2023-10-06 07:15:17 +00:00
this.power_state, this.power_draw, this.power_control);
2023-10-05 12:54:11 +00:00
}