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';
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Future<int> login() async {
// Get Access to local configuration store. Token must be stored there!
ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
String url = 'https://sandbox.sensor.awi.de/rest/sensors/contacts/login';
debugPrint("Start login to : " + url);
Map<String, dynamic> body = {'username': configuration.loginInformation.mail, 'authPassword': configuration.loginInformation.password};
String encodedBody = body.keys.map((key) => "$key=${body[key]}").join("&");
debugPrint(encodedBody);
final response = await http.post(Uri.parse(url),
body: encodedBody,
headers: {
'Accept' : 'application/json',
'Content-Type' : 'application/x-www-form-urlencoded'
},
encoding: Encoding.getByName("utf-8")
);
if (response.statusCode == 200) {
debugPrint('Login success');
debugPrint(response.body.toString());
debugPrint(response.headers.toString());
debugPrint(response.headers['set-cookie']);
//TODO store token
} else {
debugPrint('Header: ' + response.headers.toString());
debugPrint('Body: ' + response.body.toString());
debugPrint('StatusCode: ' + response.statusCode.toString());
throw Exception('Failed to login');
}
return 0;
}
Future<List<Device>> updateDevices(int collectionId) async {
debugPrint("Start HTTP GET Collection Devices. Collection Id: " + collectionId.toString());
String url = 'https://sandbox.sensor.awi.de/rest/sensors/collections/getItemsOfCollection/' + collectionId.toString();
// Get Access to local device and current event store.
EventStoreInstance eventsStore = EventStoreInstance();
ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final response = await http
.get(Uri.parse(url));
if (response.statusCode == 200) {
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());
}
configuration.devices = collectionDevices;
eventsStore.currentEvent.id = collectionDevices[0].id;
eventsStore.currentEvent.urn = collectionDevices[0].urn;
return collectionDevices;
} else {
throw Exception('Failed to load Collection');
}
}
Future<List<Collection>> fetchCollections() async {
ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final response = await http
.get(Uri.parse('https://sandbox.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 {
debugPrint('Failed to load Collection');
return collectionList;
}
}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Future<List<EventType>> fetchEventTypes() async {
debugPrint("Fetching Event Types...");
List<EventType> eventTypeList = [];
ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final response = await http
.get(Uri.parse('https://sandbox.sensor.awi.de/rest/sensors/events/getAllEventTypes'));
if (response.statusCode == 200) {
debugPrint(response.body);
eventTypeList = (json.decode(response.body) as List)
.map((i) => EventType.fromJson(i))
.toList();
configuration.eventTypes = eventTypeList;
return eventTypeList;
} else {
debugPrint('Failed to load Collection');
return eventTypeList;
}
return eventTypeList;
}
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();
futureCollections = fetchCollections();
late Future<String> eventTypes;
@override
Widget build(BuildContext context) {
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You must be online to do something here!',
TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: configuration.loginInformation.mail,
decoration: const InputDecoration(
labelText: 'Sensor E-Mail',
hintText: 'User for writing events',
),
onChanged: (value) {
configuration.loginInformation.mail = value;
},
),
TextFormField(
autofocus: false,
initialValue: configuration.loginInformation.password,
decoration: const InputDecoration(
labelText: 'Sensor Password',
hintText: 'Password for writing events',
),
onChanged: (value){
configuration.loginInformation.password = value;
},
),
const SizedBox(height: 50),
FutureBuilder<List<Collection>>(
future: futureCollections,
builder: (context, snapshot){
if (snapshot.hasData)
{
configuration.collections.add(element);
/*Initialize active collection with first received
collection if not initialized yet*/
if(configuration.currentCollection.id == -1){
configuration.currentCollection = configuration.collections[0];
return DropdownButtonFormField(
value: configuration.currentCollection.collectionName,
decoration: const InputDecoration(
),
items:
configuration.collections.map((Collection collection) {
return DropdownMenuItem(
value: collection.collectionName,
child: Text(collection.collectionName),
);
}).toList(),
onChanged: (value) {
configuration.currentCollection = configuration.getCollectionFromName(value.toString());
}
);
}
else{
),
],
),
),
bottomNavigationBar: Row(
FloatingActionButton.extended(
tooltip: 'Select Collection and download corresponding devices',
icon: const Icon(Icons.save),
updateDevices(configuration.currentCollection.id);
),
],
),
);
}
}