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';
import 'sensorconnector.dart';
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');
//TODO: display feedback to user that credentials are correct
debugPrint(response.headers['set-cookie']?.split(';')[0].toString());
debugPrint(response.body.toString());
debugPrint(response.headers.toString());
debugPrint(response.headers['set-cookie']);
} else {
debugPrint('Header: ' + response.headers.toString());
debugPrint('Body: ' + response.body.toString());
debugPrint('StatusCode: ' + response.statusCode.toString());
throw Exception('Failed to login');
//TODO: display feedback to user. Only allow export in view events if user is valid.
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 events = 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;
events.currentEvent.id = collectionDevices[0].id;
events.currentEvent.urn = collectionDevices[0].urn;
events.currentEvent.description = '';
events.currentEvent.label = '';
events.currentEvent.type = configuration.eventTypes[0].name;
var date = DateTime.now().toUtc();
events.currentEvent.startDate = '$date';
events.currentEvent.endDate = '$date';
configuration.initialized = true;
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;
}
}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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;
late Future<List<EventType>> futureEventTypes;
SensorConnector connector = SensorConnector();
@override
void initState() {
super.initState();
futureCollections = connector.fetchCollections();
futureEventTypes = connector.fetchEventTypes();
late Future<String> eventTypes;
@override
Widget build(BuildContext context) {
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final EventStoreInstance events = EventStoreInstance();
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(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
FloatingActionButton.extended(
heroTag: null,
tooltip: 'Reset all data',
icon: const Icon(Icons.cancel),
label: const Text('Reset all'),
onPressed: () {
//events.reset(); //TODO: remove before release!
configuration.reset();
},
),
FloatingActionButton.extended(
tooltip: 'Select Collection and download corresponding devices',
icon: const Icon(Icons.save),
updateDevices(configuration.currentCollection.id);
),
],
),
);
}
}