From c880757625e2dce50fc3ea502fa0e232e4e40960 Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Thu, 10 Mar 2022 13:40:33 +0100 Subject: [PATCH] fetching eventtypes from sensor added --- lib/configuration.dart | 104 +++++++++++++++++++++++++++++++++++++++-- lib/datamodel.dart | 2 +- lib/viewevents.dart | 14 +----- 3 files changed, 102 insertions(+), 18 deletions(-) diff --git a/lib/configuration.dart b/lib/configuration.dart index 0ea45e2..52ca2f6 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -5,6 +5,46 @@ import 'package:http/http.dart' as http; import 'dart:convert'; import 'datamodel.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'); + + 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(); @@ -34,7 +74,6 @@ Future<List<Device>> updateDevices(int collectionId) async { //Update id and urn for the add event widget eventsStore.currentEvent.id = collectionDevices[0].id; eventsStore.currentEvent.urn = collectionDevices[0].urn; - return collectionDevices; } else { throw Exception('Failed to load Collection'); @@ -43,6 +82,8 @@ Future<List<Device>> updateDevices(int collectionId) async { Future<List<Collection>> fetchCollections() async { debugPrint("Fetching Collections..."); + ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); + List<Collection> collectionList = []; final response = await http @@ -59,6 +100,35 @@ Future<List<Collection>> fetchCollections() async { } } +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); @@ -76,6 +146,7 @@ class _MyHomePageState extends State<Configuration> { void initState() { super.initState(); futureCollections = fetchCollections(); + fetchEventTypes(); } late Future<String> eventTypes; @@ -98,6 +169,30 @@ class _MyHomePageState extends State<Configuration> { style: TextStyle(fontSize: 14) ), const SizedBox(height: 50), + 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){ @@ -114,9 +209,6 @@ class _MyHomePageState extends State<Configuration> { configuration.currentCollection = configuration.collections[0]; } - //print(activeCollection.store); - //print(collections.store); - return DropdownButtonFormField( value: configuration.currentCollection.collectionName, decoration: const InputDecoration( @@ -149,9 +241,11 @@ class _MyHomePageState extends State<Configuration> { heroTag: null, tooltip: 'Select Collection and download corresponding devices', icon: const Icon(Icons.save), - label: const Text('Select'), + label: const Text('Store'), onPressed: () { updateDevices(configuration.currentCollection.id); + login(); + fetchEventTypes(); HapticFeedback.vibrate(); }, ), diff --git a/lib/datamodel.dart b/lib/datamodel.dart index 6f161b4..e21f5ee 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -111,7 +111,7 @@ class EventType{ ); factory EventType.fromJson(Map<String, dynamic> parsedJson){ - return EventType(parsedJson['id'] as int, parsedJson['name'] as String); + return EventType(parsedJson['id'] as int, parsedJson['generalName'] as String); } } diff --git a/lib/viewevents.dart b/lib/viewevents.dart index b8f1351..4db21a7 100644 --- a/lib/viewevents.dart +++ b/lib/viewevents.dart @@ -156,23 +156,13 @@ class ViewEvents extends StatelessWidget { ), ), bottomNavigationBar: Row( - mainAxisAlignment: MainAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.end, children: [ - FloatingActionButton.extended( - heroTag: null, - tooltip: 'Login to Sensor', - icon: null, - label: const Text('Login'), - onPressed: () { - // Provide - Navigator.pushNamed(context, '/fifth'); - }, - ), FloatingActionButton.extended( heroTag: null, tooltip: 'Upload Events', icon: null, - label: const Text('Upload'), + label: const Text('Sync'), onPressed: () { }, -- GitLab