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

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

Improved webservice class to use TrackItem in ServiceData.php and added some tests

  • Property mode set to 100644
File size: 3.9 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" => 4,
32 "longitude" => 4,
33 "speedthroughwater" => 1,
34 "speedoverground" => 1,
35 "courseoverground" => 1,
36 "waterdepth" => 1,
37 "watertemperature" => 1,
38 );
39 }
40
41 protected function process()
42 {
43 $debug = $this->debug();
44 $config = new Config();
45 $dbFile = $config->databaseFile();
46
47 $result = array();
48 $result["lastvalid"] = array();
49 $result["track"] = array();
50
51 if(!file_exists($dbFile))
52 return $this->error("Database file ". $dbFile ." does not exist");
53
54 // Fetch entries of the last 30s with maximum (available) resolution of 2s
55 $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
56 if($lastValid !== false){
57 $result["lastvalid"] = $lastValid->toArray();
58 }
59 // Fetch entries of the last 1h with maximum (available) resolution of 10s
60 $this->addRrdData($dbFile, "10", "36000", $result, $lastTime);
61 // Fetch entries of the last 24h with maximum (available) resolution of 60s
62 $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
63
64 return $this->output($result);
65 }
66
67 private function addRrdData($dbFile, $interval, $period, &$result, &$lastValid){
68 $debug = $this->debug();
69 $lastValid = false;
70
71 $endTime = floor(time() / $interval) * $interval;
72 $startTime = $endTime - $period;
73 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
74 // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
75 if($data === false)
76 return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result);
77
78 if($debug > 2){
79 var_dump($data);
80 }
81
82 $data = $data["data"];
83
84 // transform them into json format
85 $i = 0;
86 $lastValid = false;
87 $keys = array_keys($data);
88 $keyTimeframes = $keys[0];
89 $itemCount = count($keys);
90 if($itemCount < 0 )
91 return $this->error("No keys in fetched rrd data from ". $dbFile);
92
93 $timeframesCount = count($data[$keyTimeframes]);
94 if($debug > 1){
95 error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
96 }
97 foreach($data[$keys[0]] as $timeframe => $value){
98 $tmp = new TrackItem($timeframe);
99 foreach($keys as $key){
100 $value = $data[$key][$timeframe];
101 if(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
102 $value = round($value, $this->_configRound[$key]);
103 if(!$tmp->setProperty($key, $value) && $debug > 1)
104 error_log("Coult not set property ". $key ." to value ". $value ." at ". $timeframe);
105 }
106 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
107 if($tmp->isValid()){
108 $lastValid = $tmp;
109 if($tmp->gpsFixed()){
110 if(!array_key_exists($timeframe, $result["track"])){
111 $result["track"][$timeframe] = $tmp->toArray();
112 }
113 }
114 }
115 else{
116 if($debug > 1){
117 echo "Failed <pre>";
118 var_dump($tmp);
119 echo "</pre>\n";
120 }
121 }
122 }
123 ksort($result["track"]);
124 }
125}
126?>
Note: See TracBrowser for help on using the repository browser.