Index: src/webapp/webservice/index.php
===================================================================
--- src/webapp/webservice/index.php	(revision f8dfbe72eeba7737d98e56167476b6ab0a8c9391)
+++ src/webapp/webservice/index.php	(revision ed749dc571701c95c2c09e504a03db2ab81356ed)
@@ -17,115 +17,107 @@
 */
 
-/**
-  * The GPSD parts are adepted from the gpsd.php script deployed with GPSD package!
-  */
+require_once(__DIR__ ."/inc/Config.php");
+$config = new Config();
+$dbFile = $config->databaseFile();
 
-$gpsdHost = "localhost";
-$gpsdPort = "2947";
+// configure roundings per value (key) and precision (value)
+$configRound = array
+(
+	"latitude" => 5,
+	"longitude" => 5,
+	"speedthroughwater" => 1,
+	"speedoverground" => 1,
+	"waterdepth" => 1,
+	"watertemperature" => 1,
+);
 
-set_time_limit(3);
-ini_set('max_execution_time', 3);
+$debug = 0;
+if(array_key_exists("debug", $_GET))
+	$debug = $_GET["debug"];
 
-function readGpsd(){
-	global $gpsdHost, $gpsdPort;
+if($debug == 0)
+	header('Content-Type: application/json');
 
-	
-	$speedOverGround = "-";
-	$gpsfix = "-";
-	$latitude = "";
-	$longitude = "";
-	$errno = "";
-	$errstr = "";
-	
-	$sock = @fsockopen($gpsdHost, $gpsdPort, $errno, $errstr, 2);
-	if(!$sock){
-		$speedOverGround = "-";
-		$gpsfix = -1; 
-	}
-	@fwrite($sock, "?WATCH={\"enable\":true}\n");
-	usleep(1000);
-	@fwrite($sock, "?POLL;\n");
-	usleep(1000);
-	for($tries = 0; $tries < 10; $tries++){
-		$response = @fread($sock, 2000); # SKY can be pretty big
-		if (preg_match('/{"class":"POLL".+}/i', $response, $m)){
-			$response = $m[0];
-			break;
+$result["lastvalid"] = array();
+$result["track"] = array();
+
+// Fetch entries of the last 12hours
+$data = rrd_fetch($dbFile, array( "AVERAGE", "--start", "-12h"));
+if($data === false){
+	echo json_encode($result);
+	$message = "Could not open rrd database ". $dbFile; 
+	if($debug > 0)
+		echo $message;
+	else
+		error_log($message);
+	exit;
+}
+
+if($debug > 2){
+	echo "<pre>";
+	var_dump($data);
+	echo "</pre>";
+}
+
+$data = $data["data"];
+
+// transform them into json format
+$i = 0;
+$lastTime = -1;
+$keys = array_keys($data);
+if(count($keys) < 0 ){
+	error_log("No keys in fetched rrd data from ". $dbFile);
+	echo json_encode("");
+	exit;
+}
+for($i = 0; $i < count($data[$keys[0]]); $i++){
+	$time = array_keys($data[$keys[0]])[$i];
+	$allValid = true;
+	$tmp = array();
+	foreach($keys as $key){
+		if($key == "gpsfix"){
+			switch($data[$key][$time]){
+				case 1:
+					$tmp[$key] = "2D";
+					break;
+				case 2:
+					$tmp[$key] = "3D";
+					break;
+				default:
+					$tmp[$key] = "-";
+			}
+		}
+		else{
+			$tmp[$key] = $data[$key][$time];
+			if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
+				$allValid = false;
+			}
+			elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
+				$tmp[$key] = round($tmp[$key], $configRound[$key]);
 		}
 	}
-	@fclose($sock);
-	if(!$response){
-		$speedOverGround = "-";
-		$gpsfix = -2; 
+	// only add the timeframe to the track, if the values are valid and we had a gps fix at that time
+	if($allValid && $tmp["gpsfix"] != "-"){
+		$result["track"][$time] = $tmp;
+		$lastTime = $time;
 	}
 	else{
-		$data = json_decode($response, true);
-		if ($data['class'] != 'POLL'){
-			$speedOverGround = "-";
-			$gpsfix = -3; 
-		}
-		if(!array_key_exists('tpv', $data) && is_array($data['tpv'])){
-			$speedOverGround = "-";
-			$gpsfix = -4;
-		}
-		$tpv = $data['tpv'][0];
-
-		if(!array_key_exists('mode', $tpv)){
-			$gpsfix = -5;
-		}
-		else{
-			switch($tpv['mode']){
-				case 1:
-					$gpsfix = "2D";
-					break;
-				case 2:
-					$gpsfix = "3D";
-					break;
-				default:
-					$gpsfix = "-";
-			}
-		}
-		
-		if(!array_key_exists('speed', $tpv)){
-			$speedOverGround = "-1";
-		}
-		else{
-			$speedOverGround = $tpv['speed'];
-		}
-		
-		if(!array_key_exists('lat', $tpv)){
-			$latitude = "";
-		}
-		else{
-			$latitude = $tpv['lat'];
-		}
-		if(!array_key_exists('lon', $tpv)){
-			$longitude = "";
-		}
-		else{
-			$longitude = $tpv['lon'];
+		if($debug > 0){
+			echo "Failed <pre>";
+			var_dump($tmp);
+			echo "</pre>\n";
 		}
 	}
-	
-	return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix, "latitude" => $latitude, "longitude" => $longitude);
+}
+if($lastTime > -1){
+	$result["lastvalid"] = $result["track"][$lastTime];
 }
 
-
-
-mt_srand((double) microtime() * 1000000);
-
-$result = array();
-
-$gpsdResult = readGpsd();
-
-$result["speedthroughwater"] = mt_rand(0, 10);
-$result["speedoverground"] = $gpsdResult["speedOverGround"];
-$result["waterdepth"] = mt_rand(1, 30) + 0.5;
-$result["watertemperature"] = mt_rand(15, 25) + 0.5;
-$result["gpsfix"] = $gpsdResult["gpsfix"];
-$result["latitude"] = $gpsdResult["latitude"];
-$result["longitude"] = $gpsdResult["longitude"];
-
-echo json_encode($result);
+if($debug > 0){
+	echo json_encode($result,  JSON_PRETTY_PRINT);
+}
+else{
+	echo json_encode($result);
+}
 
 ?>
