Changeset 73c34f3


Ignore:
Timestamp:
Jul 20, 2016, 5:57:42 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
0c7eb83, 10edeb0
Parents:
d7ee6ae
Message:

Data is provided in resolutions from 5s to 30s, increasing with distance to now

Signed-off-by: Joachim Langenbach <joachim.langenbach@…>

File:
1 edited

Legend:

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

    rd7ee6ae r73c34f3  
    3636);
    3737
    38 $debug = 0;
    39 if(array_key_exists("debug", $_GET))
    40         $debug = $_GET["debug"];
     38function addRrdData($interval, $period, &$result, &$lastTime){
     39        global $dbFile;
     40        global $debug;
     41        global $configRound;
    4142
    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"));
     43        $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
    8344        if($data === false){
    8445                echo json_encode($result);
    85                 $message = "Could not open rrd database ". $dbFile;
     46                $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
    8647                if($debug > 0)
    8748                        echo $message;
    8849                else
    8950                        error_log($message);
    90                 exit;
    9151        }
    9252
    … …  
    10767                echo json_encode("");
    10868                exit;
     69        }
     70        if($debug > 0){
     71                echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
    10972        }
    11073        for($i = 0; $i < count($data[$keys[0]]); $i++){
    … …  
    13699                // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    137100                if($allValid && $tmp["gpsfix"] != "-"){
    138                         $result["track"][$time] = $tmp;
     101                        echo "want to add ". $time ."<br/>\n";
     102                        if(!array_key_exists($time, $result["track"])){
     103                                $result["track"][$time] = $tmp;
     104                        }
    139105                        $lastTime = $time;
    140106                }
    141107                else{
    142                         if($debug > 0){
     108                        if($debug > 1){
    143109                                echo "Failed <pre>";
    144110                                var_dump($tmp);
    … …  
    147113                }
    148114        }
     115       
     116        ksort($result["track"]);
     117}
     118
     119$debug = 0;
     120if(array_key_exists("debug", $_GET))
     121        $debug = $_GET["debug"];
     122
     123$request = REQUEST_DATA;
     124if(array_key_exists("request", $_GET)){
     125        $request = $_GET["request"];
     126}
     127
     128if($debug == 0)
     129        header('Content-Type: application/json');
     130
     131$result = array();
     132if($request == REQUEST_SHUTDOWN){
     133        $handle = @fopen("/var/spool/gnuboat/gnuboat_shutdown", "w");
     134        if($handle === false){
     135                $message = "Could not create file to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
     136                if($debug > 0)
     137                        echo $message;
     138                error_log($message);
     139                $result["shutdownstart"] = -1;
     140        }
     141        else{
     142                $shutdownTime = new DateTime('NOW');
     143                $shutdownTime>modify("+15 seconds");
     144                fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
     145                $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
     146        }
     147}
     148elseif($request == REQUEST_DATA){
     149        $result["lastvalid"] = array();
     150        $result["track"] = array();
     151
     152        if(!file_exists($dbFile)){
     153                echo json_encode("");
     154                if($debug > 0)
     155                        echo "Database file ". $dbFile ." does not exist!";
     156                error_log("Database file ". $dbFile ." does not exist");
     157                exit;
     158        }
     159        // Fetch entries of the last 30 seconds
     160        addRrdData("5s", "30s", $result, $lastTime);
    149161        if($lastTime > -1){
    150162                $result["lastvalid"] = $result["track"][$lastTime];
    151163        }
     164        addRrdData("15s", "5minutes", $result, $lastTime);
     165        addRrdData("30s", "12h", $result, $lastTime);
    152166}
    153167
Note: See TracChangeset for help on using the changeset viewer.