Ignore:
File:
1 edited

Legend:

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

    racea570 r33c6557  
    11<?php
    22/*
    3     Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     3    Copyright (C) 2016 - 2017 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 
    19 xdebug_start_trace();
    2018
    2119require_once(__DIR__ ."/inc/Config.php");
    … …  
    3836);
    3937
    40 function addRrdData($interval, $period, &$result, &$lastTime){
     38function addRrdData($interval, $period, &$result, &$lastValid){
    4139        global $dbFile;
    4240        global $debug;
    4341        global $configRound;
     42        $lastValid = false;
    4443
    45         $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
     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"));
    4648        if($data === false){
    4749                echo json_encode($result);
    … …  
    5153                else
    5254                        error_log($message);
     55                return;
    5356        }
    5457
    … …  
    6568        $lastTime = -1;
    6669        $keys = array_keys($data);
    67         $keyTimeframes = $keys[0];
    68         $itemCount = count($keys);
    69         if($itemCount < 0 ){
     70        if(count($keys) < 0 ){
    7071                error_log("No keys in fetched rrd data from ". $dbFile);
    7172                echo json_encode("");
    7273                exit;
    7374        }
    74         $timeframesCount = count($data[$keyTimeframes]);
    7575        if($debug > 0){
    76                 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
     76                echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
    7777        }
    78         $keysTimeframes = array_keys($data[$keyTimeframes]);
    79         for($i = 0; $i < $timeframesCount; $i++){
    80                 $time = $keysTimeframes[$i];
     78        foreach($data[$keys[0]] as $timeframe => $value){
    8179                $allValid = true;
     80                $gpsFix = false;
    8281                $tmp = array();
    8382                foreach($keys as $key){
    8483                        if($key == "gpsfix"){
    85                                 switch($data[$key][$time]){
    86                                         case 1:
     84                                switch($data[$key][$timeframe]){
     85                                        case 2:
    8786                                                $tmp[$key] = "2D";
     87                                                $gpsFix = true;
    8888                                                break;
    89                                         case 2:
     89                                        case 3:
    9090                                                $tmp[$key] = "3D";
     91                                                $gpsFix = true;
    9192                                                break;
    9293                                        default:
    … …  
    9596                        }
    9697                        else{
    97                                 $tmp[$key] = $data[$key][$time];
     98                                $tmp[$key] = $data[$key][$timeframe];
    9899                                if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    99100                                        $allValid = false;
    … …  
    108109                // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    109110                if($allValid){
    110                         if(!array_key_exists($time, $result["track"])){
    111                                 $result["track"][$time] = $tmp;
    112                         }
    113                         $lastTime = $time;
     111                        $lastValid = $tmp;
     112                        if($gpsFix){
     113                                if(!array_key_exists($timeframe, $result["track"])){
     114                                        $result["track"][$timeframe] = $tmp;
     115                                }
     116                        }
    114117                }
    115118                else{
    … …  
    121124                }
    122125        }
    123 
    124126        ksort($result["track"]);
    125127}
    … …  
    165167                exit;
    166168        }
    167         // Fetch entries of the last 30 seconds
    168         addRrdData("5s", "30s", $result, $lastTime);
    169         if($lastTime > -1){
    170                 $result["lastvalid"] = $result["track"][$lastTime];
     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;
    171173        }
    172         addRrdData("15s", "5minutes", $result, $lastTime);
    173         addRrdData("30s", "12h", $result, $lastTime);
     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);
    174178}
    175179
    176180if($debug > 0){
    177         echo json_encode($result,  JSON_PRETTY_PRINT);
     181        echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    178182}
    179183else{
    180         echo json_encode($result);
     184        echo json_encode($result) ."\n";
    181185}
    182186
    183 xdebug_stop_trace();
    184 
    185187?>
Note: See TracChangeset for help on using the changeset viewer.