Index: src/webapp/webservice/inc/GnuBoat/ServiceData.php
===================================================================
--- src/webapp/webservice/inc/GnuBoat/ServiceData.php	(revision 008bce99c15e3dad6fcf395f8c3a3f69b23b4b28)
+++ src/webapp/webservice/inc/GnuBoat/ServiceData.php	(revision 6b713e6ca5303e406d5a4ac9053c3ef773bb9550)
@@ -29,8 +29,9 @@
 		$this->_configRound = array
 		(
-			"latitude" => 5,
-			"longitude" => 5,
+			"latitude" => 4,
+			"longitude" => 4,
 			"speedthroughwater" => 1,
 			"speedoverground" => 1,
+			"courseoverground" => 1,
 			"waterdepth" => 1,
 			"watertemperature" => 1,
@@ -48,14 +49,11 @@
 		$result["track"] = array();
 
-		if(!file_exists($dbFile)){
-			if($debug > 0)
-				echo "Database file ". $dbFile ." does not exist!";
-			error_log("Database file ". $dbFile ." does not exist");
-			return;
-		}
+		if(!file_exists($dbFile))
+			return $this->error("Database file ". $dbFile ." does not exist");
+
 		// Fetch entries of the last 30s with maximum (available) resolution of 2s
 		$this->addRrdData($dbFile, "2", "240", $result, $lastValid);
-		if($lastValid){
-			$result["lastvalid"] = $lastValid;
+		if($lastValid !== false){
+			$result["lastvalid"] = $lastValid->toArray();
 		}
 		// Fetch entries of the last 1h with maximum (available) resolution of 10s
@@ -64,11 +62,5 @@
 		$this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
 
-		if($debug > 0){
-			echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
-		}
-		else{
-			header('Content-Type: application/json');
-			echo json_encode($result) ."\n";
-		}
+		return $this->output($result);
 	}
 
@@ -81,18 +73,9 @@
 		$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);
-			$message = "Failure reading rrd database ". $dbFile .": ". rrd_error(); 
-			if($debug > 0)
-				echo $message;
-			else
-				error_log($message);
-			return;
-		}
+		if($data === false)
+			return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result); 
 
 		if($debug > 2){
-			echo "<pre>";
 			var_dump($data);
-			echo "</pre>";
 		}
 
@@ -101,54 +84,30 @@
 		// transform them into json format
 		$i = 0;
-		$lastValid = -1;
+		$lastValid = false;
 		$keys = array_keys($data);
 		$keyTimeframes = $keys[0];
 		$itemCount = count($keys);
-		if($itemCount < 0 ){
-			error_log("No keys in fetched rrd data from ". $dbFile);
-			echo json_encode("");
-			exit;
-		}
+		if($itemCount < 0 )
+			return $this->error("No keys in fetched rrd data from ". $dbFile);
+
 		$timeframesCount = count($data[$keyTimeframes]);
-		if($debug > 0){
-			echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
+		if($debug > 1){
+			error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
 		}
 		foreach($data[$keys[0]] as $timeframe => $value){
-			$allValid = true;
-			$gpsFix = false;
-			$tmp = array();
+			$tmp = new TrackItem($timeframe);
 			foreach($keys as $key){
-				if($key == "gpsfix"){
-					switch($data[$key][$timeframe]){
-						case 2:
-							$tmp[$key] = "2D";
-							$gpsFix = true;
-							break;
-						case 3:
-							$tmp[$key] = "3D";
-							$gpsFix = true;
-							break;
-						default:
-							$tmp[$key] = "-";
-					}
-				}
-				else{
-					$tmp[$key] = $data[$key][$timeframe];
-					if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
-						$allValid = false;
-						if($debug > 1){
-							echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
-						}
-					}
-					elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
-						$tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
-				}
+				$value = $data[$key][$timeframe];
+				if(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
+					$value = round($value, $this->_configRound[$key]);
+				if(!$tmp->setProperty($key, $value) && $debug > 1)
+					error_log("Coult not set property ". $key ." to value ". $value ." at ". $timeframe);
 			}
 			// only add the timeframe to the track, if the values are valid and we had a gps fix at that time
-			if($allValid){
+			if($tmp->isValid()){
 				$lastValid = $tmp;
-				if($gpsFix){
+				if($tmp->gpsFixed()){
 					if(!array_key_exists($timeframe, $result["track"])){
-						$result["track"][$timeframe] = $tmp;
+						$result["track"][$timeframe] = $tmp->toArray();
 					}
 				}
