| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2016 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 | $debug = 0;
|
|---|
| 39 | if(array_key_exists("debug", $_GET))
|
|---|
| 40 | $debug = $_GET["debug"];
|
|---|
| 41 |
|
|---|
| 42 | $request = REQUEST_DATA;
|
|---|
| 43 | if(array_key_exists("request", $_GET)){
|
|---|
| 44 | $request = $_GET["request"];
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | if($debug == 0)
|
|---|
| 48 | header('Content-Type: application/json');
|
|---|
| 49 |
|
|---|
| 50 | $result = array();
|
|---|
| 51 | if($request == REQUEST_SHUTDOWN){
|
|---|
| 52 | $handle = @fopen("/var/spool/gnuboat/gnuboat_shutdown", "w");
|
|---|
| 53 | if($handle === false){
|
|---|
| 54 | $message = "Could not create file to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
|
|---|
| 55 | if($debug > 0)
|
|---|
| 56 | echo $message;
|
|---|
| 57 | error_log($message);
|
|---|
| 58 | $result["shutdownstart"] = -1;
|
|---|
| 59 | $shutdownTime = new DateTime('NOW');
|
|---|
| 60 | $shutdownTime->modify("+60 seconds");
|
|---|
| 61 | $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
|
|---|
| 62 | }
|
|---|
| 63 | else{
|
|---|
| 64 | $shutdownTime = new DateTime('NOW');
|
|---|
| 65 | $shutdownTime>modify("+15 seconds");
|
|---|
| 66 | fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
|
|---|
| 67 | $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | elseif($request == REQUEST_DATA){
|
|---|
| 71 | $result["lastvalid"] = array();
|
|---|
| 72 | $result["track"] = array();
|
|---|
| 73 |
|
|---|
| 74 | if(!file_exists($dbFile)){
|
|---|
| 75 | echo json_encode("");
|
|---|
| 76 | if($debug > 0)
|
|---|
| 77 | echo "Database file ". $dbFile ." does not exist!";
|
|---|
| 78 | error_log("Database file ". $dbFile ." does not exist");
|
|---|
| 79 | exit;
|
|---|
| 80 | }
|
|---|
| 81 | // Fetch entries of the last 12hours
|
|---|
| 82 | $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", "5", "--start", "-12h"));
|
|---|
| 83 | if($data === false){
|
|---|
| 84 | echo json_encode($result);
|
|---|
| 85 | $message = "Could not open rrd database ". $dbFile;
|
|---|
| 86 | if($debug > 0)
|
|---|
| 87 | echo $message;
|
|---|
| 88 | else
|
|---|
| 89 | error_log($message);
|
|---|
| 90 | exit;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | if($debug > 2){
|
|---|
| 94 | echo "<pre>";
|
|---|
| 95 | var_dump($data);
|
|---|
| 96 | echo "</pre>";
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | $data = $data["data"];
|
|---|
| 100 |
|
|---|
| 101 | // transform them into json format
|
|---|
| 102 | $i = 0;
|
|---|
| 103 | $lastTime = -1;
|
|---|
| 104 | $keys = array_keys($data);
|
|---|
| 105 | if(count($keys) < 0 ){
|
|---|
| 106 | error_log("No keys in fetched rrd data from ". $dbFile);
|
|---|
| 107 | echo json_encode("");
|
|---|
| 108 | exit;
|
|---|
| 109 | }
|
|---|
| 110 | for($i = 0; $i < count($data[$keys[0]]); $i++){
|
|---|
| 111 | $time = array_keys($data[$keys[0]])[$i];
|
|---|
| 112 | $allValid = true;
|
|---|
| 113 | $tmp = array();
|
|---|
| 114 | foreach($keys as $key){
|
|---|
| 115 | if($key == "gpsfix"){
|
|---|
| 116 | switch($data[$key][$time]){
|
|---|
| 117 | case 1:
|
|---|
| 118 | $tmp[$key] = "2D";
|
|---|
| 119 | break;
|
|---|
| 120 | case 2:
|
|---|
| 121 | $tmp[$key] = "3D";
|
|---|
| 122 | break;
|
|---|
| 123 | default:
|
|---|
| 124 | $tmp[$key] = "-";
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | else{
|
|---|
| 128 | $tmp[$key] = $data[$key][$time];
|
|---|
| 129 | if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
|
|---|
| 130 | $allValid = false;
|
|---|
| 131 | }
|
|---|
| 132 | elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
|
|---|
| 133 | $tmp[$key] = round($tmp[$key], $configRound[$key]);
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
|
|---|
| 137 | if($allValid && $tmp["gpsfix"] != "-"){
|
|---|
| 138 | $result["track"][$time] = $tmp;
|
|---|
| 139 | $lastTime = $time;
|
|---|
| 140 | }
|
|---|
| 141 | else{
|
|---|
| 142 | if($debug > 0){
|
|---|
| 143 | echo "Failed <pre>";
|
|---|
| 144 | var_dump($tmp);
|
|---|
| 145 | echo "</pre>\n";
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | if($lastTime > -1){
|
|---|
| 150 | $result["lastvalid"] = $result["track"][$lastTime];
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if($debug > 0){
|
|---|
| 155 | echo json_encode($result, JSON_PRETTY_PRINT);
|
|---|
| 156 | }
|
|---|
| 157 | else{
|
|---|
| 158 | echo json_encode($result);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | ?>
|
|---|