| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software: you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * The GPSD parts are adepted from the gpsd.php script deployed with GPSD package!
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | $gpsdHost = "localhost";
|
|---|
| 24 | $gpsdPort = "2947";
|
|---|
| 25 |
|
|---|
| 26 | set_time_limit(3);
|
|---|
| 27 | ini_set('max_execution_time', 3);
|
|---|
| 28 |
|
|---|
| 29 | function readGpsd(){
|
|---|
| 30 | global $gpsdHost, $gpsdPort;
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | $speedOverGround = "-";
|
|---|
| 34 | $gpsfix = "-";
|
|---|
| 35 | $latitude = "";
|
|---|
| 36 | $longitude = "";
|
|---|
| 37 | $errno = "";
|
|---|
| 38 | $errstr = "";
|
|---|
| 39 |
|
|---|
| 40 | $sock = @fsockopen($gpsdHost, $gpsdPort, $errno, $errstr, 2);
|
|---|
| 41 | if(!$sock){
|
|---|
| 42 | $speedOverGround = "-";
|
|---|
| 43 | $gpsfix = -1;
|
|---|
| 44 | }
|
|---|
| 45 | @fwrite($sock, "?WATCH={\"enable\":true}\n");
|
|---|
| 46 | usleep(1000);
|
|---|
| 47 | @fwrite($sock, "?POLL;\n");
|
|---|
| 48 | usleep(1000);
|
|---|
| 49 | for($tries = 0; $tries < 10; $tries++){
|
|---|
| 50 | $response = @fread($sock, 2000); # SKY can be pretty big
|
|---|
| 51 | if (preg_match('/{"class":"POLL".+}/i', $response, $m)){
|
|---|
| 52 | $response = $m[0];
|
|---|
| 53 | break;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | @fclose($sock);
|
|---|
| 57 | if(!$response){
|
|---|
| 58 | $speedOverGround = "-";
|
|---|
| 59 | $gpsfix = -2;
|
|---|
| 60 | }
|
|---|
| 61 | else{
|
|---|
| 62 | $data = json_decode($response, true);
|
|---|
| 63 | if ($data['class'] != 'POLL'){
|
|---|
| 64 | $speedOverGround = "-";
|
|---|
| 65 | $gpsfix = -3;
|
|---|
| 66 | }
|
|---|
| 67 | if(!array_key_exists('tpv', $data) && is_array($data['tpv'])){
|
|---|
| 68 | $speedOverGround = "-";
|
|---|
| 69 | $gpsfix = -4;
|
|---|
| 70 | }
|
|---|
| 71 | $tpv = $data['tpv'][0];
|
|---|
| 72 |
|
|---|
| 73 | if(!array_key_exists('mode', $tpv)){
|
|---|
| 74 | $gpsfix = -5;
|
|---|
| 75 | }
|
|---|
| 76 | else{
|
|---|
| 77 | switch($tpv['mode']){
|
|---|
| 78 | case 1:
|
|---|
| 79 | $gpsfix = "2D";
|
|---|
| 80 | break;
|
|---|
| 81 | case 2:
|
|---|
| 82 | $gpsfix = "3D";
|
|---|
| 83 | break;
|
|---|
| 84 | default:
|
|---|
| 85 | $gpsfix = "-";
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | if(!array_key_exists('speed', $tpv)){
|
|---|
| 90 | $speedOverGround = "-1";
|
|---|
| 91 | }
|
|---|
| 92 | else{
|
|---|
| 93 | $speedOverGround = $tpv['speed'];
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if(!array_key_exists('lat', $tpv)){
|
|---|
| 97 | $latitude = "";
|
|---|
| 98 | }
|
|---|
| 99 | else{
|
|---|
| 100 | $latitude = $tpv['lat'];
|
|---|
| 101 | }
|
|---|
| 102 | if(!array_key_exists('lon', $tpv)){
|
|---|
| 103 | $longitude = "";
|
|---|
| 104 | }
|
|---|
| 105 | else{
|
|---|
| 106 | $longitude = $tpv['lon'];
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix, "latitude" => $latitude, "longitude" => $longitude);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | mt_srand((double) microtime() * 1000000);
|
|---|
| 116 |
|
|---|
| 117 | $result = array();
|
|---|
| 118 |
|
|---|
| 119 | $gpsdResult = readGpsd();
|
|---|
| 120 |
|
|---|
| 121 | $result["speedthroughwater"] = mt_rand(0, 10);
|
|---|
| 122 | $result["speedoverground"] = $gpsdResult["speedOverGround"];
|
|---|
| 123 | $result["waterdepth"] = mt_rand(1, 30) + 0.5;
|
|---|
| 124 | $result["watertemperature"] = mt_rand(15, 25) + 0.5;
|
|---|
| 125 | $result["gpsfix"] = $gpsdResult["gpsfix"];
|
|---|
| 126 | $result["latitude"] = $gpsdResult["latitude"];
|
|---|
| 127 | $result["longitude"] = $gpsdResult["longitude"];
|
|---|
| 128 |
|
|---|
| 129 | echo json_encode($result);
|
|---|
| 130 |
|
|---|
| 131 | ?>
|
|---|