. */ namespace GnuBoat; require_once(__DIR__ ."/../autoloader.php"); class ServiceData extends Service { function __construct() { parent::__construct(); // configure roundings per value (key) and precision (value) $this->_configRound = array ( "latitude" => 5, "longitude" => 5, "speedthroughwater" => 1, "speedoverground" => 1, "waterdepth" => 1, "watertemperature" => 1, ); } protected function process() { $debug = $this->debug(); $config = new Config(); $dbFile = $config->databaseFile(); $result = array(); $result["lastvalid"] = array(); $result["track"] = array(); if(!file_exists($dbFile)){ echo json_encode(""); if($debug > 0) echo "Database file ". $dbFile ." does not exist!"; error_log("Database file ". $dbFile ." does not exist"); exit; } // Fetch entries of the last 30s with maximum (available) resolution of 2s $this->addRrdData($dbFile, "2", "240", $result, $lastValid); if($lastValid){ $result["lastvalid"] = $lastValid; } // Fetch entries of the last 1h with maximum (available) resolution of 10s $this->addRrdData($dbFile, "10", "36000", $result, $lastTime); // Fetch entries of the last 24h with maximum (available) resolution of 60s $this->addRrdData($dbFile, "60", "86400", $result, $lastTime); if($debug > 0){ echo json_encode($result, JSON_PRETTY_PRINT) ."\n"; } else{ header('Content-Type: application/json'); echo json_encode($result) ."\n"; } } private function addRrdData($dbFile, $interval, $period, &$result, &$lastValid){ $debug = $this->debug(); $lastValid = false; $endTime = floor(time() / $interval) * $interval; $startTime = $endTime - $period; $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime)); // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start")); if($data === false){ echo json_encode($result); $message = "Failure reading rrd database ". $dbFile .": ". rrd_error(); if($debug > 0) echo $message; else error_log($message); return; } if($debug > 2){ echo "
"; var_dump($data); echo ""; } $data = $data["data"]; // transform them into json format $i = 0; $lastValid = -1; $keys = array_keys($data); $keyTimeframes = $keys[0]; $itemCount = count($keys); if($itemCount < 0 ){ error_log("No keys in fetched rrd data from ". $dbFile); echo json_encode(""); exit; } $timeframesCount = count($data[$keyTimeframes]); if($debug > 0){ echo "
Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."
\n"; } foreach($data[$keys[0]] as $timeframe => $value){ $allValid = true; $gpsFix = false; $tmp = array(); foreach($keys as $key){ if($key == "gpsfix"){ switch($data[$key][$timeframe]){ case 2: $tmp[$key] = "2D"; $gpsFix = true; break; case 3: $tmp[$key] = "3D"; $gpsFix = true; break; default: $tmp[$key] = "-"; } } else{ $tmp[$key] = $data[$key][$timeframe]; if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){ $allValid = false; if($debug > 1){ echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe .""; var_dump($tmp); echo "\n"; } } } ksort($result["track"]); } } ?>