diff --git a/lib/configuration.dart b/lib/configuration.dart index fce5a1b2f5e581b71ffabcf029c14a8f3388e7ff..3cb42ee2dc1198bded33711e8d82735e1758c108 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -6,140 +6,6 @@ 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. - } - - 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 events = EventStoreInstance(); - ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); - - List<Device> collectionDevices = []; - - 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()); - } - - /*Update to local device store*/ - configuration.devices = collectionDevices; - //Update id and urn for the add event widget - 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 { - debugPrint("Fetching Collections..."); - ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); - - List<Collection> collectionList = []; - - 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; - } -} - -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 { const Configuration({Key? key}) : super(key: key); @@ -149,22 +15,27 @@ class Configuration extends StatefulWidget { } class _MyHomePageState extends State<Configuration> { - late Future<Collection> futureCollection; late Future<List<Collection>> futureCollections; late Future<List<EventType>> futureEventTypes; + late Future<List<Device>> futureDevices; + late Future<String> futureAuthToken; + SensorConnector connector = SensorConnector(); + final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); @override void initState() { super.initState(); + + //Start fetching all possible sensor data as soon as possible: futureCollections = connector.fetchCollections(); futureEventTypes = connector.fetchEventTypes(); - fetchEventTypes(); + futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); + futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); } late Future<String> eventTypes; - @override Widget build(BuildContext context) { @@ -195,6 +66,9 @@ class _MyHomePageState extends State<Configuration> { onChanged: (value) { configuration.loginInformation.mail = value; }, + onFieldSubmitted: (value) { + futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); + }, ), TextFormField( autofocus: false, @@ -206,6 +80,9 @@ class _MyHomePageState extends State<Configuration> { onChanged: (value){ configuration.loginInformation.password = value; }, + onFieldSubmitted: (value) { + futureAuthToken = connector.getAuthToken(configuration.loginInformation.mail, configuration.loginInformation.password); + }, ), const SizedBox(height: 50), FutureBuilder<List<Collection>>( @@ -238,6 +115,8 @@ class _MyHomePageState extends State<Configuration> { }).toList(), onChanged: (value) { configuration.currentCollection = configuration.getCollectionFromName(value.toString()); + //Fetch new selected collection devices + futureDevices = connector.fetchCollectionDevices(configuration.currentCollection.id); } ); } @@ -259,7 +138,7 @@ class _MyHomePageState extends State<Configuration> { label: const Text('Reset all'), onPressed: () { //events.reset(); //TODO: remove before release! - configuration.reset(); + configuration.reset(); //TODO: remove before release this is not a real use case! }, ), FloatingActionButton.extended( @@ -269,10 +148,34 @@ class _MyHomePageState extends State<Configuration> { icon: const Icon(Icons.save), label: const Text('Store'), onPressed: () { - login(); - fetchEventTypes(); - updateDevices(configuration.currentCollection.id); - HapticFeedback.vibrate(); + futureAuthToken.whenComplete((){ + futureDevices.whenComplete(() { + //Update configuration with newly fetched data + futureDevices.then((value) => configuration.devices = value); + futureEventTypes.then((value) => configuration.eventTypes = value); + + //Update id and urn for the add event widget with some initial data + events.currentEvent.id = configuration.devices[0].id; + events.currentEvent.urn = configuration.devices[0].urn; + events.currentEvent.description = ''; + events.currentEvent.label = ''; + events.currentEvent.type = configuration.eventTypes[0].name; + + var date = DateTime.now().toUtc(); + var isodata = date.toIso8601String(); + events.currentEvent.startDate = isodata; + events.currentEvent.endDate = isodata; + configuration.initialized = true; + + HapticFeedback.vibrate(); + }); + }); + + + //login(); + //fetchEventTypes(); + //updateDevices(configuration.currentCollection.id); + //HapticFeedback.vibrate(); }, ), ], diff --git a/lib/sensorconnector.dart b/lib/sensorconnector.dart index aad6ae3d07fb4bbb7025c537ca47f0d044dd0c12..eb6e942d3eef05bbfbcefd07d3d5d639da887d97 100644 --- a/lib/sensorconnector.dart +++ b/lib/sensorconnector.dart @@ -80,7 +80,7 @@ class SensorConnector { return eventTypeList; } - Future<List<Device>> updateDevices(int collectionId) async { + Future<List<Device>> fetchCollectionDevices(int collectionId) async { String url = baseUrl + collectionItemsUrl + collectionId.toString(); List<Device> collectionDevices = [];