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

rasperry rest querry working

parent e9b2850c
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import 'datamodel.dart';
import 'databaseconnector.dart';
import 'dart:async';
import 'package:geolocator/geolocator.dart';
import 'restdata.dart';
class AddEventKottasPegel extends StatefulWidget {
const AddEventKottasPegel({Key? key}) : super(key: key);
......@@ -16,16 +17,20 @@ class AddEventKottasPegel extends StatefulWidget {
class _AddEventKottasPegelPageState extends State<AddEventKottasPegel> {
RestData restConnector = RestData();
List<String> measurementStatusItems = ['ok', 'broken', 'missing' 'tilted',];
List<String> angleStatusItems = ['90° (straight)', '80°', '70°', '60°', '50°', '40°', '30°', ];
//Parameters which shall be added to the measurement event as json string data
String measurementStatus = '';
String angleStatus = '';
int rawMeasurementValue = 0;
double lengthOld = 0.0;
double lengthNew = 0.0;
bool syncGNSSData = true;
bool _addButtonEnabled = true;
late String long = "";
......@@ -43,18 +48,34 @@ class _AddEventKottasPegelPageState extends State<AddEventKottasPegel> {
late Timer _overlayCloseTimer;
Future startRest() async {
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
final EventStoreInstance event = EventStoreInstance();
/* Function which starts querying the URL configuration.restRequestUrl */
restQueryTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
// Querry Rest here
restQueryTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
// Query data from raspberry pi here
try{
Map data = await restConnector.fetchData(configuration.restRequestUrl);
debugPrint("Rest Query succeeded: $data");
event.currentEvent.latitude = data['lat'];
event.currentEvent.longitude = data['lon'];
event.currentEvent.startDate = data['timestamp'];
rawMeasurementValue = data['length']; //in mm
accuracy = double.parse(data['precision'].toString());
//debugPrint("Rest Query succeeded: $data");
//{latPole: 70.66619463069793, lonPole: 8.291692251935759, timestamp: 2022/09/20 12:19:22 UTC, satsInView: 5}
}catch(e){
debugPrint("Rest Query failed $e");
}
debugPrint("Rest Querry..");
});
debugPrint("left startRest");
});
}
Future startGNSS() async {
......@@ -119,19 +140,17 @@ class _AddEventKottasPegelPageState extends State<AddEventKottasPegel> {
final EventStoreInstance event = EventStoreInstance();
final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
startGNSS();
startGNSS(); //Smartphones internal GNSS
//TODO: start async 1s rest poll to get length and position value from raspberries rest url
startRest();
startRest(); //To query data from raspberry Pi measurement
//Update current event with prefix and cnt
event.currentEvent.label = configuration.labelConfig.prefix + configuration.labelConfig.cnt.toString();
//Set the device URN for the event fixed here! TODO: create device in sensor and add to a collection!
//event.currentEvent.urn
//Set the device URN for the event fixed here! TODO: create device in sensor, add to a collection!
//event.currentEvent.urn TODO: set the urn here!
//event.currentEvent.urnId
super.initState();
......
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
......@@ -5,3 +6,30 @@ import 'package:http/http.dart' as http;
import 'datamodel.dart';
class RestData {
Future<Map> fetchData(String url) async {
List<Collection> collectionList = [];
Map valueMap;
try {
final response = await http.get(Uri.parse(url)).timeout(
const Duration(milliseconds: 500));
if (response.statusCode == 200) {
valueMap = json.decode(response.body) ;
return valueMap;
} else {
throw Exception('Failed to fetch Data. Header: ${response
.headers} StatusCode: ${response.statusCode} Body: ${response
.body}');
}
} on SocketException catch (e) {
throw Exception('No connection');
} on TimeoutException catch (e) {
throw Exception('Connection Timeout');
} catch (e) {
rethrow;
}
}
}
\ No newline at end of file
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