Changeset 4ab1c0a for src


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
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webclient/css/main.css

    rf8dfbe7 r4ab1c0a  
    7272        position: absolute;
    7373        top: 2mm;
    74         left: 28mm;
     74        left: 30mm;
    7575}
    7676
    … …  
    8181}
    8282
    83 #display #gps {
     83#display #gpsfix {
    8484        position: absolute;
    8585        top: 21mm;
    … …  
    9090        position: absolute;
    9191        top: 21mm;
    92         left: 28mm;
     92        left: 30mm;
    9393}
    9494
  • src/webapp/webclient/javascript/main.js

    rf8dfbe7 r4ab1c0a  
    2121
    2222var map;
    23 var points = [];
    2423var polyline;
    2524
    … …  
    2928
    3029function displayDataSet(data){
    31         $("#display #speedthroughwater #value").text(data["speedthroughwater"].toString());
    32         $("#display #speedoverground #value").text(data["speedoverground"].toString());
    33         $("#display #waterdepth #value").text(data["waterdepth"].toString());
    34         $("#display #watertemperature #value").text(Math.round(data["watertemperature"]).toString());
    35         $("#display #gps #value").text(data["gpsfix"]);
     30        if(!("lastvalid" in data)){
     31                        console.log("No last valid values given!");
     32        }
     33        else{
     34                var lastValid = data["lastvalid"];
     35                var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"];
     36                for(var i = 0; i < keys.length; i++){
     37                        var key = keys[i];
     38                        var value = lastValid[key];
     39                        if($.isNumeric(value)){
     40                                if(key == "watertemperature"){
     41                                        value = Math.round(value);
     42                                }
     43                                $("#display #"+ keys[i] +" #value").text(value.toString());
     44                        }
     45                        else if(key == "gpsfix"){
     46                                $("#display #"+ keys[i] +" #value").text(value);
     47                        }
     48                }
     49        }
    3650       
    37         var latitude = data["latitude"];
    38         var longitude = data["longitude"];
    39         if(latitude && longitude){
     51        if(!("track" in data)){
     52                        console.log("No track given!");
     53        }
     54        else{
     55                var track = data["track"];
     56                var points = [];
     57                for(var time in track){
     58                        points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"]));
     59                }
    4060                if(points.length > 0){
    41                         var lastPoint = points[points.length - 1];
    42                         if(lastPoint.lat != latitude || lastPoint.lng != longitude){
    43                                 points.push(new L.latLng(latitude, longitude));
    44                                 console.log(points);
     61                        if(!polyline){
     62                                polyline = L.polyline(points, {color: 'red'}).addTo(map);
     63                        }
     64                        else{
    4565                                polyline.setLatLngs(points);
    46                                 map.fitBounds(polyline.getBounds());
    4766                        }
     67                        map.fitBounds(polyline.getBounds());
    4868                }
    4969        }
    … …  
    80100                attribution: 'undefined',opacity: 0.85
    81101                }).addTo(map);
    82                
    83         points.push(new L.latLng(47.5914051, 10.710862));
    84         points.push(new L.latLng(47.576667, 10.735278));
    85         polyline = L.polyline(points, {color: 'red'}).addTo(map);
    86102}
    87103
  • src/webapp/webclient/root/index.php

    rf8dfbe7 r4ab1c0a  
    8383                        <span id="value">-</span>&#176;C
    8484                </div>
    85                 <div id="gps">
     85                <div id="gpsfix">
    8686                        <h2>Fix</h2>
    8787                        <span id="value">-</span>
  • 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.