Index: src/webapp/webservice/index.php
===================================================================
--- src/webapp/webservice/index.php	(revision 9be4f3f930159f7ac2f4226754d722785db58342)
+++ src/webapp/webservice/index.php	(revision c0782d972ec42b8efb00dfb9194cde154dac763f)
@@ -1,5 +1,5 @@
 <?php
 /*
-    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
+    Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
 
     This program is free software: you can redistribute it and/or modify
@@ -36,10 +36,14 @@
 );
 
-function addRrdData($interval, $period, &$result, &$lastTime){
+function addRrdData($interval, $period, &$result, &$lastValid){
 	global $dbFile;
 	global $debug;
 	global $configRound;
+	$lastValid = false;
 
-	$data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
+	$endTime = floor(time() / $interval) * $interval;
+        $startTime = $endTime - $period;
+	$data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
+	// $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
 	if($data === false){
 		echo json_encode($result);
@@ -49,4 +53,5 @@
 		else
 			error_log($message);
+		return;
 	}
 
@@ -71,16 +76,18 @@
 		echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
 	}
-	for($i = 0; $i < count($data[$keys[0]]); $i++){
-		$time = array_keys($data[$keys[0]])[$i];
+	foreach($data[$keys[0]] as $timeframe => $value){
 		$allValid = true;
+		$gpsFix = false;
 		$tmp = array();
 		foreach($keys as $key){
 			if($key == "gpsfix"){
-				switch($data[$key][$time]){
-					case 1:
+				switch($data[$key][$timeframe]){
+					case 2:
 						$tmp[$key] = "2D";
+						$gpsFix = true;
 						break;
-					case 2:
+					case 3:
 						$tmp[$key] = "3D";
+						$gpsFix = true;
 						break;
 					default:
@@ -89,5 +96,5 @@
 			}
 			else{
-				$tmp[$key] = $data[$key][$time];
+				$tmp[$key] = $data[$key][$timeframe];
 				if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
 					$allValid = false;
@@ -102,8 +109,10 @@
 		// only add the timeframe to the track, if the values are valid and we had a gps fix at that time
 		if($allValid){
- 			if(!array_key_exists($time, $result["track"])){
-				$result["track"][$time] = $tmp;
- 			}
-			$lastTime = $time;
+			$lastValid = $tmp;
+			if($gpsFix){
+				if(!array_key_exists($timeframe, $result["track"])){
+					$result["track"][$timeframe] = $tmp;
+				}
+			}
 		}
 		else{
@@ -115,5 +124,4 @@
 		}
 	}
-
  	ksort($result["track"]);
 }
@@ -159,18 +167,20 @@
 		exit;
 	}
-	// Fetch entries of the last 30 seconds
-	addRrdData("5s", "30s", $result, $lastTime);
-	if($lastTime > -1){
-		$result["lastvalid"] = $result["track"][$lastTime];
+	// Fetch entries of the last 30s with maximum (available) resolution of 2s
+	addRrdData("2", "240", $result, $lastValid);
+	if($lastValid){
+		$result["lastvalid"] = $lastValid;
 	}
- 	addRrdData("15s", "5minutes", $result, $lastTime);
- 	addRrdData("30s", "12h", $result, $lastTime);
+	// Fetch entries of the last 1h with maximum (available) resolution of 10s
+ 	addRrdData("10", "36000", $result, $lastTime);
+ 	// Fetch entries of the last 24h with maximum (available) resolution of 60s
+ 	addRrdData("60", "86400", $result, $lastTime);
 }
 
 if($debug > 0){
-	echo json_encode($result,  JSON_PRETTY_PRINT);
+	echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
 }
 else{
-	echo json_encode($result);
+	echo json_encode($result) ."\n";
 }
 
