Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'datamodel.dart';
class SensorConnector {
final String baseUrl = 'https://sandbox.sensor.awi.de/rest/';
final String loginUrl = 'sensors/contacts/login';
final String collectionsUrl = 'sensors/collections/getAllCollections?pointInTime=2018-07-03T12%3A30%3A55.389Z';
final String eventTypesUrl = 'sensors/events/getAllEventTypes';
final String collectionItemsUrl = 'sensors/collections/getItemsOfCollection/'; // + collectionId
final String putEventUrl = 'sensors/events/putEvent/'; // + eventId;
Future<String> getAuthToken(String username, String password) async {
String url = baseUrl + loginUrl;
String? token;
Map<String, dynamic> body = {
'username': username,
'authPassword': password
};
String encodedBody = body.keys.map((key) => "$key=${body[key]}").join("&");
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) {
token = response.headers['set-cookie']?.split(';')[0];
debugPrint('Login success. Token: ' + token!.toString());
} else {
throw Exception('Failed to login. '
'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString());
}
return token;
}
Future<List<Collection>> fetchCollections() async {
String url = baseUrl + collectionsUrl;
List<Collection> collectionList = [];
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
collectionList = (json.decode(response.body) as List)
.map((i) => Collection.fromJson(i))
.toList();
debugPrint("Fetching Collections Success: " + collectionList.toString());
} else {
throw Exception('Failed to fetch Collections. '
'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString());
}
return collectionList;
}
Future<List<EventType>> fetchEventTypes() async {
String url = baseUrl + eventTypesUrl;
List<EventType> eventTypeList = [];
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
eventTypeList = (json.decode(response.body) as List)
.map((i) => EventType.fromJson(i))
.toList();
debugPrint("Fetching Event Types Success: " + eventTypeList.toString());
} else{
throw Exception('Failed to fetch event types. '
'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString());
}
return eventTypeList;
}
Future<List<Device>> updateDevices(int collectionId) async {
String url = baseUrl + collectionItemsUrl + collectionId.toString();
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("Fetching Collection Devices Success: " + collectionDevices.toString());
} else {
throw Exception('Failed to load Collection Devices. '
'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString());
}
return collectionDevices;
}
Future<bool> putEvent(Event event, String authToken) async {
// Create url with corresponding device id:
String url = baseUrl + putEventUrl + event.id.toString() + '?createVersion=false';
final response = await http.put(Uri.parse(url),
headers: {
"Content-Type": "application/json",
"Cookie": authToken
},
body: event.toSensorJson().toString(),
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. '
'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString());
return false;
}
}
}