Ignore:
Timestamp:
Jul 12, 2016, 6:39:45 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
d0e98d5
Parents:
f8dfbe7
Message:
  • Added gnuboat python script, which collects the data from gpsd into a rrd database for the webservice
    • Adapted the webservice
    • Adapted the webclient

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

Location:
src/webapp/webservice
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webservice/index.php

    rf8dfbe7 r4ab1c0a  
    1717*/
    1818
    19 /**
    20   * The GPSD parts are adepted from the gpsd.php script deployed with GPSD package!
    21   */
     19require_once(__DIR__ ."/inc/Config.php");
     20$config = new Config();
     21$dbFile = $config->databaseFile();
    2222
    23 $gpsdHost = "localhost";
    24 $gpsdPort = "2947";
     23// configure roundings per value (key) and precision (value)
     24$configRound = array
     25(
     26        "latitude" => 5,
     27        "longitude" => 5,
     28        "speedthroughwater" => 1,
     29        "speedoverground" => 1,
     30        "waterdepth" => 1,
     31        "watertemperature" => 1,
     32);
    2533
    26 set_time_limit(3);
    27 ini_set('max_execution_time', 3);
     34$debug = 0;
     35if(array_key_exists("debug", $_GET))
     36        $debug = $_GET["debug"];
    2837
    29 function readGpsd(){
    30         global $gpsdHost, $gpsdPort;
     38if($debug == 0)
     39        header('Content-Type: application/json');
    3140
    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;
     41$result["lastvalid"] = array();
     42$result["track"] = array();
     43
     44// Fetch entries of the last 12hours
     45$data = rrd_fetch($dbFile, array( "AVERAGE", "--start", "-12h"));
     46if($data === false){
     47        echo json_encode($result);
     48        $message = "Could not open rrd database ". $dbFile;
     49        if($debug > 0)
     50                echo $message;
     51        else
     52                error_log($message);
     53        exit;
     54}
     55
     56if($debug > 2){
     57        echo "<pre>";
     58        var_dump($data);
     59        echo "</pre>";
     60}
     61
     62$data = $data["data"];
     63
     64// transform them into json format
     65$i = 0;
     66$lastTime = -1;
     67$keys = array_keys($data);
     68if(count($keys) < 0 ){
     69        error_log("No keys in fetched rrd data from ". $dbFile);
     70        echo json_encode("");
     71        exit;
     72}
     73for($i = 0; $i < count($data[$keys[0]]); $i++){
     74        $time = array_keys($data[$keys[0]])[$i];
     75        $allValid = true;
     76        $tmp = array();
     77        foreach($keys as $key){
     78                if($key == "gpsfix"){
     79                        switch($data[$key][$time]){
     80                                case 1:
     81                                        $tmp[$key] = "2D";
     82                                        break;
     83                                case 2:
     84                                        $tmp[$key] = "3D";
     85                                        break;
     86                                default:
     87                                        $tmp[$key] = "-";
     88                        }
     89                }
     90                else{
     91                        $tmp[$key] = $data[$key][$time];
     92                        if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
     93                                $allValid = false;
     94                        }
     95                        elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
     96                                $tmp[$key] = round($tmp[$key], $configRound[$key]);
    5497                }
    5598        }
    56         @fclose($sock);
    57         if(!$response){
    58                 $speedOverGround = "-";
    59                 $gpsfix = -2;
     99        // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
     100        if($allValid && $tmp["gpsfix"] != "-"){
     101                $result["track"][$time] = $tmp;
     102                $lastTime = $time;
    60103        }
    61104        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'];
     105                if($debug > 0){
     106                        echo "Failed <pre>";
     107                        var_dump($tmp);
     108                        echo "</pre>\n";
    107109                }
    108110        }
    109        
    110         return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix, "latitude" => $latitude, "longitude" => $longitude);
     111}
     112if($lastTime > -1){
     113        $result["lastvalid"] = $result["track"][$lastTime];
    111114}
    112115
    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);
     116if($debug > 0){
     117        echo json_encode($result,  JSON_PRETTY_PRINT);
     118}
     119else{
     120        echo json_encode($result);
     121}
    130122
    131123?>
Note: See TracChangeset for help on using the changeset viewer.