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

moved event put logic to sensorconnector.dart

parent 07ae2545
No related branches found
No related tags found
No related merge requests found
......@@ -114,7 +114,6 @@ class SensorConnector {
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 201) {
event.status = 'EXPORTED'; //Update the event status so that it is only exported once.
return true;
} else {
throw Exception('Failed to put event. '
......
......@@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'package:http/http.dart' as http;
import 'sensorconnector.dart';
class ViewEvents extends StatefulWidget {
const ViewEvents({Key? key}) : super(key: key);
......@@ -13,58 +14,15 @@ class ViewEvents extends StatefulWidget {
class _ViewEvents extends State<ViewEvents> {
int _synccounter = 0; //For displaying progress during event upload
Future<String?> login() async {
String? token;
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
token = response.headers['set-cookie']?.split(';')[0];
debugPrint("Token:" + token!);
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');
}
return token;
}
Future<bool> syncEvents() async {
final EventStoreInstance events = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
String? token = await login();
SensorConnector connection = SensorConnector();
if (token != null) {
String baseUrl = 'https://sandbox.sensor.awi.de/rest/sensors/events/putEvent/';
String url = '';
String? token = await connection.getAuthToken(configuration.loginInformation.mail,
configuration.loginInformation.password);
if (token != null) {
_synccounter = events.getPendingEventCount();
debugPrint('Number of Events: ' + events.events.length.toString());
debugPrint('Number of Pending Events: ' + _synccounter.toString());
......@@ -74,37 +32,13 @@ class _ViewEvents extends State<ViewEvents> {
debugPrint('Idx: ' + index.toString() + ' ' +
event.toSensorJson().toString());
index++;
url = baseUrl + event.id.toString() + '?createVersion=false';
debugPrint('XXX ' + Uri.parse(url).toString());
final response = await http.put(Uri.parse(url),
headers: {
"Content-Type": "application/json",
"Cookie": token
},
body: event.toSensorJson().toString(),
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 201) {
if (await connection.putEvent(event, token) == true){
//Event has been posted to Sensor.
_synccounter--;
debugPrint(
'put success, remaining events: ' + _synccounter.toString());
event.status =
'EXPORTED'; //Update event status so that it is only exported once.
setState(() {});
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 put.');
debugPrint('put success, remaining events: ' + _synccounter.toString());
event.status = 'EXPORTED'; //Update event status so that it is only exported once.
}
setState(() {});
}
}
return true;
......
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