Ignore:
Timestamp:
Aug 30, 2017, 2:45:37 PM (9 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, debian/bullseye, debian/jessie, master
Children:
a50c18f
Parents:
89c510f
Message:

Improved webservice class to use TrackItem in ServiceData.php and added some tests

File:
1 edited

Legend:

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

    r89c510f 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                         if($debug > 0)
    52                                 echo "Database file ". $dbFile ." does not exist!";
    53                         error_log("Database file ". $dbFile ." does not exist");
    54                         return;
    55                 }
     51                if(!file_exists($dbFile))
     52                        return $this->error("Database file ". $dbFile ." does not exist");
     53
    5654                // Fetch entries of the last 30s with maximum (available) resolution of 2s
    5755                $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
    58                 if($lastValid){
    59                         $result["lastvalid"] = $lastValid;
     56                if($lastValid !== false){
     57                        $result["lastvalid"] = $lastValid->toArray();
    6058                }
    6159                // Fetch entries of the last 1h with maximum (available) resolution of 10s
    … …  
    6462                $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
    6563
    66                 if($debug > 0){
    67                         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    68                 }
    69                 else{
    70                         header('Content-Type: application/json');
    71                         echo json_encode($result) ."\n";
    72                 }
     64                return $this->output($result);
    7365        }
    7466
    … …  
    8173                $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    8274                // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
    83                 if($data === false){
    84                         echo json_encode($result);
    85                         $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
    86                         if($debug > 0)
    87                                 echo $message;
    88                         else
    89                                 error_log($message);
    90                         return;
    91                 }
     75                if($data === false)
     76                        return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result);
    9277
    9378                if($debug > 2){
    94                         echo "<pre>";
    9579                        var_dump($data);
    96                         echo "</pre>";
    9780                }
    9881
    … …  
    10184                // transform them into json format
    10285                $i = 0;
    103                 $lastValid = -1;
     86                $lastValid = false;
    10487                $keys = array_keys($data);
    10588                $keyTimeframes = $keys[0];
    10689                $itemCount = count($keys);
    107                 if($itemCount < 0 ){
    108                         error_log("No keys in fetched rrd data from ". $dbFile);
    109                         echo json_encode("");
    110                         exit;
    111                 }
     90                if($itemCount < 0 )
     91                        return $this->error("No keys in fetched rrd data from ". $dbFile);
     92
    11293                $timeframesCount = count($data[$keyTimeframes]);
    113                 if($debug > 0){
    114                         echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
     94                if($debug > 1){
     95                        error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
    11596                }
    11697                foreach($data[$keys[0]] as $timeframe => $value){
    117                         $allValid = true;
    118                         $gpsFix = false;
    119                         $tmp = array();
     98                        $tmp = new TrackItem($timeframe);
    12099                        foreach($keys as $key){
    121                                 if($key == "gpsfix"){
    122                                         switch($data[$key][$timeframe]){
    123                                                 case 2:
    124                                                         $tmp[$key] = "2D";
    125                                                         $gpsFix = true;
    126                                                         break;
    127                                                 case 3:
    128                                                         $tmp[$key] = "3D";
    129                                                         $gpsFix = true;
    130                                                         break;
    131                                                 default:
    132                                                         $tmp[$key] = "-";
    133                                         }
    134                                 }
    135                                 else{
    136                                         $tmp[$key] = $data[$key][$timeframe];
    137                                         if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    138                                                 $allValid = false;
    139                                                 if($debug > 1){
    140                                                         echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
    141                                                 }
    142                                         }
    143                                         elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
    144                                                 $tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
    145                                 }
     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);
    146105                        }
    147106                        // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    148                         if($allValid){
     107                        if($tmp->isValid()){
    149108                                $lastValid = $tmp;
    150                                 if($gpsFix){
     109                                if($tmp->gpsFixed()){
    151110                                        if(!array_key_exists($timeframe, $result["track"])){
    152                                                 $result["track"][$timeframe] = $tmp;
     111                                                $result["track"][$timeframe] = $tmp->toArray();
    153112                                        }
    154113                                }
Note: See TracChangeset for help on using the changeset viewer.