From 02b8797e9bcd7c82b06669d815f73dcf7146e153 Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Fri, 25 Mar 2022 11:47:11 +0100 Subject: [PATCH] minor changes --- lib/configuration.dart | 73 +++++++++++++++++++++++++--------------- lib/datamodel.dart | 33 ++++++++++-------- lib/main.dart | 12 ++++--- lib/sensorconnector.dart | 11 ++++-- lib/viewevents.dart | 2 +- 5 files changed, 81 insertions(+), 50 deletions(-) diff --git a/lib/configuration.dart b/lib/configuration.dart index f47d5f5..5cb7416 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -147,36 +149,51 @@ class _MyHomePageState extends State<Configuration> { icon: const Icon(Icons.save), label: const Text('Store'), onPressed: () { - futureAuthToken.whenComplete((){ - futureDevices.whenComplete(() { - //Update configuration with newly fetched data - futureDevices.then((value) => configuration.devices = value); - futureEventTypes.then((value) => configuration.eventTypes = value); - print("Devices: " + configuration.devices.toString()); //Still empty on first execution Why? - // TODO: Move fetchCollectionDevices to an own future and set configuration inside on finish! - try { - //Update id and urn for the add event widget with some initial data - events.currentEvent.id = configuration.devices[0].id; //TODO: fix if devices are an empty list. - events.currentEvent.urn = configuration.devices[0].urn; - events.currentEvent.description = ''; - events.currentEvent.label = ''; - events.currentEvent.type = configuration.eventTypes[0].name; - - var date = DateTime.now().toUtc(); - var isodata = date.toIso8601String(); - events.currentEvent.startDate = isodata; - events.currentEvent.endDate = isodata; - configuration.initialized = true; - - HapticFeedback.vibrate(); - //TODO: display success - } catch(e){ - throw Exception('Something went wrong try again'); - } + futureAuthToken.then((value){ //NOTE: Counterintuitive async callback function + debugPrint("Login credentials correct! Auth Token: " + value); + + futureDevices.then((value) { + debugPrint("Device List complete"); + configuration.devices = value; + + futureEventTypes.then((value) { + debugPrint("Event Types List complete"); + configuration.eventTypes = value; + + debugPrint("EventType Json: " + jsonEncode( + configuration.eventTypes[0])); + debugPrint("Device Json: " + jsonEncode( + configuration.devices[0])); + debugPrint("Current Collection Json: " + jsonEncode( + configuration.currentCollection)); + debugPrint("Sensor Login Json: " + jsonEncode( + configuration.loginInformation)); + + try { + //Update id and urn for the add event widget with some initial data + events.currentEvent.id = configuration.devices[0] + .id; //TODO: fix if devices are an empty list. + events.currentEvent.urn = configuration.devices[0].urn; + events.currentEvent.description = ''; + events.currentEvent.label = ''; + events.currentEvent.type = configuration.eventTypes[0] + .name; + + var date = DateTime.now().toUtc(); + var isoDate = date.toIso8601String(); + events.currentEvent.startDate = isoDate; + events.currentEvent.endDate = isoDate; + configuration.initialized = true; + + HapticFeedback.vibrate(); + //TODO: display success user feedback + } catch (e) { + throw Exception('Something went wrong try again'); + } + }); }); }); - - //TODO: display failed here somehow + //TODO: display failed as user feedback somehow }, ), ], diff --git a/lib/datamodel.dart b/lib/datamodel.dart index a14e6ba..8664c17 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -49,7 +49,7 @@ class Device{ @override String toString(){ - return '{ ${this.id}, ${this.urn} }'; + return urn; } } @@ -155,14 +155,14 @@ class EventType{ int id; String name; - EventType( - this.id, - this.name - ); + EventType({ + required this.id, + required this.name + }); - factory EventType.fromSensorJson(Map<String, dynamic> parsedJson){ - return EventType(parsedJson['id'] as int, parsedJson['generalName'] as String); - } + EventType.fromSensorJson(Map<String, dynamic> json) + : id = json['id'], + name = json['generalName']; EventType.fromJson(Map<String, dynamic> json) : id = json['id'], @@ -172,6 +172,11 @@ class EventType{ 'id': id, 'name': name, }; + + @override + String toString(){ + return name; + } } @@ -184,14 +189,14 @@ class SensorLogin{ this.password, ); - String toJsonString() { - return '{ "username": $mail, "authPassword": $password }'; - } - Map<String, dynamic> toJson() => { - "username": mail, - "authPassword": password + "mail": mail, + "password": password }; + + SensorLogin.fromJson(Map<String, dynamic> json) + : mail = json['mail'], + password = json['password']; } diff --git a/lib/main.dart b/lib/main.dart index 4f0019f..f4e9985 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -37,12 +37,16 @@ void main() { events.reset(); // TODO: load configuration and events from shared preferences. configuration.reset(); - configuration.loginInformation.mail = 'admin'; // Sandbox.sensor.de admin account - configuration.loginInformation.password ='adminadmin'; //TODO: replace for productive version. + configuration.loginInformation = SensorLogin.fromJson({"mail":"admin","password":"adminadmin"}); //TODO: replace for productive version. + //Load data from storage: + //events.currentEvent = Event.fromJson({"id":102,"urn":"mooring:f9-12","label":"gg","type":"Calibration","typeId":15,"description":"gvg","status":"EXPORTED","startDate":"2022-03-25T09:24:48.856120Z","endDate":"2022-03-25T09:24:48.856120Z","latitude":"53.54388522","longitude":"8.58144825","elevation":"1.9925537109375"}); + //events.events.add(Event.fromJson({"id":102,"urn":"mooring:f9-12","label":"gg","type":"Calibration","typeId":15,"description":"gvg","status":"EXPORTED","startDate":"2022-03-25T09:24:48.856120Z","endDate":"2022-03-25T09:24:48.856120Z","latitude":"53.54388522","longitude":"8.58144825","elevation":"1.9925537109375"})); + //configuration.devices.add(Device.fromJson({"id":102,"urn":"mooring:f9-12"})); + //configuration.eventTypes.add(EventType.fromJson({"id":15,"name":"Calibration"})); + //configuration.currentCollection = Collection.fromJson({"id":1,"description":"","collectionName":"FRAM"}); + //configuration.initialized = true; - //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', diff --git a/lib/sensorconnector.dart b/lib/sensorconnector.dart index 3929c61..5df77c3 100644 --- a/lib/sensorconnector.dart +++ b/lib/sensorconnector.dart @@ -69,9 +69,14 @@ class SensorConnector { final response = await http.get(Uri.parse(url)); if (response.statusCode == 200) { - eventTypeList = (json.decode(response.body) as List) - .map((i) => EventType.fromSensorJson(i)) - .toList(); + //eventTypeList = (json.decode(response.body) as List) + // .map((i) => EventType.fromSensorJson(i)) + // .toList(); + + List<dynamic> data = json.decode(response.body); + for (var entry in data) { + eventTypeList.add(EventType.fromSensorJson(entry)); + } debugPrint("Fetching Event Types Success: " + eventTypeList.toString()); } else{ throw Exception('Failed to fetch event types. ' diff --git a/lib/viewevents.dart b/lib/viewevents.dart index 7edbf64..a773fc1 100644 --- a/lib/viewevents.dart +++ b/lib/viewevents.dart @@ -239,7 +239,7 @@ class _ViewEvents extends State<ViewEvents> { label: const Text('Sync'), onPressed: () { for (var event in _events.events){ - debugPrint("Event: " + event.toJson().toString()); + debugPrint("Event: " + jsonEncode(event)); } syncEvents(); -- GitLab