Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webservice/inc/GnuBoat/ServiceData.php

    r0662b2d r1ba85cf  
    2929                $this->_configRound = array
    3030                (
    31                         "latitude" => 4,
    32                         "longitude" => 4,
     31                        "latitude" => 5,
     32                        "longitude" => 5,
    3333                        "speedthroughwater" => 1,
    3434                        "speedoverground" => 1,
    35                         "courseoverground" => 1,
    3635                        "waterdepth" => 1,
    3736                        "watertemperature" => 1,
    … …  
    4948                $result["track"] = array();
    5049
    51                 if(!file_exists($dbFile))
    52                         return $this->error("Database file ". $dbFile ." does not exist");
    53 
     50                if(!file_exists($dbFile)){
     51                        echo json_encode("");
     52                        if($debug > 0)
     53                                echo "Database file ". $dbFile ." does not exist!";
     54                        error_log("Database file ". $dbFile ." does not exist");
     55                        exit;
     56                }
    5457                // Fetch entries of the last 30s with maximum (available) resolution of 2s
    5558                $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
    56                 if($lastValid !== false){
    57                         $result["lastvalid"] = $lastValid->toArray();
     59                if($lastValid){
     60                        $result["lastvalid"] = $lastValid;
    5861                }
    5962                // Fetch entries of the last 1h with maximum (available) resolution of 10s
    … …  
    6265                $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
    6366
    64                 return $this->output($result);
     67                if($debug > 0){
     68                        echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
     69                }
     70                else{
     71                        header('Content-Type: application/json');
     72                        echo json_encode($result) ."\n";
     73                }
    6574        }
    6675
    … …  
    7382                $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    7483                // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
    75                 if($data === false)
    76                         return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result);
     84                if($data === false){
     85                        echo json_encode($result);
     86                        $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
     87                        if($debug > 0)
     88                                echo $message;
     89                        else
     90                                error_log($message);
     91                        return;
     92                }
    7793
    7894                if($debug > 2){
     95                        echo "<pre>";
    7996                        var_dump($data);
     97                        echo "</pre>";
    8098                }
    8199
    … …  
    84102                // transform them into json format
    85103                $i = 0;
    86                 $lastValid = false;
     104                $lastValid = -1;
    87105                $keys = array_keys($data);
    88106                $keyTimeframes = $keys[0];
    89107                $itemCount = count($keys);
    90                 if($itemCount < 0 )
    91                         return $this->error("No keys in fetched rrd data from ". $dbFile);
    92 
     108                if($itemCount < 0 ){
     109                        error_log("No keys in fetched rrd data from ". $dbFile);
     110                        echo json_encode("");
     111                        exit;
     112                }
    93113                $timeframesCount = count($data[$keyTimeframes]);
    94                 if($debug > 1){
    95                         error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
     114                if($debug > 0){
     115                        echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
    96116                }
    97117                foreach($data[$keys[0]] as $timeframe => $value){
    98                         $tmp = new TrackItem($timeframe);
     118                        $allValid = true;
     119                        $gpsFix = false;
     120                        $tmp = array();
    99121                        foreach($keys as $key){
    100                                 $value = $data[$key][$timeframe];
    101                                 if(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
    102                                         $value = round($value, $this->_configRound[$key]);
    103                                 if(!$tmp->setProperty($key, $value) && $debug > 1)
    104                                         error_log("Coult not set property ". $key ." to value ". $value ." at ". $timeframe);
     122                                if($key == "gpsfix"){
     123                                        switch($data[$key][$timeframe]){
     124                                                case 2:
     125                                                        $tmp[$key] = "2D";
     126                                                        $gpsFix = true;
     127                                                        break;
     128                                                case 3:
     129                                                        $tmp[$key] = "3D";
     130                                                        $gpsFix = true;
     131                                                        break;
     132                                                default:
     133                                                        $tmp[$key] = "-";
     134                                        }
     135                                }
     136                                else{
     137                                        $tmp[$key] = $data[$key][$timeframe];
     138                                        if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
     139                                                $allValid = false;
     140                                                if($debug > 1){
     141                                                        echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
     142                                                }
     143                                        }
     144                                        elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
     145                                                $tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
     146                                }
    105147                        }
    106148                        // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    107                         if($tmp->isValid()){
     149                        if($allValid){
    108150                                $lastValid = $tmp;
    109                                 if($tmp->gpsFixed()){
     151                                if($gpsFix){
    110152                                        if(!array_key_exists($timeframe, $result["track"])){
    111                                                 $result["track"][$timeframe] = $tmp->toArray();
     153                                                $result["track"][$timeframe] = $tmp;
    112154                                        }
    113155                                }
Note: See TracChangeset for help on using the changeset viewer.