Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MobileEventLog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Data-Logistics-Support
MobileEventLog
Commits
cbb1b7a2
Commit
cbb1b7a2
authored
3 years ago
by
Maximilian Betz
Browse files
Options
Downloads
Patches
Plain Diff
started moving the sensor communication to separate dart file
parent
546cec09
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/configuration.dart
+5
-1
5 additions, 1 deletion
lib/configuration.dart
lib/sensorconnector.dart
+132
-0
132 additions, 0 deletions
lib/sensorconnector.dart
with
137 additions
and
1 deletion
lib/configuration.dart
+
5
−
1
View file @
cbb1b7a2
...
...
@@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import
'package:http/http.dart'
as
http
;
import
'dart:convert'
;
import
'datamodel.dart'
;
import
'sensorconnector.dart'
;
Future
<
int
>
login
()
async
{
// Get Access to local configuration store. Token must be stored there!
...
...
@@ -151,11 +152,14 @@ class _MyHomePageState extends State<Configuration> {
late
Future
<
Collection
>
futureCollection
;
late
Future
<
List
<
Collection
>>
futureCollections
;
late
Future
<
List
<
EventType
>>
futureEventTypes
;
SensorConnector
connector
=
SensorConnector
();
@override
void
initState
()
{
super
.
initState
();
futureCollections
=
fetchCollections
();
futureCollections
=
connector
.
fetchCollections
();
futureEventTypes
=
connector
.
fetchEventTypes
();
fetchEventTypes
();
}
late
Future
<
String
>
eventTypes
;
...
...
This diff is collapsed.
Click to expand it.
lib/sensorconnector.dart
0 → 100644
+
132
−
0
View file @
cbb1b7a2
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
;
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment