Ignore:
File:
1 edited

Legend:

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

    r33c6557 racea570  
    11<?php
    22/*
    3     Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     3    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    44
    55    This program is free software: you can redistribute it and/or modify
    … …  
    1616    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1717*/
     18
     19xdebug_start_trace();
    1820
    1921require_once(__DIR__ ."/inc/Config.php");
    … …  
    3638);
    3739
    38 function addRrdData($interval, $period, &$result, &$lastValid){
     40function addRrdData($interval, $period, &$result, &$lastTime){
    3941        global $dbFile;
    4042        global $debug;
    4143        global $configRound;
    42         $lastValid = false;
    4344
    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"));
     45        $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
    4846        if($data === false){
    4947                echo json_encode($result);
    … …  
    5351                else
    5452                        error_log($message);
    55                 return;
    5653        }
    5754
    … …  
    6865        $lastTime = -1;
    6966        $keys = array_keys($data);
    70         if(count($keys) < 0 ){
     67        $keyTimeframes = $keys[0];
     68        $itemCount = count($keys);
     69        if($itemCount < 0 ){
    7170                error_log("No keys in fetched rrd data from ". $dbFile);
    7271                echo json_encode("");
    7372                exit;
    7473        }
     74        $timeframesCount = count($data[$keyTimeframes]);
    7575        if($debug > 0){
    76                 echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
     76                echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
    7777        }
    78         foreach($data[$keys[0]] as $timeframe => $value){
     78        $keysTimeframes = array_keys($data[$keyTimeframes]);
     79        for($i = 0; $i < $timeframesCount; $i++){
     80                $time = $keysTimeframes[$i];
    7981                $allValid = true;
    80                 $gpsFix = false;
    8182                $tmp = array();
    8283                foreach($keys as $key){
    8384                        if($key == "gpsfix"){
    84                                 switch($data[$key][$timeframe]){
     85                                switch($data[$key][$time]){
     86                                        case 1:
     87                                                $tmp[$key] = "2D";
     88                                                break;
    8589                                        case 2:
    86                                                 $tmp[$key] = "2D";
    87                                                 $gpsFix = true;
    88                                                 break;
    89                                         case 3:
    9090                                                $tmp[$key] = "3D";
    91                                                 $gpsFix = true;
    9291                                                break;
    9392                                        default:
    … …  
    9695                        }
    9796                        else{
    98                                 $tmp[$key] = $data[$key][$timeframe];
     97                                $tmp[$key] = $data[$key][$time];
    9998                                if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    10099                                        $allValid = false;
    … …  
    109108                // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    110109                if($allValid){
    111                         $lastValid = $tmp;
    112                         if($gpsFix){
    113                                 if(!array_key_exists($timeframe, $result["track"])){
    114                                         $result["track"][$timeframe] = $tmp;
    115                                 }
    116                         }
     110                        if(!array_key_exists($time, $result["track"])){
     111                                $result["track"][$time] = $tmp;
     112                        }
     113                        $lastTime = $time;
    117114                }
    118115                else{
    … …  
    124121                }
    125122        }
     123
    126124        ksort($result["track"]);
    127125}
    … …  
    167165                exit;
    168166        }
    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;
     167        // Fetch entries of the last 30 seconds
     168        addRrdData("5s", "30s", $result, $lastTime);
     169        if($lastTime > -1){
     170                $result["lastvalid"] = $result["track"][$lastTime];
    173171        }
    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);
     172        addRrdData("15s", "5minutes", $result, $lastTime);
     173        addRrdData("30s", "12h", $result, $lastTime);
    178174}
    179175
    180176if($debug > 0){
    181         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
     177        echo json_encode($result,  JSON_PRETTY_PRINT);
    182178}
    183179else{
    184         echo json_encode($result) ."\n";
     180        echo json_encode($result);
    185181}
    186182
     183xdebug_stop_trace();
     184
    187185?>
Note: See TracChangeset for help on using the changeset viewer.