Ignore:
Timestamp:
Aug 30, 2017, 2:45:37 PM (9 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, debian/bullseye, debian/jessie, master
Children:
a50c18f
Parents:
89c510f
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webservice/inc/GnuBoat/TrackItem.php

    r89c510f r0662b2d  
    2424class TrackItem
    2525{
    26         function __construct()
    27         {
    28                 $this->_time = null;
    29                 $this->_latitude = null;
    30                 $this->_longitude = null;
     26        function __construct($timestamp = null, $latitude = null, $longitude = null)
     27        {
     28                $this->setTimestamp($timestamp);
     29                $this->setLatitude($latitude);
     30                $this->setLongitude($longitude);
    3131                $this->_speedThroughWater = null;
    3232                $this->_speedOverGround = null;
    … …  
    3535                $this->_waterTemperature = null;
    3636                $this->_gpsfix = null;
    37         }
    38 
     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                ];
     49        }
     50
     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       
    3972        public function setTimestamp($time)
    4073        {
     74                if(!is_numeric($time) || is_nan($time)){
     75                        $this->_time = null;
     76                        return false;
     77                }
    4178                $this->_time = $time;
     79                return true;
    4280        }
    4381       
    … …  
    4583        {
    4684                $timezone = new \DateTimeZone("UTC");
    47                 if($dateTime == null)
     85                if($dateTime == null){
    4886                        $this->_time = null;
     87                        return true;
     88                }
    4989                $dateTime->setTimezone($timezone);
    5090                $this->_time = $dateTime->getTimestamp();
     91                return true;
    5192        }
    5293       
    … …  
    5697        }
    5798       
    58                 public function setLatitude($latitude)
    59         {
     99        public function setLatitude($latitude)
     100        {
     101                if(!is_numeric($latitude) || is_nan($latitude)){
     102                        $this->_latitude = null;
     103                        return false;
     104                }
    60105                $this->_latitude = $latitude;
     106                return true;
    61107        }
    62108       
    … …  
    66112        }
    67113       
    68                 public function setLongitude($longitude)
    69         {
     114        public function setLongitude($longitude)
     115        {
     116                if(!is_numeric($longitude) || is_nan($longitude)){
     117                        $this->_longitude = null;
     118                        return false;
     119                }
    70120                $this->_longitude = $longitude;
     121                return true;
    71122        }
    72123       
    … …  
    76127        }
    77128       
    78                 public function setSpeedThroughWater($speedThroughWater)
    79         {
     129        public function setSpeedThroughWater($speedThroughWater)
     130        {
     131                if(!is_numeric($speedThroughWater) || is_nan($speedThroughWater)){
     132                        $this->_speedThroughWater = null;
     133                        return false;
     134                }
    80135                $this->_speedThroughWater = $speedThroughWater;
     136                return true;
    81137        }
    82138       
    … …  
    86142        }
    87143       
    88                 public function setSpeedOverGround($speedOverGround)
    89         {
     144        public function setSpeedOverGround($speedOverGround)
     145        {
     146                if(!is_numeric($speedOverGround) || is_nan($speedOverGround)){
     147                        $this->_speedOverGround = null;
     148                        return false;
     149                }
    90150                $this->_speedOverGround = $speedOverGround;
     151                return true;
    91152        }
    92153       
    … …  
    96157        }
    97158       
    98                 public function setCourseOverGround($courseOverGround)
    99         {
     159        public function setCourseOverGround($courseOverGround)
     160        {
     161                if(!is_numeric($courseOverGround) || is_nan($courseOverGround)){
     162                        $this->_courseOverGround = null;
     163                        return false;
     164                }
    100165                $this->_courseOverGround = $courseOverGround;
     166                return true;
    101167        }
    102168       
    … …  
    106172        }
    107173       
    108                 public function setWaterDepth($waterDepth)
    109         {
     174        public function setWaterDepth($waterDepth)
     175        {
     176                if(!is_numeric($waterDepth) || is_nan($waterDepth)){
     177                        $this->_waterDepth = null;
     178                        return false;
     179                }
    110180                $this->_waterDepth = $waterDepth;
     181                return true;
    111182        }
    112183       
    … …  
    116187        }
    117188       
    118                 public function setWaterTemperature($waterTemperature)
    119         {
     189        public function setWaterTemperature($waterTemperature)
     190        {
     191                if(!is_numeric($waterTemperature) || is_nan($waterTemperature)){
     192                        $this->_waterTemperature = null;
     193                        return false;
     194                }
    120195                $this->_waterTemperature = $waterTemperature;
     196                return true;
    121197        }
    122198       
    … …  
    126202        }
    127203       
    128                 public function setGpsFix($gpsFix)
    129         {
     204        public function setGpsFix($gpsFix)
     205        {
     206                if(!is_numeric($gpsFix) || is_nan($gpsFix)){
     207                        $this->_gpsFix = null;
     208                        return false;
     209                }
    130210                $this->_gpsFix = $gpsFix;
     211                return true;
    131212        }
    132213       
    … …  
    136217        }
    137218       
     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       
    138238        public function isValid()
    139239        {
    … …  
    149249        public function toArray()
    150250        {
    151                 $speedThroughWater = ($this->speedThroughWater() != null) ? $this->speedThroughWater() : 0;
    152                 $speedOverGround = ($this->speedOverGround() != null) ? $this->speedOverGround() : 0;
    153                 $courseOverGround = ($this->courseOverGround() != null) ? $this->courseOverGround() : 0;
    154                 $waterDepth = ($this->waterDepth() != null) ? $this->waterDepth() : 0;
    155                 $waterTemperature = ($this->waterTemperature() != null) ? $this->waterTemperature() : 0;
    156                 $gpsFix = ($this->gpsFix() != null) ? $this->gpsFix() : 0;
     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();
    157257       
    158258                return [
Note: See TracChangeset for help on using the changeset viewer.