Ignore:
File:
1 edited

Legend:

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

    r1ba85cf r0662b2d  
    2929                $this->_configRound = array
    3030                (
    31                         "latitude" => 5,
    32                         "longitude" => 5,
     31                        "latitude" => 4,
     32                        "longitude" => 4,
    3333                        "speedthroughwater" => 1,
    3434                        "speedoverground" => 1,
     35                        "courseoverground" => 1,
    3536                        "waterdepth" => 1,
    3637                        "watertemperature" => 1,
    … …  
    4849                $result["track"] = array();
    4950
    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                 }
     51                if(!file_exists($dbFile))
     52                        return $this->error("Database file ". $dbFile ." does not exist");
     53
    5754                // Fetch entries of the last 30s with maximum (available) resolution of 2s
    5855                $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
    59                 if($lastValid){
    60                         $result["lastvalid"] = $lastValid;
     56                if($lastValid !== false){
     57                        $result["lastvalid"] = $lastValid->toArray();
    6158                }
    6259                // Fetch entries of the last 1h with maximum (available) resolution of 10s
    … …  
    6562                $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
    6663
    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                 }
     64                return $this->output($result);
    7465        }
    7566
    … …  
    8273                $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    8374                // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
    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                 }
     75                if($data === false)
     76                        return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result);
    9377
    9478                if($debug > 2){
    95                         echo "<pre>";
    9679                        var_dump($data);
    97                         echo "</pre>";
    9880                }
    9981
    … …  
    10284                // transform them into json format
    10385                $i = 0;
    104                 $lastValid = -1;
     86                $lastValid = false;
    10587                $keys = array_keys($data);
    10688                $keyTimeframes = $keys[0];
    10789                $itemCount = count($keys);
    108                 if($itemCount < 0 ){
    109                         error_log("No keys in fetched rrd data from ". $dbFile);
    110                         echo json_encode("");
    111                         exit;
    112                 }
     90                if($itemCount < 0 )
     91                        return $this->error("No keys in fetched rrd data from ". $dbFile);
     92
    11393                $timeframesCount = count($data[$keyTimeframes]);
    114                 if($debug > 0){
    115                         echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
     94                if($debug > 1){
     95                        error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
    11696                }
    11797                foreach($data[$keys[0]] as $timeframe => $value){
    118                         $allValid = true;
    119                         $gpsFix = false;
    120                         $tmp = array();
     98                        $tmp = new TrackItem($timeframe);
    12199                        foreach($keys as $key){
    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                                 }
     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);
    147105                        }
    148106                        // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    149                         if($allValid){
     107                        if($tmp->isValid()){
    150108                                $lastValid = $tmp;
    151                                 if($gpsFix){
     109                                if($tmp->gpsFixed()){
    152110                                        if(!array_key_exists($timeframe, $result["track"])){
    153                                                 $result["track"][$timeframe] = $tmp;
     111                                                $result["track"][$timeframe] = $tmp->toArray();
    154112                                        }
    155113                                }
Note: See TracChangeset for help on using the changeset viewer.