Changeset 3121b3b for webapp/webservice


Ignore:
Timestamp:
Jul 10, 2016, 6:35:41 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
1fec879
Parents:
36be960
Message:
  • Webservice is now able to connect to GPSD
    • Improved the display section in webclient

Signed-off-by: Joachim Langenbach <joachim.langenbach@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • webapp/webservice/index.php

    r36be960 r3121b3b  
    1717*/
    1818
    19 mt_srand(microtime());
     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
     26set_time_limit(3);
     27ini_set('max_execution_time', 3);
     28
     29function 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
     100mt_srand((double) microtime() * 1000000);
    20101
    21102$result = array();
    22103
     104$gpsdResult = readGpsd();
     105
    23106$result["speedthroughwater"] = mt_rand(0, 10);
    24 $result["speedoverground"] = mt_rand(0, 5);
    25 $result["waterdepth"] = mt_rand(1, 30);
    26 $result["watertemperature"] = mt_rand(15, 25);
    27 $result["satellites"] = 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"];
    28111
    29112echo json_encode($result);
Note: See TracChangeset for help on using the changeset viewer.