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

started adding a sqlite implementation to store events locally

parent 5b8b8790
No related branches found
No related tags found
No related merge requests found
File deleted
{
"version": 2,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "awi.de.mobileeventlog",
"variantName": "processReleaseResources",
"elements": [
{
"type": "SINGLE",
"filters": [],
"versionCode": 1,
"versionName": "1.0.0",
"outputFile": "app-release.apk"
}
]
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart'; ...@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'datamodel.dart'; import 'datamodel.dart';
import 'dart:async'; import 'dart:async';
import 'package:geolocator/geolocator.dart'; import 'package:geolocator/geolocator.dart';
...@@ -20,6 +21,7 @@ class _AddEventPageState extends State<AddEvent> { ...@@ -20,6 +21,7 @@ class _AddEventPageState extends State<AddEvent> {
late String alt = ""; late String alt = "";
late double accuracy = 0.0; late double accuracy = 0.0;
late StreamSubscription<Position> streamHandler; //For canceling GNSS stream on dispose late StreamSubscription<Position> streamHandler; //For canceling GNSS stream on dispose
final prefs = SharedPreferences.getInstance(); // Is async
Future startGNSS() async { Future startGNSS() async {
debugPrint("Check Location Permission"); debugPrint("Check Location Permission");
...@@ -163,7 +165,8 @@ class _AddEventPageState extends State<AddEvent> { ...@@ -163,7 +165,8 @@ class _AddEventPageState extends State<AddEvent> {
eventsStore.currentEvent.status = "PENDING"; eventsStore.currentEvent.status = "PENDING";
eventsStore.events.add( eventsStore.events.add(
Event( Event(
id: eventsStore.currentEvent.id, id: 0,
urnId: eventsStore.currentEvent.urnId,
urn: eventsStore.currentEvent.urn, urn: eventsStore.currentEvent.urn,
label: eventsStore.currentEvent.label, label: eventsStore.currentEvent.label,
type: eventsStore.currentEvent.type, type: eventsStore.currentEvent.type,
...@@ -274,7 +277,7 @@ class _AddEventPageState extends State<AddEvent> { ...@@ -274,7 +277,7 @@ class _AddEventPageState extends State<AddEvent> {
}).toList(), }).toList(),
onChanged: (value) { onChanged: (value) {
eventsStore.currentEvent.urn = value.toString(); eventsStore.currentEvent.urn = value.toString();
eventsStore.currentEvent.id = eventsStore.currentEvent.urnId =
configuration.getDeviceIdFromUrn(value.toString()); configuration.getDeviceIdFromUrn(value.toString());
} }
), ),
......
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'dart:async';
import 'datamodel.dart';
class DatabaseConnector{
static const String eventDatabase = 'sensor_events.db';
static const String eventTable = 'events';
dynamic database;
Future<void> connect() async {
database = openDatabase(
join(await getDatabasesPath(), eventDatabase),
// When the database is first created, create a table to store the events.
onCreate: (db, version) {
// Run the CREATE TABLE statement on the database.
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)',
);
},
// Set the version. This executes the onCreate function and provides a
// path to perform database upgrades and downgrades.
version: 1,
);
}
Future<void> insertEvent(Event event) async {
final db = await database;
await db.insert(
eventTable,
event.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
// A method that retrieves all the dogs from the dogs table.
Future<List<Event>> getEvents() async {
// Get a reference to the database.
final db = await database;
final List<Map<String, dynamic>> maps = await db.query(eventTable);
// Convert the List<Map<String, dynamic> into a List<Event>.
return List.generate(maps.length, (i) {
return Event(
id: maps[i]['id'],
urnId: maps[i]['urnId'],
urn: maps[i]['urn'],
label: maps[i]['label'],
type: maps[i]['type'],
typeId: maps[i]['typeId'],
description: maps[i]['description'],
status: maps[i]['status'],
startDate: maps[i]['startDate'],
endDate: maps[i]['endDate'],
latitude: maps[i]['latitude'],
longitude: maps[i]['longitude'],
elevation: maps[i]['elevation'],
);
});
}
}
/*
* int id; // Device URN id
String urn;
String label;
String type; // Event type name TODO: this should be an EventType variable
int typeId;
String description;
String status;
String startDate;
String endDate;
String latitude;
String longitude;
String elevation;
*
*
* */
\ No newline at end of file
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/cupertino.dart';
class Collection { class Collection {
int id; int id;
String description; String description;
...@@ -57,7 +59,8 @@ class Device{ ...@@ -57,7 +59,8 @@ class Device{
class Event{ class Event{
int id; // Device URN id int id;
int urnId; // Device URN id
String urn; String urn;
String label; String label;
String type; // Event type name TODO: this should be an EventType variable String type; // Event type name TODO: this should be an EventType variable
...@@ -72,6 +75,7 @@ class Event{ ...@@ -72,6 +75,7 @@ class Event{
Event({ Event({
required this.id, required this.id,
required this.urnId,
required this.urn, required this.urn,
required this.label, required this.label,
required this.type, required this.type,
...@@ -88,6 +92,7 @@ class Event{ ...@@ -88,6 +92,7 @@ class Event{
factory Event.fromJson(Map<String, dynamic> json){ factory Event.fromJson(Map<String, dynamic> json){
return Event( return Event(
id: json['id'], id: json['id'],
urnId: json['urnId'],
urn: json['urn'], urn: json['urn'],
label: json['label'], label: json['label'],
type: json['type'], type: json['type'],
...@@ -102,8 +107,27 @@ class Event{ ...@@ -102,8 +107,27 @@ class Event{
); );
} }
Map<String, dynamic> toMap() {
return {
'id' : id,
'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() => { Map<String, dynamic> toJson() => {
'id': id, 'id' : id,
'urnId': urnId,
'urn': urn, 'urn': urn,
'label': label, 'label': label,
'type' : type, 'type' : type,
...@@ -119,7 +143,7 @@ class Event{ ...@@ -119,7 +143,7 @@ class Event{
Map<String, dynamic> toSensorJson() { Map<String, dynamic> toSensorJson() {
return { return {
"\"itemID\"":"\"$id\"", "\"itemID\"":"\"$urnId\"",
"\"inheritToAllChildren\"":"\"false\"", "\"inheritToAllChildren\"":"\"false\"",
"\"inheritToChildren\"":[], "\"inheritToChildren\"":[],
"\"event\"":{ "\"event\"":{
...@@ -138,7 +162,9 @@ class Event{ ...@@ -138,7 +162,9 @@ class Event{
@override @override
String toString(){ String toString(){
return '{ ${this.id}, ' return '{ '
'${this.id}, '
'${this.urnId}, '
'${this.urn}, ' '${this.urn}, '
'${this.label}, ' '${this.label}, '
'${this.type}, ' '${this.type}, '
...@@ -270,7 +296,8 @@ class ConfigurationStoreInstance extends ConfigurationStoreBase { ...@@ -270,7 +296,8 @@ class ConfigurationStoreInstance extends ConfigurationStoreBase {
abstract class EventStoreBase{ abstract class EventStoreBase{
List<Event> events = []; List<Event> events = [];
Event currentEvent = Event( Event currentEvent = Event(
id:-1, id: 0,
urnId:-1,
urn:'urn0', urn:'urn0',
label:'', label:'',
type:'', type:'',
...@@ -307,10 +334,8 @@ abstract class EventStoreBase{ ...@@ -307,10 +334,8 @@ abstract class EventStoreBase{
fromEventDump(dump){ fromEventDump(dump){
events = []; events = [];
print(dump); for (var entry in dump) {
List<dynamic> data = jsonDecode(dump); debugPrint('Added Event from Storage: ' + entry.toString());
for (var entry in data) {
print(entry);
events.add(Event.fromJson(entry)); events.add(Event.fromJson(entry));
} }
} }
...@@ -327,7 +352,8 @@ class EventStoreInstance extends EventStoreBase { ...@@ -327,7 +352,8 @@ class EventStoreInstance extends EventStoreBase {
EventStoreInstance._internal() { EventStoreInstance._internal() {
events = []; events = [];
currentEvent = Event( currentEvent = Event(
id:-1, id:0,
urnId:-1,
urn:'urn0', urn:'urn0',
label:'', label:'',
type:'', type:'',
...@@ -345,7 +371,8 @@ class EventStoreInstance extends EventStoreBase { ...@@ -345,7 +371,8 @@ class EventStoreInstance extends EventStoreBase {
void reset(){ void reset(){
events = []; events = [];
currentEvent = Event( currentEvent = Event(
id:-1, id:0,
urnId:-1,
urn:'urn0', urn:'urn0',
label:'', label:'',
type:'', type:'',
......
...@@ -2,32 +2,48 @@ import 'dart:io'; ...@@ -2,32 +2,48 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'datamodel.dart'; import 'datamodel.dart';
import 'addevent.dart'; import 'addevent.dart';
import 'viewevents.dart'; import 'viewevents.dart';
import 'overview.dart'; import 'overview.dart';
import 'configuration.dart'; import 'configuration.dart';
Future<void> pathstuff() async { Future<void> pathStuff() async {
//Directory downloadsDirectory = await DownloadsPathProvider.downloadsDirectory; //Directory downloadsDirectory = await DownloadsPathProvider.downloadsDirectory;
//Directory appDocDir = await getApplicationDocumentsDirectory(); //Directory appDocDir = await getApplicationDocumentsDirectory();
//debugPrint(appDocDir.path.toString());
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
//prefs.setString('events', 'blabla');
final String? events = prefs.getString('events');
print('Shared Preferences: ' + events!);
//const storage = FlutterSecureStorage();
//Map<String, String> allValues = await storage.readAll();
//print('Secure Storage: ' + allValues.toString());
//debugPrint(appDocDir.path.toString());
}
void main() {
//pathstuff();
}
void main() {
pathStuff();
EventStoreInstance events = EventStoreInstance(); EventStoreInstance events = EventStoreInstance();
...@@ -47,6 +63,19 @@ void main() { ...@@ -47,6 +63,19 @@ void main() {
//configuration.currentCollection = Collection.fromJson({"id":1,"description":"","collectionName":"FRAM"}); //configuration.currentCollection = Collection.fromJson({"id":1,"description":"","collectionName":"FRAM"});
//configuration.initialized = true; //configuration.initialized = true;
events.fromEventDump(
[
{"id":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",
"typeId":15,"description":"ggggg","status":"PENDING",
"startDate":"2022-03-28T07:00:03.828190Z",
"endDate":"2022-03-28T07:00:03.828190Z","latitude":"53.54401033",
"longitude":"8.58032778","elevation":"48.720947265625"},
]);
//events.fromEventDump([{"id":102,"urn":"mooring:f9-12","label":"cf","type":"Calibration","typeId":15,"description":"fd","status":"PENDING","startDate":"2022-03-25T12:47:30.659436Z","endDate":"2022-03-25T12:47:30.659436Z","latitude":"","longitude":"","elevation":""},{"id":102,"urn":"mooring:f9-12","label":"cf","type":"Calibration","typeId":15,"description":"fd","status":"PENDING","startDate":"2022-03-25T12:47:32.136009Z","endDate":"2022-03-25T12:47:32.136009Z","latitude":"","longitude":"","elevation":""}]); //events.fromEventDump([{"id":102,"urn":"mooring:f9-12","label":"cf","type":"Calibration","typeId":15,"description":"fd","status":"PENDING","startDate":"2022-03-25T12:47:30.659436Z","endDate":"2022-03-25T12:47:30.659436Z","latitude":"","longitude":"","elevation":""},{"id":102,"urn":"mooring:f9-12","label":"cf","type":"Calibration","typeId":15,"description":"fd","status":"PENDING","startDate":"2022-03-25T12:47:32.136009Z","endDate":"2022-03-25T12:47:32.136009Z","latitude":"","longitude":"","elevation":""}]);
...@@ -57,7 +86,7 @@ void main() { ...@@ -57,7 +86,7 @@ void main() {
), ),
initialRoute: '/', initialRoute: '/',
routes: { routes: {
'/': (context) => Overview(), '/': (context) => const Overview(),
'/second': (context) => const AddEvent(), '/second': (context) => const AddEvent(),
'/third': (context) => const ViewEvents(), '/third': (context) => const ViewEvents(),
'/forth': (context) => const Configuration(), '/forth': (context) => const Configuration(),
......
...@@ -14,6 +14,8 @@ class Overview extends StatelessWidget { ...@@ -14,6 +14,8 @@ class Overview extends StatelessWidget {
children: const <Widget>[ children: const <Widget>[
Icon( Icons.copyright_outlined), Icon( Icons.copyright_outlined),
Text('Alfred Wegener Institute'), Text('Alfred Wegener Institute'),
Text('Data-Logistics-Support'),
SizedBox(height: 10),
Text('Maximilian Betz'), Text('Maximilian Betz'),
SizedBox(height: 30), SizedBox(height: 30),
Text('o2a-support@awi.de'), Text('o2a-support@awi.de'),
......
...@@ -118,6 +118,48 @@ packages: ...@@ -118,6 +118,48 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.1.0" version: "3.1.0"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
flutter_secure_storage_linux:
dependency: transitive
description:
name: flutter_secure_storage_linux
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_secure_storage_macos:
dependency: transitive
description:
name: flutter_secure_storage_macos
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_secure_storage_platform_interface:
dependency: transitive
description:
name: flutter_secure_storage_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
flutter_secure_storage_web:
dependency: transitive
description:
name: flutter_secure_storage_web
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
flutter_secure_storage_windows:
dependency: transitive
description:
name: flutter_secure_storage_windows
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
flutter_signin_button: flutter_signin_button:
dependency: transitive dependency: transitive
description: description:
...@@ -399,6 +441,20 @@ packages: ...@@ -399,6 +441,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.1" version: "1.8.1"
sqflite:
dependency: "direct main"
description:
name: sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
sqflite_common:
dependency: transitive
description:
name: sqflite_common
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
...@@ -420,6 +476,13 @@ packages: ...@@ -420,6 +476,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" version: "1.1.0"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0+2"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
......
...@@ -31,6 +31,8 @@ dependencies: ...@@ -31,6 +31,8 @@ dependencies:
sdk: flutter sdk: flutter
shared_preferences: ^2.0.12 shared_preferences: ^2.0.12
flutter_secure_storage: ^5.0.2
sqflite: ^2.0.2
path_provider: ^2.0.9 path_provider: ^2.0.9
http: ^0.13.4 http: ^0.13.4
geolocator: ^8.1.1 geolocator: ^8.1.1
......
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