| 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 | $errno = "";
|
|---|
| 36 | $errstr = "";
|
|---|
| 37 |
|
|---|
| 38 | $sock = @fsockopen($gpsdHost, $gpsdPort, $errno, $errstr, 2);
|
|---|
| 39 | if(!$sock){
|
|---|
| 40 | $speedOverGround = "-";
|
|---|
| 41 | $gpsfix = -1;
|
|---|
| 42 | }
|
|---|
| 43 | @fwrite($sock, "?WATCH={\"enable\":true}\n");
|
|---|
| 44 | usleep(1000);
|
|---|
| 45 | @fwrite($sock, "?POLL;\n");
|
|---|
| 46 | usleep(1000);
|
|---|
| 47 | for($tries = 0; $tries < 10; $tries++){
|
|---|
| 48 | $response = @fread($sock, 2000); # SKY can be pretty big
|
|---|
| 49 | if (preg_match('/{"class":"POLL".+}/i', $response, $m)){
|
|---|
| 50 | $response = $m[0];
|
|---|
| 51 | break;
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | @fclose($sock);
|
|---|
| 55 | if(!$response){
|
|---|
| 56 | $speedOverGround = "-";
|
|---|
| 57 | $gpsfix = -2;
|
|---|
| 58 | }
|
|---|
| 59 | else{
|
|---|
| 60 | $data = json_decode($response, true);
|
|---|
| 61 | if ($data['class'] != 'POLL'){
|
|---|
| 62 | $speedOverGround = "-";
|
|---|
| 63 | $gpsfix = -3;
|
|---|
| 64 | }
|
|---|
| 65 | if(!array_key_exists('tpv', $data) && is_array($data['tpv'])){
|
|---|
| 66 | $speedOverGround = "-";
|
|---|
| 67 | $gpsfix = -4;
|
|---|
| 68 | }
|
|---|
| 69 | $tpv = $data['tpv'][0];
|
|---|
| 70 |
|
|---|
| 71 | if(!array_key_exists('mode', $tpv)){
|
|---|
| 72 | $gpsfix = -5;
|
|---|
| 73 | }
|
|---|
| 74 | else{
|
|---|
| 75 | switch($tpv['mode']){
|
|---|
| 76 | case 1:
|
|---|
| 77 | $gpsfix = "2D";
|
|---|
| 78 | break;
|
|---|
| 79 | case 2:
|
|---|
| 80 | $gpsfix = "3D";
|
|---|
| 81 | break;
|
|---|
| 82 | default:
|
|---|
| 83 | $gpsfix = "-";
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | if(!array_key_exists('speed', $tpv)){
|
|---|
| 88 | $speedOverGround = "-1";
|
|---|
| 89 | }
|
|---|
| 90 | else{
|
|---|
| 91 | $speedOverGround = $tpv['speed'];
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | mt_srand((double) microtime() * 1000000);
|
|---|
| 101 |
|
|---|
| 102 | $result = array();
|
|---|
| 103 |
|
|---|
| 104 | $gpsdResult = readGpsd();
|
|---|
| 105 |
|
|---|
| 106 | $result["speedthroughwater"] = mt_rand(0, 10);
|
|---|
| 107 | $result["speedoverground"] = $gpsdResult["speedOverGround"];
|
|---|
| 108 | $result["waterdepth"] = mt_rand(1, 30) + 0.5;
|
|---|
| 109 | $result["watertemperature"] = mt_rand(15, 25) + 0.5;
|
|---|
| 110 | $result["gpsfix"] = $gpsdResult["gpsfix"];
|
|---|
| 111 |
|
|---|
| 112 | echo json_encode($result);
|
|---|
| 113 |
|
|---|
| 114 | ?>
|
|---|