Index: src/webapp/webservice/index.php
===================================================================
--- src/webapp/webservice/index.php	(revision 6cce084946e683b94554d9b975ee875d382fe5be)
+++ src/webapp/webservice/index.php	(revision 12d7e6f3718223afcb535bbe396575c444a4c529)
@@ -17,174 +17,8 @@
 */
 
-require_once(__DIR__ ."/inc/Config.php");
-$config = new Config();
-$dbFile = $config->databaseFile();
+require_once(__DIR__ ."/inc/autoloader.php");
 
-define("REQUEST_DATA", "data");
-define("REQUEST_SHUTDOWN", "shutdown");
-
-
-// configure roundings per value (key) and precision (value)
-$configRound = array
-(
-	"latitude" => 5,
-	"longitude" => 5,
-	"speedthroughwater" => 1,
-	"speedoverground" => 1,
-	"waterdepth" => 1,
-	"watertemperature" => 1,
-);
-
-function addRrdData($interval, $period, &$result, &$lastValid){
-	global $dbFile;
-	global $debug;
-	global $configRound;
-	$lastValid = false;
-
-	$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);
-		$message = "Failure reading rrd database ". $dbFile .": ". rrd_error(); 
-		if($debug > 0)
-			echo $message;
-		else
-			error_log($message);
-		return;
-	}
-
-	if($debug > 2){
-		echo "<pre>";
-		var_dump($data);
-		echo "</pre>";
-	}
-
-	$data = $data["data"];
-
-	// transform them into json format
-	$i = 0;
-	$lastValid = -1;
-	$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;
-	}
-	$timeframesCount = count($data[$keyTimeframes]);
-	if($debug > 0){
-		echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
-	}
-	foreach($data[$keys[0]] as $timeframe => $value){
-		$allValid = true;
-		$gpsFix = false;
-		$tmp = array();
-		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 ". $time ."<br/>";
-					}
-				}
-				elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
-					$tmp[$key] = round($tmp[$key], $configRound[$key]);
-			}
-		}
-		// only add the timeframe to the track, if the values are valid and we had a gps fix at that time
-		if($allValid){
-			$lastValid = $tmp;
-			if($gpsFix){
-				if(!array_key_exists($timeframe, $result["track"])){
-					$result["track"][$timeframe] = $tmp;
-				}
-			}
-		}
-		else{
-			if($debug > 1){
-				echo "Failed <pre>";
-				var_dump($tmp);
-				echo "</pre>\n";
-			}
-		}
-	}
- 	ksort($result["track"]);
-}
-
-$debug = 0;
-if(array_key_exists("debug", $_GET))
-	$debug = $_GET["debug"];
-
-$request = REQUEST_DATA;
-if(array_key_exists("request", $_GET)){
-	$request = $_GET["request"];
-}
-
-if($debug == 0)
-	header('Content-Type: application/json');
-
-$result = array();
-if($request == REQUEST_SHUTDOWN){
-	$handle = @fopen($config->shutdownRequestFile(), "w");
-	if($handle === false){
-		$message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
-		if($debug > 0)
-			echo $message;
-		error_log($message);
-		$result["shutdownstart"] = -1;
-	}
-	else{
-		$shutdownTime = new DateTime('NOW');
-		$shutdownTime->modify("+15 seconds");
-		fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
-		$result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
-	}
-}
-elseif($request == REQUEST_DATA){
-	$result["lastvalid"] = array();
-	$result["track"] = array();
-
-	if(!file_exists($dbFile)){
-		echo json_encode("");
-		if($debug > 0)
-			echo "Database file ". $dbFile ." does not exist!";
-		error_log("Database file ". $dbFile ." does not exist");
-		exit;
-	}
-	// Fetch entries of the last 30s with maximum (available) resolution of 2s
-	addRrdData("2", "240", $result, $lastValid);
-	if($lastValid){
-		$result["lastvalid"] = $lastValid;
-	}
-	// 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) ."\n";
-}
-else{
-	echo json_encode($result) ."\n";
-}
+$webservice = new GnuBoat\Webservice();
+$webservice->process();
 
 ?>
