| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software: you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | require_once(__DIR__ ."/inc/Config.php");
|
|---|
| 20 | $config = new Config();
|
|---|
| 21 | $dbFile = $config->databaseFile();
|
|---|
| 22 |
|
|---|
| 23 | define("REQUEST_DATA", "data");
|
|---|
| 24 | define("REQUEST_SHUTDOWN", "shutdown");
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | // configure roundings per value (key) and precision (value)
|
|---|
| 28 | $configRound = array
|
|---|
| 29 | (
|
|---|
| 30 | "latitude" => 5,
|
|---|
| 31 | "longitude" => 5,
|
|---|
| 32 | "speedthroughwater" => 1,
|
|---|
| 33 | "speedoverground" => 1,
|
|---|
| 34 | "waterdepth" => 1,
|
|---|
| 35 | "watertemperature" => 1,
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | function addRrdData($interval, $period, &$result, &$lastValid){
|
|---|
| 39 | global $dbFile;
|
|---|
| 40 | global $debug;
|
|---|
| 41 | global $configRound;
|
|---|
| 42 | $lastValid = false;
|
|---|
| 43 |
|
|---|
| 44 | $endTime = floor(time() / $interval) * $interval;
|
|---|
| 45 | $startTime = $endTime - $period;
|
|---|
| 46 | $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
|
|---|
| 47 | // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
|
|---|
| 48 | if($data === false){
|
|---|
| 49 | echo json_encode($result);
|
|---|
| 50 | $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
|
|---|
| 51 | if($debug > 0)
|
|---|
| 52 | echo $message;
|
|---|
| 53 | else
|
|---|
| 54 | error_log($message);
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if($debug > 2){
|
|---|
| 59 | echo "<pre>";
|
|---|
| 60 | var_dump($data);
|
|---|
| 61 | echo "</pre>";
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | $data = $data["data"];
|
|---|
| 65 |
|
|---|
| 66 | // transform them into json format
|
|---|
| 67 | $i = 0;
|
|---|
| 68 | $lastTime = -1;
|
|---|
| 69 | $keys = array_keys($data);
|
|---|
| 70 | if(count($keys) < 0 ){
|
|---|
| 71 | error_log("No keys in fetched rrd data from ". $dbFile);
|
|---|
| 72 | echo json_encode("");
|
|---|
| 73 | exit;
|
|---|
| 74 | }
|
|---|
| 75 | if($debug > 0){
|
|---|
| 76 | echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
|
|---|
| 77 | }
|
|---|
| 78 | foreach($data[$keys[0]] as $timeframe => $value){
|
|---|
| 79 | $allValid = true;
|
|---|
| 80 | $gpsFix = false;
|
|---|
| 81 | $tmp = array();
|
|---|
| 82 | foreach($keys as $key){
|
|---|
| 83 | if($key == "gpsfix"){
|
|---|
| 84 | switch($data[$key][$timeframe]){
|
|---|
| 85 | case 2:
|
|---|
| 86 | $tmp[$key] = "2D";
|
|---|
| 87 | $gpsFix = true;
|
|---|
| 88 | break;
|
|---|
| 89 | case 3:
|
|---|
| 90 | $tmp[$key] = "3D";
|
|---|
| 91 | $gpsFix = true;
|
|---|
| 92 | break;
|
|---|
| 93 | default:
|
|---|
| 94 | $tmp[$key] = "-";
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | else{
|
|---|
| 98 | $tmp[$key] = $data[$key][$timeframe];
|
|---|
| 99 | if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
|
|---|
| 100 | $allValid = false;
|
|---|
| 101 | if($debug > 1){
|
|---|
| 102 | echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
|
|---|
| 106 | $tmp[$key] = round($tmp[$key], $configRound[$key]);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
|
|---|
| 110 | if($allValid){
|
|---|
| 111 | $lastValid = $tmp;
|
|---|
| 112 | if($gpsFix){
|
|---|
| 113 | if(!array_key_exists($timeframe, $result["track"])){
|
|---|
| 114 | $result["track"][$timeframe] = $tmp;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | else{
|
|---|
| 119 | if($debug > 1){
|
|---|
| 120 | echo "Failed <pre>";
|
|---|
| 121 | var_dump($tmp);
|
|---|
| 122 | echo "</pre>\n";
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | ksort($result["track"]);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | $debug = 0;
|
|---|
| 130 | if(array_key_exists("debug", $_GET))
|
|---|
| 131 | $debug = $_GET["debug"];
|
|---|
| 132 |
|
|---|
| 133 | $request = REQUEST_DATA;
|
|---|
| 134 | if(array_key_exists("request", $_GET)){
|
|---|
| 135 | $request = $_GET["request"];
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if($debug == 0)
|
|---|
| 139 | header('Content-Type: application/json');
|
|---|
| 140 |
|
|---|
| 141 | $result = array();
|
|---|
| 142 | if($request == REQUEST_SHUTDOWN){
|
|---|
| 143 | $handle = @fopen($config->shutdownRequestFile(), "w");
|
|---|
| 144 | if($handle === false){
|
|---|
| 145 | $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
|
|---|
| 146 | if($debug > 0)
|
|---|
| 147 | echo $message;
|
|---|
| 148 | error_log($message);
|
|---|
| 149 | $result["shutdownstart"] = -1;
|
|---|
| 150 | }
|
|---|
| 151 | else{
|
|---|
| 152 | $shutdownTime = new DateTime('NOW');
|
|---|
| 153 | $shutdownTime->modify("+15 seconds");
|
|---|
| 154 | fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
|
|---|
| 155 | $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | elseif($request == REQUEST_DATA){
|
|---|
| 159 | $result["lastvalid"] = array();
|
|---|
| 160 | $result["track"] = array();
|
|---|
| 161 |
|
|---|
| 162 | if(!file_exists($dbFile)){
|
|---|
| 163 | echo json_encode("");
|
|---|
| 164 | if($debug > 0)
|
|---|
| 165 | echo "Database file ". $dbFile ." does not exist!";
|
|---|
| 166 | error_log("Database file ". $dbFile ." does not exist");
|
|---|
| 167 | exit;
|
|---|
| 168 | }
|
|---|
| 169 | // Fetch entries of the last 30s with maximum (available) resolution of 2s
|
|---|
| 170 | addRrdData("2", "240", $result, $lastValid);
|
|---|
| 171 | if($lastValid){
|
|---|
| 172 | $result["lastvalid"] = $lastValid;
|
|---|
| 173 | }
|
|---|
| 174 | // Fetch entries of the last 1h with maximum (available) resolution of 10s
|
|---|
| 175 | addRrdData("10", "36000", $result, $lastTime);
|
|---|
| 176 | // Fetch entries of the last 24h with maximum (available) resolution of 60s
|
|---|
| 177 | addRrdData("60", "86400", $result, $lastTime);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if($debug > 0){
|
|---|
| 181 | echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
|
|---|
| 182 | }
|
|---|
| 183 | else{
|
|---|
| 184 | echo json_encode($result) ."\n";
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | ?>
|
|---|