- File:
-
- 1 edited
-
src/webapp/webservice/inc/GnuBoat/ServiceData.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webapp/webservice/inc/GnuBoat/ServiceData.php
r0662b2d r1ba85cf 29 29 $this->_configRound = array 30 30 ( 31 "latitude" => 4,32 "longitude" => 4,31 "latitude" => 5, 32 "longitude" => 5, 33 33 "speedthroughwater" => 1, 34 34 "speedoverground" => 1, 35 "courseoverground" => 1,36 35 "waterdepth" => 1, 37 36 "watertemperature" => 1, … … 49 48 $result["track"] = array(); 50 49 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 } 54 57 // Fetch entries of the last 30s with maximum (available) resolution of 2s 55 58 $this->addRrdData($dbFile, "2", "240", $result, $lastValid); 56 if($lastValid !== false){57 $result["lastvalid"] = $lastValid ->toArray();59 if($lastValid){ 60 $result["lastvalid"] = $lastValid; 58 61 } 59 62 // Fetch entries of the last 1h with maximum (available) resolution of 10s … … 62 65 $this->addRrdData($dbFile, "60", "86400", $result, $lastTime); 63 66 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 } 65 74 } 66 75 … … 73 82 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime)); 74 83 // $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 } 77 93 78 94 if($debug > 2){ 95 echo "<pre>"; 79 96 var_dump($data); 97 echo "</pre>"; 80 98 } 81 99 … … 84 102 // transform them into json format 85 103 $i = 0; 86 $lastValid = false;104 $lastValid = -1; 87 105 $keys = array_keys($data); 88 106 $keyTimeframes = $keys[0]; 89 107 $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 } 93 113 $timeframesCount = count($data[$keyTimeframes]); 94 if($debug > 1){95 e rror_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);114 if($debug > 0){ 115 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n"; 96 116 } 97 117 foreach($data[$keys[0]] as $timeframe => $value){ 98 $tmp = new TrackItem($timeframe); 118 $allValid = true; 119 $gpsFix = false; 120 $tmp = array(); 99 121 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 } 105 147 } 106 148 // 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){ 108 150 $lastValid = $tmp; 109 if($ tmp->gpsFixed()){151 if($gpsFix){ 110 152 if(!array_key_exists($timeframe, $result["track"])){ 111 $result["track"][$timeframe] = $tmp ->toArray();153 $result["track"][$timeframe] = $tmp; 112 154 } 113 155 }
Note:
See TracChangeset
for help on using the changeset viewer.
