diff --git a/android/app/build.gradle b/android/app/build.gradle index 35f63a355392aa7f779a6d2335c712d7e93d207f..c8755d255a6b86e894e41814438bf6aa35ff2f50 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -44,7 +44,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "awi.de.mobileeventlog" - minSdkVersion flutter.minSdkVersion + minSdkVersion 18 //flutter.minSdkVersion replaced with 18 as min required for secure storage targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/lib/configuration.dart b/lib/configuration.dart index b66237994f3046f773cf09e06e77e2da6427421a..3c2b2a571cffd3a5ce5f4d64aaa7692a29499453 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -179,7 +179,8 @@ class _MyHomePageState extends State<Configuration> { try { //Update id and urn for the add event widget with some initial data - events.currentEvent.id = configuration.devices[0] + events.currentEvent.id = 0; + events.currentEvent.urnId = configuration.devices[0] .id; //TODO: fix if devices are an empty list. events.currentEvent.urn = configuration.devices[0].urn; events.currentEvent.description = ''; diff --git a/lib/databaseconnector.dart b/lib/databaseconnector.dart index 2462a6cc8bf1bc3da20c10c9542ffd989c5f1591..da584897140c46038d9691a292a0412fe985cf53 100644 --- a/lib/databaseconnector.dart +++ b/lib/databaseconnector.dart @@ -13,33 +13,43 @@ class DatabaseConnector{ Future<void> connect() async { - database = openDatabase( + database = await openDatabase( join(await getDatabasesPath(), eventDatabase), // When the database is first created, create a table to store the events. onCreate: (db, version) { return db.execute( - 'CREATE TABLE $eventTable(id INTEGER PRIMARY KEY, urnId INTEGER,urn TEXT, label TEXT, type TEXT, typeId INTEGER, description TEXT, status TEXT, startDate TEXT, endDate TEXT, latitude REAL, longitude REAL, elevation REAL)', + 'CREATE TABLE $eventTable(id INTEGER PRIMARY KEY, urnId INTEGER, urn TEXT, label TEXT, type TEXT, typeId INTEGER, description TEXT, status TEXT, startDate TEXT, endDate TEXT, latitude REAL, longitude REAL, elevation REAL)', ); }, // Set the version. This executes the onCreate function and provides a // path to perform database upgrades and downgrades. - version: 1, + version: 3, ); } - //Can also be used to update an existing event. In this case ensure that id is not 0! - Future<void> insertEvent(Event event) async { - final db = await database; - await db.insert( + Future<int> insertNewEvent(Event event) async { + int rowId = 0; + rowId = await database.insert( eventTable, - event.toMap(), - conflictAlgorithm: ConflictAlgorithm.replace, + event.toMapNoId(), + conflictAlgorithm: ConflictAlgorithm.fail, ); + return rowId; } + // Ensure the events id is the correct row id! + Future<int> updateEvent(Event event) async { + int rowId = 0; + rowId = await database.insert( + eventTable, + event.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + return rowId; + } Future<List<Event>> getEvents() async { // Get a reference to the database. @@ -60,9 +70,9 @@ class DatabaseConnector{ status: maps[i]['status'], startDate: maps[i]['startDate'], endDate: maps[i]['endDate'], - latitude: maps[i]['latitude'], - longitude: maps[i]['longitude'], - elevation: maps[i]['elevation'], + latitude: maps[i]['latitude'].toString(), //TODO: change datamodel to double values! + longitude: maps[i]['longitude'].toString(), + elevation: maps[i]['elevation'].toString(), ); }); } diff --git a/lib/datamodel.dart b/lib/datamodel.dart index 5561e462d1b485e19092efbe952e1a4f64b60d6f..53b7d6283a85a74e73eca009848f08de5bdd35e5 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -59,8 +59,8 @@ class Device{ class Event{ - int id; - int urnId; // Device URN id + int id; //This shall be the mysql primary key database id + int urnId; String urn; String label; String type; // Event type name TODO: this should be an EventType variable @@ -125,6 +125,23 @@ class Event{ }; } + Map<String, dynamic> toMapNoId() { + return { + 'urnId': urnId, + 'urn': urn, + 'label': label, + 'type' : type, + 'typeId' : typeId, + 'description' : description, + 'status' : status, + 'startDate' : startDate, + 'endDate' : endDate, + 'latitude' : latitude, + 'longitude' : longitude, + 'elevation' : elevation + }; + } + Map<String, dynamic> toJson() => { 'id' : id, 'urnId': urnId, diff --git a/lib/main.dart b/lib/main.dart index b6dbf74a8a130808780bf9f1012c537902dbf815..7fa259d28e50d793c62dd3e53b3ae01e4e538453 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,10 +9,13 @@ import 'addevent.dart'; import 'viewevents.dart'; import 'overview.dart'; import 'configuration.dart'; +import 'databaseconnector.dart'; Future<void> pathStuff() async { + + //Directory downloadsDirectory = await DownloadsPathProvider.downloadsDirectory; //Directory appDocDir = await getApplicationDocumentsDirectory(); @@ -31,12 +34,32 @@ Future<void> pathStuff() async { //Map<String, String> allValues = await storage.readAll(); //print('Secure Storage: ' + allValues.toString()); + var database = DatabaseConnector(); + await database.connect(); + Event event = Event.fromJson({"id":0,"urnId":102,"urn":"mooring:f9-12","label":"hggg","type":"Calibration", + "typeId":15,"description":"ggggg","status":"PENDING", + "startDate":"2022-03-28T07:00:02.712112Z", + "endDate":"2022-03-28T07:00:02.712112Z","latitude":"53.5440109", + "longitude":"8.58033187","elevation":"48.7139892578125"}); + List<Event> tempEvents = []; + //Update second event in sql database + event.id = 2; + event.description = 'updated description'; + database.updateEvent(event); + //debugPrint("Add Event to database: " + event.toString()); + //int rowId = await database.insertEvent(event); + //debugPrint("Events row Id: " + rowId.toString()); + tempEvents = await database.getEvents(); + debugPrint("Events in SQL Database: "); + for (var event in tempEvents){ + debugPrint(event.toString()); + } } void main() { @@ -65,12 +88,12 @@ void main() { events.fromEventDump( [ - {"id":102,"urn":"mooring:f9-12","label":"hggg","type":"Calibration", + {"id":1,"urnId":102,"urn":"mooring:f9-12","label":"hggg","type":"Calibration", "typeId":15,"description":"ggggg","status":"PENDING", "startDate":"2022-03-28T07:00:02.712112Z", "endDate":"2022-03-28T07:00:02.712112Z","latitude":"53.5440109", "longitude":"8.58033187","elevation":"48.7139892578125"}, - {"id":102,"urn":"mooring:f9-12","label":"hggg","type":"Calibration", + {"id":2,"urnId":102,"urn":"mooring:f9-12","label":"hggg","type":"Calibration", "typeId":15,"description":"ggggg","status":"PENDING", "startDate":"2022-03-28T07:00:03.828190Z", "endDate":"2022-03-28T07:00:03.828190Z","latitude":"53.54401033", diff --git a/lib/sensorconnector.dart b/lib/sensorconnector.dart index 5df77c35c3163770319545ead74da67ef82a3e46..df1c699999e51422e17f33bd767b0b70b7549256 100644 --- a/lib/sensorconnector.dart +++ b/lib/sensorconnector.dart @@ -111,7 +111,7 @@ class SensorConnector { Future<bool> putEvent(Event event, String authToken) async { // Create url with corresponding device id: - String url = baseUrl + putEventUrl + event.id.toString() + '?createVersion=false'; + String url = baseUrl + putEventUrl + event.urnId.toString() + '?createVersion=false'; final response = await http.put(Uri.parse(url), headers: { "Content-Type": "application/json", diff --git a/lib/viewevents.dart b/lib/viewevents.dart index 746b35cd31b5b9ba52e0cbed85aac15964479b0b..c883ee781fd559423cd851dd975759a2725e68c8 100644 --- a/lib/viewevents.dart +++ b/lib/viewevents.dart @@ -14,13 +14,13 @@ class ViewEvents extends StatefulWidget { class _ViewEvents extends State<ViewEvents> { // Get singleton to access locally stored events: - EventStoreInstance _events = EventStoreInstance(); - String _sync_status = ''; + final EventStoreInstance _events = EventStoreInstance(); + String _syncStatus = ''; //TODO: add exception handling and display exceptions to user. @override void initState() { - _sync_status = _events.getPendingEventCount().toString() + ' event(s) pending'; + _syncStatus = _events.getPendingEventCount().toString() + ' event(s) pending'; super.initState(); //TODO: Query all events from database everytime this widget is opened. Remove local event storage instance! @@ -58,15 +58,15 @@ class _ViewEvents extends State<ViewEvents> { } } } - _sync_status = 'export success'; + _syncStatus = 'export success'; } } on SocketException catch (e) { debugPrint('Exception: $e'); - _sync_status = 'No connection'; + _syncStatus = 'No connection'; setState(() {}); } catch (e) { debugPrint('Exception: $e'); - _sync_status = '$e'; + _syncStatus = '$e'; setState(() {}); } }else{ @@ -93,7 +93,7 @@ class _ViewEvents extends State<ViewEvents> { columns: const <DataColumn>[ DataColumn( label: Text( - 'Id', + 'UrnId', style: TextStyle(fontStyle: FontStyle.italic), ), ), @@ -162,7 +162,7 @@ class _ViewEvents extends State<ViewEvents> { for (var event in _events.events) DataRow( cells: <DataCell>[ - DataCell(Text(event.id.toString())), + DataCell(Text(event.urnId.toString())), DataCell(Text(event.urn)), DataCell( TextFormField( @@ -234,7 +234,7 @@ class _ViewEvents extends State<ViewEvents> { mainAxisAlignment: MainAxisAlignment.end, children: [ const SizedBox(width: 10), - Text(_sync_status), + Text(_syncStatus), const SizedBox(width: 50), Container( margin: const EdgeInsets.symmetric(vertical: 10.0),