From 53074cde40fec969983b7d34c8d57330e7bba63b Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Fri, 25 Mar 2022 10:25:10 +0100 Subject: [PATCH] jsonising for local storage --- lib/addevent.dart | 24 ++--- lib/datamodel.dart | 186 +++++++++++++++++++++++++-------------- lib/main.dart | 3 + lib/sensorconnector.dart | 2 +- lib/viewevents.dart | 4 + 5 files changed, 142 insertions(+), 77 deletions(-) diff --git a/lib/addevent.dart b/lib/addevent.dart index 918c7d7..a850abe 100644 --- a/lib/addevent.dart +++ b/lib/addevent.dart @@ -163,18 +163,18 @@ class _AddEventPageState extends State<AddEvent> { eventsStore.currentEvent.status = "PENDING"; eventsStore.events.add( Event( - eventsStore.currentEvent.id, - eventsStore.currentEvent.urn, - eventsStore.currentEvent.label, - eventsStore.currentEvent.type, - eventsStore.currentEvent.typeId, - eventsStore.currentEvent.description, - eventsStore.currentEvent.status, - eventsStore.currentEvent.startDate, - eventsStore.currentEvent.endDate, - eventsStore.currentEvent.latitude, - eventsStore.currentEvent.longitude, - eventsStore.currentEvent.elevation + id: eventsStore.currentEvent.id, + urn: eventsStore.currentEvent.urn, + label: eventsStore.currentEvent.label, + type: eventsStore.currentEvent.type, + typeId: eventsStore.currentEvent.typeId, + description: eventsStore.currentEvent.description, + status: eventsStore.currentEvent.status, + startDate: eventsStore.currentEvent.startDate, + endDate: eventsStore.currentEvent.endDate, + latitude: eventsStore.currentEvent.latitude, + longitude: eventsStore.currentEvent.longitude, + elevation: eventsStore.currentEvent.elevation )); } diff --git a/lib/datamodel.dart b/lib/datamodel.dart index 422526a..a14e6ba 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -14,11 +14,17 @@ class Collection { factory Collection.fromJson(Map<String, dynamic> json) { return Collection( id: json['id'], - collectionName: json['collectionName'], description: json['description'], + collectionName: json['collectionName'], ); } + Map<String, dynamic> toJson() => { + 'id': id, + 'description': description, + 'collectionName' : collectionName + }; + @override String toString(){ return collectionName; @@ -31,11 +37,16 @@ class Device{ Device(this.id, this.urn); - factory Device.fromJson(Map<String, dynamic> parsedJson){ - return Device( parsedJson['id'] as int, - parsedJson['urn'] as String); + factory Device.fromJson(Map<String, dynamic> json){ + return Device( json['id'] as int, + json['urn'] as String); } + Map<String, dynamic> toJson() => { + 'id': id, + 'urn': urn + }; + @override String toString(){ return '{ ${this.id}, ${this.urn} }'; @@ -57,70 +68,69 @@ class Event{ String longitude; String elevation; - Event( - this.id, - this.urn, - this.label, - this.type, - this.typeId, - this.description, - this.status, - this.startDate, - this.endDate, - [this.latitude = "", - this.longitude = "", - this.elevation = ""] - ); + Event({ + required this.id, + required this.urn, + required this.label, + required this.type, + required this.typeId, + required this.description, + required this.status, + required this.startDate, + required this.endDate, + required this.latitude, + required this.longitude, + required this.elevation, + }); - factory Event.fromJson(Map<String, dynamic> parsedJson){ - return Event( parsedJson['id'] as int, - parsedJson['urn'] as String, - parsedJson['label'] as String, - parsedJson['type'] as String, - parsedJson['typeId'] as int, - parsedJson['description'] as String, - parsedJson['status'] as String, - parsedJson['startDate'] as String, - parsedJson['endDate'] as String, - "", - "", - "", + factory Event.fromJson(Map<String, dynamic> json){ + return Event( + id: json['id'], + urn: json['urn'], + label: json['label'], + type: json['type'], + typeId: json['typeId'], + description: json['description'], + status: json['status'], + startDate: json['startDate'], + endDate: json['endDate'], + latitude: json['latitude'], + longitude: json['longitude'], + elevation: json['elevation'], ); } - Map<String, dynamic> toSensorJsonOld() => { - "\"itemID\"":"\"$id\"", - "\"inheritToAllChildren\"":"\"false\"", - "\"inheritToChildren\"":[], - "\"event\"":{ - "\"startDate\"":"\"$startDate\"", - "\"endDate\"":"\"$endDate\"", - "\"label\"":"\"$label\"", - "\"description\"":"\"$description\"", - "\"eventType\"":"\"$type\"", - "\"latitude\"":"\"$latitude\"", - "\"longitude\"":"\"$longitude\"", - "\"elevationInMeter\"":"\"$elevation\"", - "\"id\"":"\"0\"" - } + Map<String, dynamic> toJson() => { + 'id': id, + '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> toSensorJson() { return { - "\"itemID\"":"\"$id\"", - "\"inheritToAllChildren\"":"\"false\"", - "\"inheritToChildren\"":[], - "\"event\"":{ - "\"startDate\"":"\"$startDate\"", - "\"endDate\"":"\"$endDate\"", - "\"label\"":"\"$label\"", - "\"description\"":"\"$description\"", - "\"eventType\"":"\"$typeId\"", - "\"latitude\"":"\"$latitude\"", - "\"longitude\"":"\"$longitude\"", - "\"elevationInMeter\"":"\"$elevation\"", - "\"id\"":"\"0\"" - } + "\"itemID\"":"\"$id\"", + "\"inheritToAllChildren\"":"\"false\"", + "\"inheritToChildren\"":[], + "\"event\"":{ + "\"startDate\"":"\"$startDate\"", + "\"endDate\"":"\"$endDate\"", + "\"label\"":"\"$label\"", + "\"description\"":"\"$description\"", + "\"eventType\"":"\"$typeId\"", + "\"latitude\"":"\"$latitude\"", + "\"longitude\"":"\"$longitude\"", + "\"elevationInMeter\"":"\"$elevation\"", + "\"id\"":"\"0\"" + } }; } @@ -150,9 +160,18 @@ class EventType{ this.name ); - factory EventType.fromJson(Map<String, dynamic> parsedJson){ + factory EventType.fromSensorJson(Map<String, dynamic> parsedJson){ return EventType(parsedJson['id'] as int, parsedJson['generalName'] as String); } + + EventType.fromJson(Map<String, dynamic> json) + : id = json['id'], + name = json['name']; + + Map<String, dynamic> toJson() => { + 'id': id, + 'name': name, + }; } @@ -243,7 +262,20 @@ class ConfigurationStoreInstance extends ConfigurationStoreBase { abstract class EventStoreBase{ List<Event> events = []; - Event currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', ''); + Event currentEvent = Event( + id:-1, + urn:'urn0', + label:'', + type:'', + typeId:-1, + description:'', + status:'PENDING', + startDate:'', + endDate: '', + latitude: '', + longitude: '', + elevation: '' + ); int getPendingEventCount(){ int count = 0; @@ -266,11 +298,37 @@ class EventStoreInstance extends EventStoreBase { EventStoreInstance._internal() { events = []; - currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', ''); + currentEvent = Event( + id:-1, + urn:'urn0', + label:'', + type:'', + typeId:-1, + description:'', + status:'PENDING', + startDate:'', + endDate: '', + latitude: '', + longitude: '', + elevation: '' + );; } void reset(){ events = []; - currentEvent = Event(0, 'urn0', '', '', -1, '', 'PENDING', '', ''); + currentEvent = Event( + id:-1, + urn:'urn0', + label:'', + type:'', + typeId:-1, + description:'', + status:'PENDING', + startDate:'', + endDate: '', + latitude: '', + longitude: '', + elevation: '' + ); } } diff --git a/lib/main.dart b/lib/main.dart index 4ccf870..4f0019f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -41,6 +41,9 @@ void main() { configuration.loginInformation.password ='adminadmin'; //TODO: replace for productive version. + //events.events.add(Event.fromJson({id: 102, urn: mooring:f9-12, label: gg, type: Calibration, typeId: 15, description: ggg, status: EXPORTED, startDate: 2022-03-25T09:10:46.932955Z, endDate: 2022-03-25T09:10:46.932955Z, latitude: 53.54409252, longitude: 8.58022428, elevation: 52.7396240234375} + //)); + runApp(MaterialApp( title: 'Mobile Event Log', theme: ThemeData( diff --git a/lib/sensorconnector.dart b/lib/sensorconnector.dart index f872dee..3929c61 100644 --- a/lib/sensorconnector.dart +++ b/lib/sensorconnector.dart @@ -70,7 +70,7 @@ class SensorConnector { final response = await http.get(Uri.parse(url)); if (response.statusCode == 200) { eventTypeList = (json.decode(response.body) as List) - .map((i) => EventType.fromJson(i)) + .map((i) => EventType.fromSensorJson(i)) .toList(); debugPrint("Fetching Event Types Success: " + eventTypeList.toString()); } else{ diff --git a/lib/viewevents.dart b/lib/viewevents.dart index 57cf27e..7edbf64 100644 --- a/lib/viewevents.dart +++ b/lib/viewevents.dart @@ -238,6 +238,10 @@ class _ViewEvents extends State<ViewEvents> { icon: null, label: const Text('Sync'), onPressed: () { + for (var event in _events.events){ + debugPrint("Event: " + event.toJson().toString()); + } + syncEvents(); }, ), -- GitLab