source: src/webapp/webservice/inc/GnuBoat/TrackItem.php@ 6da03be

debian/bullseye
Last change on this file since 6da03be 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: 6.5 KB
RevLine 
[008bce9]1<?php
2/*
3 Copyright (C) 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
21/**
22 * @brief One item in a track
23 */
24class TrackItem
25{
[0662b2d]26 function __construct($timestamp = null, $latitude = null, $longitude = null)
[008bce9]27 {
[0662b2d]28 $this->setTimestamp($timestamp);
29 $this->setLatitude($latitude);
30 $this->setLongitude($longitude);
[008bce9]31 $this->_speedThroughWater = null;
32 $this->_speedOverGround = null;
33 $this->_courseOverGround = null;
34 $this->_waterDepth = null;
35 $this->_waterTemperature = null;
36 $this->_gpsfix = null;
[0662b2d]37
38 $this->_properties = [
39 "time" => ["timestamp", "setTimestamp"],
40 "latitude" => ["latitude", "setLatitude"],
41 "longitude" => ["longitude", "setLongitude"],
42 "speedthroughwater" => ["speedThroughWater", "setSpeedThroughWater"],
43 "speedoverground" => ["speedOverGround", "setSpeedOverGround"],
44 "courseoverground" => ["courseOverGround", "setCourseOverGround"],
45 "waterdepth" => ["waterDepth", "setWaterDepth"],
46 "watertemperature" => ["waterTemperature", "setWaterTemperature"],
47 "gpsfix" => ["gpsFix", "setGpsFix"],
48 ];
[008bce9]49 }
50
[0662b2d]51 public function hasProperty($property)
52 {
53 return array_key_exists(strtolower($property), $this->_properties);
54 }
55
56 public function getProperty($property)
57 {
58 $property = strtolower($property);
59 if(!$this->hasProperty($property))
60 return null;
61 return call_user_func(array($this, $this->_properties[$property][0]));
62 }
63
64 public function setProperty($property, $value)
65 {
66 $property = strtolower($property);
67 if(!$this->hasProperty($property))
68 return false;
69 return call_user_func(array($this, $this->_properties[$property][1]), $value);
70 }
71
[008bce9]72 public function setTimestamp($time)
73 {
[0662b2d]74 if(!is_numeric($time) || is_nan($time)){
75 $this->_time = null;
76 return false;
77 }
[008bce9]78 $this->_time = $time;
[0662b2d]79 return true;
[008bce9]80 }
81
82 public function setDateTime($dateTime)
83 {
84 $timezone = new \DateTimeZone("UTC");
[0662b2d]85 if($dateTime == null){
[008bce9]86 $this->_time = null;
[0662b2d]87 return true;
88 }
[008bce9]89 $dateTime->setTimezone($timezone);
90 $this->_time = $dateTime->getTimestamp();
[0662b2d]91 return true;
[008bce9]92 }
93
94 public function timestamp()
95 {
96 return $this->_time;
97 }
98
[0662b2d]99 public function setLatitude($latitude)
[008bce9]100 {
[0662b2d]101 if(!is_numeric($latitude) || is_nan($latitude)){
102 $this->_latitude = null;
103 return false;
104 }
[008bce9]105 $this->_latitude = $latitude;
[0662b2d]106 return true;
[008bce9]107 }
108
109 public function latitude()
110 {
111 return $this->_latitude;
112 }
113
[0662b2d]114 public function setLongitude($longitude)
[008bce9]115 {
[0662b2d]116 if(!is_numeric($longitude) || is_nan($longitude)){
117 $this->_longitude = null;
118 return false;
119 }
[008bce9]120 $this->_longitude = $longitude;
[0662b2d]121 return true;
[008bce9]122 }
123
124 public function longitude()
125 {
126 return $this->_longitude;
127 }
128
[0662b2d]129 public function setSpeedThroughWater($speedThroughWater)
[008bce9]130 {
[0662b2d]131 if(!is_numeric($speedThroughWater) || is_nan($speedThroughWater)){
132 $this->_speedThroughWater = null;
133 return false;
134 }
[008bce9]135 $this->_speedThroughWater = $speedThroughWater;
[0662b2d]136 return true;
[008bce9]137 }
138
139 public function speedThroughWater()
140 {
141 return $this->_speedThroughWater;
142 }
143
[0662b2d]144 public function setSpeedOverGround($speedOverGround)
[008bce9]145 {
[0662b2d]146 if(!is_numeric($speedOverGround) || is_nan($speedOverGround)){
147 $this->_speedOverGround = null;
148 return false;
149 }
[008bce9]150 $this->_speedOverGround = $speedOverGround;
[0662b2d]151 return true;
[008bce9]152 }
153
154 public function speedOverGround()
155 {
156 return $this->_speedOverGround;
157 }
158
[0662b2d]159 public function setCourseOverGround($courseOverGround)
[008bce9]160 {
[0662b2d]161 if(!is_numeric($courseOverGround) || is_nan($courseOverGround)){
162 $this->_courseOverGround = null;
163 return false;
164 }
[008bce9]165 $this->_courseOverGround = $courseOverGround;
[0662b2d]166 return true;
[008bce9]167 }
168
169 public function courseOverGround()
170 {
171 return $this->_courseOverGround;
172 }
173
[0662b2d]174 public function setWaterDepth($waterDepth)
[008bce9]175 {
[0662b2d]176 if(!is_numeric($waterDepth) || is_nan($waterDepth)){
177 $this->_waterDepth = null;
178 return false;
179 }
[008bce9]180 $this->_waterDepth = $waterDepth;
[0662b2d]181 return true;
[008bce9]182 }
183
184 public function waterDepth()
185 {
186 return $this->_waterDepth;
187 }
188
[0662b2d]189 public function setWaterTemperature($waterTemperature)
[008bce9]190 {
[0662b2d]191 if(!is_numeric($waterTemperature) || is_nan($waterTemperature)){
192 $this->_waterTemperature = null;
193 return false;
194 }
[008bce9]195 $this->_waterTemperature = $waterTemperature;
[0662b2d]196 return true;
[008bce9]197 }
198
199 public function waterTemperature()
200 {
201 return $this->_waterTemperature;
202 }
203
[0662b2d]204 public function setGpsFix($gpsFix)
[008bce9]205 {
[0662b2d]206 if(!is_numeric($gpsFix) || is_nan($gpsFix)){
207 $this->_gpsFix = null;
208 return false;
209 }
[008bce9]210 $this->_gpsFix = $gpsFix;
[0662b2d]211 return true;
[008bce9]212 }
213
214 public function gpsFix()
215 {
216 return $this->_gpsFix;
217 }
218
[0662b2d]219 public function gpsFixString()
220 {
221 switch($this->gpsFix()){
222 case 2:
223 return "2D";
224 case 3:
225 return "3D";
226 default:
227 return "-";
228 }
229 }
230
231 public function gpsFixed()
232 {
233 if($this->gpsFix() != null && $this->gpsFix() > 1)
234 return true;
235 return false;
236 }
237
[008bce9]238 public function isValid()
239 {
240 if($this->timestamp() == null)
241 return false;
242 if($this->latitude() == null)
243 return false;
244 if($this->longitude() == null)
245 return false;
246 return true;
247 }
248
249 public function toArray()
250 {
[0662b2d]251 $speedThroughWater = ($this->speedThroughWater() != null) ? $this->speedThroughWater() : "-";
252 $speedOverGround = ($this->speedOverGround() != null) ? $this->speedOverGround() : "-";
253 $courseOverGround = ($this->courseOverGround() != null) ? $this->courseOverGround() : "-";
254 $waterDepth = ($this->waterDepth() != null) ? $this->waterDepth() : "-";
255 $waterTemperature = ($this->waterTemperature() != null) ? $this->waterTemperature() : "-";
256 $gpsFix = $this->gpsFixString();
[008bce9]257
258 return [
259 "latitude" => $this->latitude(),
260 "longitude" => $this->longitude(),
261 "speedthroughwater" => $speedThroughWater,
262 "speedoverground" => $speedOverGround,
263 "courseoverground" => $courseOverGround,
264 "waterdepth" => $waterDepth,
265 "watertemperature" => $waterTemperature,
266 "gpsfix" => $gpsFix,
267 ];
268 }
269
270 private $_time;
271 private $_latitude, $_longitude;
272 private $_speedThroughWater, $_speedOverGround, $_courseOverGround;
273 private $_waterDepth, $_waterTemperature;
274 private $_gpsFix;
275}
276
277?>
Note: See TracBrowser for help on using the repository browser.