Ignore:
Timestamp:
Aug 10, 2017, 6:05:33 PM (9 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
6fec060
Parents:
82b4b49
Message:
  • Webservice now leverages PHP classes and autoloader functionality. All webservice tasks are combined in index.php and served by Webservice.php
  • Webclient lists available log files and webservice provides them to download
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webservice/index.php

    r82b4b49 rc455d7b  
    1717*/
    1818
    19 require_once(__DIR__ ."/inc/Config.php");
    20 $config = new Config();
    21 $dbFile = $config->databaseFile();
     19require_once(__DIR__ ."/inc/autoloader.php");
    2220
    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         $lastValid = -1;
    69         $keys = array_keys($data);
    70         $keyTimeframes = $keys[0];
    71         $itemCount = count($keys);
    72         if($itemCount < 0 ){
    73                 error_log("No keys in fetched rrd data from ". $dbFile);
    74                 echo json_encode("");
    75                 exit;
    76         }
    77         $timeframesCount = count($data[$keyTimeframes]);
    78         if($debug > 0){
    79                 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
    80         }
    81         foreach($data[$keys[0]] as $timeframe => $value){
    82                 $allValid = true;
    83                 $gpsFix = false;
    84                 $tmp = array();
    85                 foreach($keys as $key){
    86                         if($key == "gpsfix"){
    87                                 switch($data[$key][$timeframe]){
    88                                         case 2:
    89                                                 $tmp[$key] = "2D";
    90                                                 $gpsFix = true;
    91                                                 break;
    92                                         case 3:
    93                                                 $tmp[$key] = "3D";
    94                                                 $gpsFix = true;
    95                                                 break;
    96                                         default:
    97                                                 $tmp[$key] = "-";
    98                                 }
    99                         }
    100                         else{
    101                                 $tmp[$key] = $data[$key][$timeframe];
    102                                 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    103                                         $allValid = false;
    104                                         if($debug > 1){
    105                                                 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
    106                                         }
    107                                 }
    108                                 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
    109                                         $tmp[$key] = round($tmp[$key], $configRound[$key]);
    110                         }
    111                 }
    112                 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    113                 if($allValid){
    114                         $lastValid = $tmp;
    115                         if($gpsFix){
    116                                 if(!array_key_exists($timeframe, $result["track"])){
    117                                         $result["track"][$timeframe] = $tmp;
    118                                 }
    119                         }
    120                 }
    121                 else{
    122                         if($debug > 1){
    123                                 echo "Failed <pre>";
    124                                 var_dump($tmp);
    125                                 echo "</pre>\n";
    126                         }
    127                 }
    128         }
    129         ksort($result["track"]);
    130 }
    131 
    132 $debug = 0;
    133 if(array_key_exists("debug", $_GET))
    134         $debug = $_GET["debug"];
    135 
    136 $request = REQUEST_DATA;
    137 if(array_key_exists("request", $_GET)){
    138         $request = $_GET["request"];
    139 }
    140 
    141 if($debug == 0)
    142         header('Content-Type: application/json');
    143 
    144 $result = array();
    145 if($request == REQUEST_SHUTDOWN){
    146         $handle = @fopen($config->shutdownRequestFile(), "w");
    147         if($handle === false){
    148                 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
    149                 if($debug > 0)
    150                         echo $message;
    151                 error_log($message);
    152                 $result["shutdownstart"] = -1;
    153         }
    154         else{
    155                 $shutdownTime = new DateTime('NOW');
    156                 $shutdownTime->modify("+15 seconds");
    157                 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
    158                 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
    159         }
    160 }
    161 elseif($request == REQUEST_DATA){
    162         $result["lastvalid"] = array();
    163         $result["track"] = array();
    164 
    165         if(!file_exists($dbFile)){
    166                 echo json_encode("");
    167                 if($debug > 0)
    168                         echo "Database file ". $dbFile ." does not exist!";
    169                 error_log("Database file ". $dbFile ." does not exist");
    170                 exit;
    171         }
    172         // Fetch entries of the last 30s with maximum (available) resolution of 2s
    173         addRrdData("2", "240", $result, $lastValid);
    174         if($lastValid){
    175                 $result["lastvalid"] = $lastValid;
    176         }
    177         // Fetch entries of the last 1h with maximum (available) resolution of 10s
    178         addRrdData("10", "36000", $result, $lastTime);
    179         // Fetch entries of the last 24h with maximum (available) resolution of 60s
    180         addRrdData("60", "86400", $result, $lastTime);
    181 }
    182 
    183 if($debug > 0){
    184         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    185 }
    186 else{
    187         echo json_encode($result) ."\n";
    188 }
     21$webservice = new GnuBoat\Webservice();
     22$webservice->process();
    18923
    19024?>
Note: See TracChangeset for help on using the changeset viewer.