36 lines
792 B
Dart
36 lines
792 B
Dart
|
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"));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<Category> categories = [];
|
||
|
|
||
|
Map<String, Map<String, dynamic>> json = jsonDecode(response.body);
|
||
|
|
||
|
return categories;
|
||
|
} else {
|
||
|
throw Exception("Failed to fetch devices");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Device {
|
||
|
final int device_id;
|
||
|
bool led_state;
|
||
|
bool power_state;
|
||
|
double power_draw;
|
||
|
|
||
|
Device(this.device_id, this.led_state, this.power_state, this.power_draw);
|
||
|
}
|