HomeServer/frontend/lib/devices/devices.dart

177 lines
4.4 KiB
Dart
Raw Normal View History

2023-10-13 21:33:13 +00:00
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../constants.dart';
import 'dish_washer.dart';
import 'plug.dart';
import 'temp_humid.dart';
import 'thermostat.dart';
import 'washing_machine.dart';
class Category {
Category(this.name) : devices = [];
final String name;
List<Device> devices;
2023-10-15 07:41:35 +00:00
static Future<List<Category>> fetch_ids_only() async {
final response = await http.get(Uri.parse("${Constants.BASE_URL}/devices"));
if (response.statusCode != 200) {
throw Exception("Failed to fetch devices");
}
final List<Category> categories = [];
final Map<String, List<dynamic>> json =
Map.castFrom(jsonDecode(jsonDecode(response.body)));
2023-10-19 14:26:09 +00:00
for (final MapEntry<String, List<dynamic>> entry in json.entries) {
2023-10-15 07:41:35 +00:00
final Category category = Category(entry.key);
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in entry.value) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-15 07:41:35 +00:00
category.devices
2023-10-19 14:26:09 +00:00
.add(DeviceIdOnly(deviceInfo['id'], deviceInfo['desc']));
2023-10-15 07:41:35 +00:00
}
if (category.devices.isNotEmpty) {
categories.add(category);
}
}
return categories;
}
2023-10-13 21:33:13 +00:00
static Future<List<Category>> fetch() async {
final response = await http.get(Uri.parse("${Constants.BASE_URL}/devices"));
if (response.statusCode != 200) {
throw Exception("Failed to fetch devices");
}
final List<Category> categories = [];
final Map<String, List<dynamic>> json =
Map.castFrom(jsonDecode(jsonDecode(response.body)));
// create plugs
{
final Category category = Category('plugs');
final List<dynamic> plugs = json['plugs']!;
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in plugs) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
category.devices.add(await Plug.create(deviceInfo));
2023-10-13 21:33:13 +00:00
}
categories.add(category);
}
// create thermostat
{
final Category category = Category('thermostat');
final List<dynamic> thermostats = json['thermostat']!;
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in thermostats) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
category.devices.add(await Thermostat.create(deviceInfo));
2023-10-13 21:33:13 +00:00
}
categories.add(category);
}
// create temperature_and_humidity
{
final Category category = Category('temperature_and_humidity');
2023-10-19 14:26:09 +00:00
final List<dynamic> temperatureAndHumidities =
2023-10-13 21:33:13 +00:00
json['temperature_and_humidity']!;
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in temperatureAndHumidities) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
category.devices.add(await TemperatureHumidity.create(deviceInfo));
2023-10-13 21:33:13 +00:00
}
categories.add(category);
}
// create dish_washer
{
final Category category = Category('dish_washer');
2023-10-19 14:26:09 +00:00
final List<dynamic> dishWasher = json['dish_washer']!;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in dishWasher) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
category.devices.add(await DishWasher.create(deviceInfo));
2023-10-13 21:33:13 +00:00
}
categories.add(category);
}
// create washing_machines
{
final Category category = Category('washing_machines');
2023-10-19 14:26:09 +00:00
final List<dynamic> washingMachines = json['washing_machines']!;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
for (final dynamic device_info_dyn in washingMachines) {
final Map<String, dynamic> deviceInfo = device_info_dyn;
2023-10-13 21:33:13 +00:00
2023-10-19 14:26:09 +00:00
category.devices.add(await WashingMachine.create(deviceInfo));
2023-10-13 21:33:13 +00:00
}
categories.add(category);
}
return categories;
}
String Name() {
2023-10-15 07:41:35 +00:00
return name
2023-10-13 21:33:13 +00:00
.split('_')
.map((s) => "${s[0].toUpperCase()}${s.substring(1)}")
.join(' ');
}
2023-10-15 07:41:35 +00:00
}
class CategoryWidget extends StatelessWidget {
2023-10-19 14:26:09 +00:00
const CategoryWidget({super.key, required this.category});
final Category category;
2023-10-13 21:33:13 +00:00
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 25,
runSpacing: 25,
2023-10-14 06:41:27 +00:00
children: category.devices
.map((device) => device.create_widget(context))
.toList(),
2023-10-13 21:33:13 +00:00
);
}
}
abstract class Device {
2023-10-14 06:41:27 +00:00
Widget create_widget(BuildContext context);
2023-10-13 21:33:13 +00:00
}
2023-10-15 07:41:35 +00:00
class DeviceIdOnly extends Device {
DeviceIdOnly(this.device_id, this.device_descriptor);
final String device_id;
final String? device_descriptor;
2023-10-19 14:26:09 +00:00
@override
2023-10-15 07:41:35 +00:00
Widget create_widget(BuildContext context) {
throw UnimplementedError();
}
}