Changeset 5519ced for src/webapp/webservice
- Timestamp:
- Jul 20, 2016, 5:18:34 PM (10 years ago)
- Branches:
- 0.5, app-cordova, debian/bullseye, debian/jessie, master
- Children:
- d7ee6ae
- Parents:
- a250eb4
- File:
-
- 1 edited
-
src/webapp/webservice/index.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webapp/webservice/index.php
ra250eb4 r5519ced 21 21 $dbFile = $config->databaseFile(); 22 22 23 define("REQUEST_DATA", "data"); 24 define("REQUEST_SHUTDOWN", "shutdown"); 25 26 23 27 // configure roundings per value (key) and precision (value) 24 28 $configRound = array … … 36 40 $debug = $_GET["debug"]; 37 41 42 $request = REQUEST_DATA; 43 if(array_key_exists("request", $_GET)){ 44 $request = $_GET["request"]; 45 } 46 38 47 if($debug == 0) 39 48 header('Content-Type: application/json'); 40 49 41 $result["lastvalid"] = array(); 42 $result["track"] = array(); 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(); 43 73 44 // Fetch entries of the last 12hours 45 $data = rrd_fetch($dbFile, array( "AVERAGE", "--start", "-12h")); 46 if($data === false){ 47 echo json_encode($result); 48 $message = "Could not open rrd database ". $dbFile; 49 if($debug > 0) 50 echo $message; 51 else 52 error_log($message); 53 exit; 54 } 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 } 55 92 56 if($debug > 2){57 echo "<pre>";58 var_dump($data);59 echo "</pre>";60 }93 if($debug > 2){ 94 echo "<pre>"; 95 var_dump($data); 96 echo "</pre>"; 97 } 61 98 62 $data = $data["data"];99 $data = $data["data"]; 63 100 64 // transform them into json format 65 $i = 0; 66 $lastTime = -1; 67 $keys = array_keys($data); 68 if(count($keys) < 0 ){ 69 error_log("No keys in fetched rrd data from ". $dbFile); 70 echo json_encode(""); 71 exit; 72 } 73 for($i = 0; $i < count($data[$keys[0]]); $i++){ 74 $time = array_keys($data[$keys[0]])[$i]; 75 $allValid = true; 76 $tmp = array(); 77 foreach($keys as $key){ 78 if($key == "gpsfix"){ 79 switch($data[$key][$time]){ 80 case 1: 81 $tmp[$key] = "2D"; 82 break; 83 case 2: 84 $tmp[$key] = "3D"; 85 break; 86 default: 87 $tmp[$key] = "-"; 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]); 88 134 } 89 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 } 90 141 else{ 91 $tmp[$key] = $data[$key][$time]; 92 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){ 93 $allValid = false; 142 if($debug > 0){ 143 echo "Failed <pre>"; 144 var_dump($tmp); 145 echo "</pre>\n"; 94 146 } 95 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))96 $tmp[$key] = round($tmp[$key], $configRound[$key]);97 147 } 98 148 } 99 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time 100 if($allValid && $tmp["gpsfix"] != "-"){ 101 $result["track"][$time] = $tmp; 102 $lastTime = $time; 149 if($lastTime > -1){ 150 $result["lastvalid"] = $result["track"][$lastTime]; 103 151 } 104 else{105 if($debug > 0){106 echo "Failed <pre>";107 var_dump($tmp);108 echo "</pre>\n";109 }110 }111 }112 if($lastTime > -1){113 $result["lastvalid"] = $result["track"][$lastTime];114 152 } 115 153
Note:
See TracChangeset
for help on using the changeset viewer.
