Newer
Older
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'datamodel.dart';
Future<List<Device>> updateDevices(int collectionId) async {
debugPrint("Start HTTP GET Collection Devices. Collection Id: " + collectionId.toString());
String url = 'https://sensor.awi.de/rest/sensors/collections/getItemsOfCollection/' + collectionId.toString();
// Get Access to local device and current event store.
DeviceStoreInstance availableDevices = DeviceStoreInstance();
EventCurrentInstance currentEvent = EventCurrentInstance();
final response = await http
.get(Uri.parse(url));
debugPrint("Got something via Sensor Rest...");
List<Device> collectionDevices = [];
if (response.statusCode == 200) {
// If the server did return a 200 OK response, then parse the JSON.
List<dynamic> data = json.decode(response.body);
for (var entry in data) {
collectionDevices.add(Device.fromJson(entry));
}
debugPrint("Got the following devices: ");
for (var device in collectionDevices){
debugPrint(device.toString());
}
/*Update to local event store*/
availableDevices.store = collectionDevices;
//Update id and urn in add event widget
currentEvent.store.id = collectionDevices[0].id;
currentEvent.store.urn = collectionDevices[0].urn;
return collectionDevices;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load Collection');
}
}
Future<List<Collection>> fetchCollections() async {
debugPrint("Fetching Collections...");
List<Collection> collectionList = [];
final response = await http
.get(Uri.parse('https://sensor.awi.de/rest/sensors/collections/getAllCollections?pointInTime=2018-07-03T12%3A30%3A55.389Z'));
if (response.statusCode == 200) {
return (json.decode(response.body) as List)
.map((i) => Collection.fromJson(i))
.toList();
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
//throw Exception('Failed to load Collection');
debugPrint('Failed to load Collection');
return collectionList;
}
}
class Configuration extends StatefulWidget {
@override
State<Configuration> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<Configuration> {
late Future<Collection> futureCollection;
late Future<List<Collection>> futureCollections;
@override
void initState() {
super.initState();
//futureCollection = fetchCollection();
futureCollections = fetchCollections();
late Future<String> eventTypes;
@override
Widget build(BuildContext context) {
final CollectionStoreInstance collections = CollectionStoreInstance();
final CollectionCurrentInstance activeCollection = CollectionCurrentInstance();
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder<List<Collection>>(
future: futureCollections,
builder: (context, snapshot){
if (snapshot.hasData)
{
collections.store = [];
//debugPrint('Got Collections:');
//debugPrint(element.id.toString() + ' ' + element.collectionName.toString() + ' #' + element.description.toString());
collections.store.add(element);
/*Initialize active collection with first received collection if not initialized yet*/
if(activeCollection.store.collectionName == ''){
activeCollection.store = collections.store[0];
}
return DropdownButtonFormField(
value: activeCollection.store.collectionName,
decoration: const InputDecoration(
),
items:
collections.store.map((Collection collection) {
return DropdownMenuItem(
value: collection.collectionName,
child: Text(collection.collectionName),
);
}).toList(),
onChanged: (value) {
activeCollection.store = collections.getCollectionFromName(value.toString());
}
);
}
else{
),
],
),
),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FloatingActionButton.extended(
tooltip: 'Request Collections from sensor.awi.de',
icon: const Icon(Icons.download),
label: const Text("Get Collections"),
onPressed: () {
},
FloatingActionButton.extended(
tooltip: 'Select Collection and download corresponding devices',
icon: const Icon(Icons.save),
label: const Text('Select'),
onPressed: () {
updateDevices(activeCollection.store.id);
},
),
],
),
);
}
}