source: src/webapp/webservice/inc/GnuBoat/ServiceData.php@ 89c510f

0.5 debian/bullseye debian/jessie
Last change on this file since 89c510f was 008bce9, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

First more or less working version of visualising logfiles on the map

  • Property mode set to 100644
File size: 4.6 KB
Line 
1<?php
2/*
3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19namespace GnuBoat;
20
21require_once(__DIR__ ."/../autoloader.php");
22
23class ServiceData extends Service
24{
25 function __construct()
26 {
27 parent::__construct();
28 // configure roundings per value (key) and precision (value)
29 $this->_configRound = array
30 (
31 "latitude" => 5,
32 "longitude" => 5,
33 "speedthroughwater" => 1,
34 "speedoverground" => 1,
35 "waterdepth" => 1,
36 "watertemperature" => 1,
37 );
38 }
39
40 protected function process()
41 {
42 $debug = $this->debug();
43 $config = new Config();
44 $dbFile = $config->databaseFile();
45
46 $result = array();
47 $result["lastvalid"] = array();
48 $result["track"] = array();
49
50 if(!file_exists($dbFile)){
51 if($debug > 0)
52 echo "Database file ". $dbFile ." does not exist!";
53 error_log("Database file ". $dbFile ." does not exist");
54 return;
55 }
56 // Fetch entries of the last 30s with maximum (available) resolution of 2s
57 $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
58 if($lastValid){
59 $result["lastvalid"] = $lastValid;
60 }
61 // Fetch entries of the last 1h with maximum (available) resolution of 10s
62 $this->addRrdData($dbFile, "10", "36000", $result, $lastTime);
63 // Fetch entries of the last 24h with maximum (available) resolution of 60s
64 $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
65
66 if($debug > 0){
67 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
68 }
69 else{
70 header('Content-Type: application/json');
71 echo json_encode($result) ."\n";
72 }
73 }
74
75 private function addRrdData($dbFile, $interval, $period, &$result, &$lastValid){
76 $debug = $this->debug();
77 $lastValid = false;
78
79 $endTime = floor(time() / $interval) * $interval;
80 $startTime = $endTime - $period;
81 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
82 // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
83 if($data === false){
84 echo json_encode($result);
85 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
86 if($debug > 0)
87 echo $message;
88 else
89 error_log($message);
90 return;
91 }
92
93 if($debug > 2){
94 echo "<pre>";
95 var_dump($data);
96 echo "</pre>";
97 }
98
99 $data = $data["data"];
100
101 // transform them into json format
102 $i = 0;
103 $lastValid = -1;
104 $keys = array_keys($data);
105 $keyTimeframes = $keys[0];
106 $itemCount = count($keys);
107 if($itemCount < 0 ){
108 error_log("No keys in fetched rrd data from ". $dbFile);
109 echo json_encode("");
110 exit;
111 }
112 $timeframesCount = count($data[$keyTimeframes]);
113 if($debug > 0){
114 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
115 }
116 foreach($data[$keys[0]] as $timeframe => $value){
117 $allValid = true;
118 $gpsFix = false;
119 $tmp = array();
120 foreach($keys as $key){
121 if($key == "gpsfix"){
122 switch($data[$key][$timeframe]){
123 case 2:
124 $tmp[$key] = "2D";
125 $gpsFix = true;
126 break;
127 case 3:
128 $tmp[$key] = "3D";
129 $gpsFix = true;
130 break;
131 default:
132 $tmp[$key] = "-";
133 }
134 }
135 else{
136 $tmp[$key] = $data[$key][$timeframe];
137 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
138 $allValid = false;
139 if($debug > 1){
140 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
141 }
142 }
143 elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
144 $tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
145 }
146 }
147 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
148 if($allValid){
149 $lastValid = $tmp;
150 if($gpsFix){
151 if(!array_key_exists($timeframe, $result["track"])){
152 $result["track"][$timeframe] = $tmp;
153 }
154 }
155 }
156 else{
157 if($debug > 1){
158 echo "Failed <pre>";
159 var_dump($tmp);
160 echo "</pre>\n";
161 }
162 }
163 }
164 ksort($result["track"]);
165 }
166}
167?>
Note: See TracBrowser for help on using the repository browser.