Skip to content
Snippets Groups Projects
Commit c8807576 authored by Maximilian Betz's avatar Maximilian Betz
Browse files

fetching eventtypes from sensor added

parent 9ec2d5f2
No related branches found
No related tags found
No related merge requests found
......@@ -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();
},
),
......
......@@ -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);
}
}
......
......@@ -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: () {
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment