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

improved exception handling

parent 6596ab64
No related branches found
No related tags found
No related merge requests found
class Collection { class Collection {
int id; int id;
String description; String description;
...@@ -41,6 +42,7 @@ class Device{ ...@@ -41,6 +42,7 @@ class Device{
} }
} }
class Event{ class Event{
int id; // Device URN id int id; // Device URN id
String urn; String urn;
...@@ -138,6 +140,7 @@ class Event{ ...@@ -138,6 +140,7 @@ class Event{
} }
} }
class EventType{ class EventType{
int id; int id;
String name; String name;
...@@ -152,6 +155,7 @@ class EventType{ ...@@ -152,6 +155,7 @@ class EventType{
} }
} }
class SensorLogin{ class SensorLogin{
String mail; String mail;
String password; String password;
...@@ -220,6 +224,7 @@ abstract class ConfigurationStoreBase { ...@@ -220,6 +224,7 @@ abstract class ConfigurationStoreBase {
} }
} }
class ConfigurationStoreInstance extends ConfigurationStoreBase { class ConfigurationStoreInstance extends ConfigurationStoreBase {
static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance static final ConfigurationStoreInstance _instance = ConfigurationStoreInstance
._internal(); ._internal();
...@@ -251,6 +256,7 @@ abstract class EventStoreBase{ ...@@ -251,6 +256,7 @@ abstract class EventStoreBase{
} }
} }
class EventStoreInstance extends EventStoreBase { class EventStoreInstance extends EventStoreBase {
static final EventStoreInstance _instance = EventStoreInstance._internal(); static final EventStoreInstance _instance = EventStoreInstance._internal();
......
...@@ -34,10 +34,12 @@ class SensorConnector { ...@@ -34,10 +34,12 @@ class SensorConnector {
debugPrint('Login success. Token: ' + token!.toString()); debugPrint('Login success. Token: ' + token!.toString());
} else { } else {
throw Exception('Failed to login. ' debugPrint('Failed to login'
'Header: ' + response.headers.toString() + 'Header: ' + response.headers.toString() +
' StatusCode: ' +response.statusCode.toString() + ' StatusCode: ' +response.statusCode.toString() +
' Body: ' +response.body.toString()); ' Body: ' +response.body.toString());
throw Exception('Failed to login');
} }
return token; return token;
} }
......
...@@ -23,40 +23,48 @@ class _ViewEvents extends State<ViewEvents> { ...@@ -23,40 +23,48 @@ class _ViewEvents extends State<ViewEvents> {
super.initState(); super.initState();
} }
Future<bool> syncEvents() async { Future<void> syncEvents() async {
//final EventStoreInstance events = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance(); final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
SensorConnector connection = SensorConnector(); SensorConnector connection = SensorConnector();
int _synccounter = 0; //For displaying progress during event upload int syncCounter = _events.getPendingEventCount();; //For displaying progress during event upload
String? token = await connection.getAuthToken(configuration.loginInformation.mail, if(syncCounter > 0) {
configuration.loginInformation.password); try {
String? token = await connection.getAuthToken(
configuration.loginInformation.mail,
configuration.loginInformation.password);
if (token != null) { if (token != null) {
_synccounter = _events.getPendingEventCount(); debugPrint('Number of Events: ' + _events.events.length.toString());
debugPrint('Number of Events: ' + _events.events.length.toString()); debugPrint('Number of Pending Events: ' + syncCounter.toString());
debugPrint('Number of Pending Events: ' + _synccounter.toString()); var index = 0;
var index = 0; for (var event in _events.events) {
for (var event in _events.events) { if (event.status == 'PENDING') {
if (event.status == 'PENDING') { debugPrint('Idx: ' + index.toString() + ' ' +
debugPrint('Idx: ' + index.toString() + ' ' + event.toSensorJson().toString());
event.toSensorJson().toString()); index++;
index++; if (await connection.putEvent(event, token) == true) {
if (await connection.putEvent(event, token) == true){ //Event has been posted to Sensor.
//Event has been posted to Sensor. syncCounter--;
_synccounter--; debugPrint(
debugPrint('put success, remaining events: ' + _synccounter.toString()); 'put success, remaining events: ' + syncCounter.toString());
event.status = 'EXPORTED'; //Update event status so that it is only exported once. event.status = 'EXPORTED'; //Update event export only once
setState(() {}); setState(() {});
}else{ } else {
_sync_status = 'export failed: ' + event.urn; throw Exception('Sync for ' + event.urn + 'failed');
}
}
} }
_sync_status = 'export success';
} }
} catch (e) {
debugPrint('Exception: $e');
_sync_status = '$e';
setState(() {});
} }
_sync_status = 'export success'; }else{
return true; debugPrint('Nothing to export');
} }
return false;
} }
@override @override
...@@ -67,168 +75,174 @@ class _ViewEvents extends State<ViewEvents> { ...@@ -67,168 +75,174 @@ class _ViewEvents extends State<ViewEvents> {
title: const Text("View Added Events"), title: const Text("View Added Events"),
), ),
body: Container( body: Container(
margin: const EdgeInsets.symmetric(horizontal: 5.0), margin: const EdgeInsets.symmetric(horizontal: 5.0),
child: child:
SingleChildScrollView( SingleChildScrollView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
child: SingleChildScrollView( child: SingleChildScrollView(
scrollDirection: Axis.vertical, scrollDirection: Axis.vertical,
child: DataTable( child: DataTable(
columns: const <DataColumn>[ columns: const <DataColumn>[
DataColumn( DataColumn(
label: Text( label: Text(
'Id', 'Id',
style: TextStyle(fontStyle: FontStyle.italic), style: TextStyle(fontStyle: FontStyle.italic),
),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'URN',
'URN', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Label',
'Label', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Type',
'Type', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Description',
'Description', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'StartDate',
'StartDate', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'EndDate',
'EndDate', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Latitude',
'Latitude', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Longitude',
'Longitude', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Elevation',
'Elevation', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), DataColumn(
DataColumn( label: Text(
label: Text( 'Status',
'Status', style: TextStyle(fontStyle: FontStyle.italic),
style: TextStyle(fontStyle: FontStyle.italic), ),
), ),
), ],
], rows: <DataRow>[
rows: <DataRow>[ for (var event in _events.events)
for (var event in _events.events) DataRow(
DataRow( cells: <DataCell>[
cells: <DataCell>[ DataCell(Text(event.id.toString())),
DataCell(Text(event.id.toString())), DataCell(Text(event.urn)),
DataCell(Text(event.urn)), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, initialValue: event.label,
initialValue: event.label, onFieldSubmitted: (val) {
onFieldSubmitted: (val) { event.label = val; //Update Database
event.label = val; //Update Database },
}, ),
), ),
), DataCell(Text(event.type)),
DataCell(Text(event.type)), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, initialValue: event.description,
initialValue: event.description, onFieldSubmitted: (val) {
onFieldSubmitted: (val) { event.description = val; //Update Database
event.description = val; //Update Database },
}, ),
), ),
), DataCell(Text(event.startDate)),
DataCell(Text(event.startDate)), DataCell(Text(event.endDate)),
DataCell(Text(event.endDate)), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, initialValue: event.latitude,
initialValue: event.latitude, onFieldSubmitted: (val) {
onFieldSubmitted: (val) { event.latitude = val; //Update Database
event.latitude = val; //Update Database },
}, ),
), ),
), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, initialValue: event.longitude,
initialValue: event.longitude, onFieldSubmitted: (val) {
onFieldSubmitted: (val) { event.longitude = val; //Update Database
event.longitude = val; //Update Database },
}, ),
), ),
), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, initialValue: event.elevation,
initialValue: event.elevation, onFieldSubmitted: (val) {
onFieldSubmitted: (val) { event.elevation = val; //Update Database
event.elevation = val; //Update Database },
}, ),
), ),
), DataCell(
DataCell( TextFormField(
TextFormField( readOnly: true,
readOnly: true, controller: TextEditingController( //Required to update field here
controller: TextEditingController( //Required to update field here
text: event.status, text: event.status,
),
onFieldSubmitted: (val) {
event.status = val; //Update Database
},
), ),
onFieldSubmitted: (val) {
event.status = val; //Update Database
},
), ),
), ],
], ),
), ],
], ),
), ),
), ),
), ),
),
bottomNavigationBar: Row( bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.end,
children: [ children: [
const SizedBox(width: 10), const SizedBox(width: 10),
Text(_sync_status), Text(_sync_status),
FloatingActionButton.extended( const SizedBox(width: 50),
heroTag: null, Container(
tooltip: 'Upload Events', margin: const EdgeInsets.symmetric(vertical: 10.0),
icon: null, child:
label: const Text('Sync'), FloatingActionButton.extended(
onPressed: () { heroTag: null,
syncEvents(); tooltip: 'Upload Events',
}, icon: null,
label: const Text('Sync'),
onPressed: () {
syncEvents();
},
),
), ),
const SizedBox(width: 10.0),
], ],
), ),
); );
......
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