From fe7378d174d1ec1a035fcb4228c8655193b70f52 Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Fri, 11 Mar 2022 14:11:39 +0100 Subject: [PATCH] sync to sensor partly done. Auth not working yet --- README.md | 4 +- lib/addevent.dart | 9 +++-- lib/configuration.dart | 2 +- lib/datamodel.dart | 19 +++++++++ lib/main.dart | 3 ++ lib/viewevents.dart | 89 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 10a6289..fec890d 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ A cross plattform project for android and ios to log events offline everywhere a - Events added on the addevent.dart page have to be stored locally persistently. They can be displayed in the view events widget. Each event has a flag which shows if the event is already exported to sensor. Shall exported events be displayed here? Slide switch to also show exported events? -- Functionality to export events locally for backup in the field! +- Functionality to export events locally for backup in the field! ## Configuration Page @@ -53,6 +53,6 @@ The fields lat, long, elevation are updated automatically if checkbox is checked a-z , A-Z , _ , 0-9 , ,(Comma) , ( , ) , + , - , . , : -90 => Latitude <= +90 --180 => Latitude <= +180 +-180 => Longitude <= +180 Elevation: any numer diff --git a/lib/addevent.dart b/lib/addevent.dart index 8a15494..a79a847 100644 --- a/lib/addevent.dart +++ b/lib/addevent.dart @@ -465,10 +465,13 @@ class _AddEventPageState extends State<AddEvent> { if (_validateInput()) { _storeCurrentEvent(); HapticFeedback.vibrate(); + + //Update time for next event + var date = DateTime.now().toUtc(); + eventsStore.currentEvent.startDate = '$date'; + eventsStore.currentEvent.endDate = '$date'; } - else { - setState(() {}); - } + setState(() {}); }, tooltip: 'Add Event', child: const Icon(Icons.check), diff --git a/lib/configuration.dart b/lib/configuration.dart index c02ef08..56e3766 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -253,7 +253,7 @@ class _MyHomePageState extends State<Configuration> { icon: const Icon(Icons.cancel), label: const Text('Reset all'), onPressed: () { - events.reset(); + //events.reset(); //TODO: remove before release! configuration.reset(); }, ), diff --git a/lib/datamodel.dart b/lib/datamodel.dart index a4f671d..2ab5425 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -87,6 +87,23 @@ class Event{ ); } + Map<String, dynamic> toSensorJson() => { + 'itemID':id, + 'inheritToAllChildren':'false', + 'inheritToChildren':[], + 'event':{ + 'startDate':startDate, + 'endDate':endDate, + 'label':label, + 'description':description, + 'eventType':type, + 'latitude':latitude, + 'longitude':longitude, + 'elevationInMeter':elevation, + 'id':0 + } + }; + @override String toString(){ return '{ ${this.id}, ' @@ -94,6 +111,8 @@ class Event{ '${this.label}, ' '${this.type}, ' '${this.description}, ' + '${this.startDate}, ' + '${this.endDate}, ' '${this.latitude},' '${this.longitude},' '${this.elevation},' diff --git a/lib/main.dart b/lib/main.dart index 08d841b..9ba93c6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -39,6 +39,7 @@ void main() { //Add some dummy events to event store. Just development purpose. TODO: Remove after development. + /* events.events.add(Event.fromJson({ 'id': 8311, 'urn': 'station:neumayer_iii:awi_snow_sampler_1', @@ -79,6 +80,8 @@ void main() { 'startDate': '2022-03-08T05:29:26Z', 'endDate': '2022-03-08T06:29:26Z' })); + */ + //Add some dummy eventtypes. // TODO: loard from shared preferences. diff --git a/lib/viewevents.dart b/lib/viewevents.dart index 4db21a7..39f3d01 100644 --- a/lib/viewevents.dart +++ b/lib/viewevents.dart @@ -1,6 +1,69 @@ +import 'dart:convert'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'datamodel.dart'; +import 'package:http/http.dart' as http; + +Future<bool> syncEvents() async { + final EventStoreInstance events = EventStoreInstance(); + + String base_url = 'https://sandbox.sensor.awi.de/rest/sensors/events/putEvent/'; + String url = ''; + + debugPrint('Number of Events: ' + events.events.length.toString()); + debugPrint('Pending Events:'); + var index = 0; + for (var event in events.events){ + if (event.status == 'PENDING') { + debugPrint('Idx: ' + index.toString() + ' ' + event.toSensorJson().toString()); + index++; + + url = base_url + event.id.toString() + '?createVersion=false'; + debugPrint('XXX ' + Uri.parse(url).toString()); + final response = await http.put(Uri.parse(url), + headers: { "Accept" : "application/json"}, + body: event.toSensorJson().toString(), + encoding: Encoding.getByName("utf-8"), + ); + + if (response.statusCode == 200) { + debugPrint('put success'); + + debugPrint(response.body.toString()); + debugPrint(response.headers.toString()); + debugPrint(response.headers['set-cookie']); + + + } else { + debugPrint('Header: ' + response.headers.toString()); + debugPrint('Body: ' + response.body.toString()); + debugPrint('StatusCode: ' + response.statusCode.toString()); + throw Exception('Failed to login'); + } + + + + + + } + } + + return true; +} + + + + + + + + + + + + + class ViewEvents extends StatelessWidget { const ViewEvents({Key? key}) : super(key: key); @@ -164,7 +227,12 @@ class ViewEvents extends StatelessWidget { icon: null, label: const Text('Sync'), onPressed: () { + //showDialog( + // context: context, + // builder: (BuildContext context) => _buildPopupDialog(context), + //); + syncEvents(); }, ), ], @@ -173,4 +241,25 @@ class ViewEvents extends StatelessWidget { } } +Widget _buildPopupDialog(BuildContext context) { + return AlertDialog( + title: const Text('Popup example'), + content: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: const <Widget>[ + Text("Hello"), + ], + ), + actions: <Widget>[ + FlatButton( + onPressed: () { + Navigator.of(context).pop(); + }, + textColor: Theme.of(context).primaryColor, + child: const Text('Close'), + ), + ], + ); +} //TODO: allow editing fields here. Check validation of input value! \ No newline at end of file -- GitLab