<?php
/*
    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
  * The GPSD parts are adepted from the gpsd.php script deployed with GPSD package!
  */

$gpsdHost = "localhost";
$gpsdPort = "2947";

set_time_limit(3);
ini_set('max_execution_time', 3);

function readGpsd(){
	global $gpsdHost, $gpsdPort;

	
	$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;
		}
	}
	@fclose($sock);
	if(!$response){
		$speedOverGround = "-";
		$gpsfix = -2; 
	}
	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'];
		}
	}
	
	return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix, "latitude" => $latitude, "longitude" => $longitude);
}



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);

?>
