source: src/webapp/webservice/inc/GnuBoat/ServiceData.php@ 654b10c

0.5 debian/bullseye debian/jessie
Last change on this file since 654b10c was 1ba85cf, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago
  • Fixed gnuboat daemon logging errors
  • Fixed minor issues in Webservice classes
  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[c455d7b]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 }
[1ba85cf]39
40 protected function process()
[c455d7b]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 echo json_encode("");
52 if($debug > 0)
53 echo "Database file ". $dbFile ." does not exist!";
54 error_log("Database file ". $dbFile ." does not exist");
55 exit;
56 }
57 // Fetch entries of the last 30s with maximum (available) resolution of 2s
58 $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
59 if($lastValid){
60 $result["lastvalid"] = $lastValid;
61 }
62 // Fetch entries of the last 1h with maximum (available) resolution of 10s
63 $this->addRrdData($dbFile, "10", "36000", $result, $lastTime);
64 // Fetch entries of the last 24h with maximum (available) resolution of 60s
65 $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
66
67 if($debug > 0){
68 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
69 }
70 else{
71 header('Content-Type: application/json');
72 echo json_encode($result) ."\n";
73 }
74 }
75
76 private function addRrdData($dbFile, $interval, $period, &$result, &$lastValid){
77 $debug = $this->debug();
78 $lastValid = false;
79
80 $endTime = floor(time() / $interval) * $interval;
81 $startTime = $endTime - $period;
82 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
83 // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
84 if($data === false){
85 echo json_encode($result);
86 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
87 if($debug > 0)
88 echo $message;
89 else
90 error_log($message);
91 return;
92 }
93
94 if($debug > 2){
95 echo "<pre>";
96 var_dump($data);
97 echo "</pre>";
98 }
99
100 $data = $data["data"];
101
102 // transform them into json format
103 $i = 0;
104 $lastValid = -1;
105 $keys = array_keys($data);
106 $keyTimeframes = $keys[0];
107 $itemCount = count($keys);
108 if($itemCount < 0 ){
109 error_log("No keys in fetched rrd data from ". $dbFile);
110 echo json_encode("");
111 exit;
112 }
113 $timeframesCount = count($data[$keyTimeframes]);
114 if($debug > 0){
115 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
116 }
117 foreach($data[$keys[0]] as $timeframe => $value){
118 $allValid = true;
119 $gpsFix = false;
120 $tmp = array();
121 foreach($keys as $key){
122 if($key == "gpsfix"){
123 switch($data[$key][$timeframe]){
124 case 2:
125 $tmp[$key] = "2D";
126 $gpsFix = true;
127 break;
128 case 3:
129 $tmp[$key] = "3D";
130 $gpsFix = true;
131 break;
132 default:
133 $tmp[$key] = "-";
134 }
135 }
136 else{
137 $tmp[$key] = $data[$key][$timeframe];
138 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
139 $allValid = false;
140 if($debug > 1){
[1ba85cf]141 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
[c455d7b]142 }
143 }
144 elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
145 $tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
146 }
147 }
148 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
149 if($allValid){
150 $lastValid = $tmp;
151 if($gpsFix){
152 if(!array_key_exists($timeframe, $result["track"])){
153 $result["track"][$timeframe] = $tmp;
154 }
155 }
156 }
157 else{
158 if($debug > 1){
159 echo "Failed <pre>";
160 var_dump($tmp);
161 echo "</pre>\n";
162 }
163 }
164 }
165 ksort($result["track"]);
166 }
167}
168?>
Note: See TracBrowser for help on using the repository browser.