diff --git a/lib/Database/AbstractDatabase.php b/lib/Database/AbstractDatabase.php index 2daf28b..d111c2f 100644 --- a/lib/Database/AbstractDatabase.php +++ b/lib/Database/AbstractDatabase.php @@ -3,6 +3,7 @@ namespace OwntracksRecorder\Database; use \OwntracksRecorder\RecordType\AbstractRecordType; +use \OwntracksRecorder\RecordType\Location; class AbstractDatabase { @@ -96,6 +97,12 @@ class AbstractDatabase $min_epoch = time() - $max_age; $sql = 'SELECT * from locations l1 INNER JOIN (SELECT tracker_id, MAX(epoch) AS epoch FROM locations GROUP BY tracker_id) l2 ON l1.tracker_id=l2.tracker_id AND l1.epoch=l2.epoch WHERE l1.epoch >= ?'; $result = $this->query($sql, array($min_epoch)); - return $result; + + $loclist = array(); + foreach ($result as $entry) { + $loclist[] = new Location($entry); + } + + return $loclist; } } diff --git a/lib/RecordType/AbstractRecordType.php b/lib/RecordType/AbstractRecordType.php index 5ccc01b..ecf6986 100755 --- a/lib/RecordType/AbstractRecordType.php +++ b/lib/RecordType/AbstractRecordType.php @@ -9,12 +9,20 @@ class AbstractRecordType implements \Iterator protected $fields = array(); protected $data = array(); - public function __construct() + public function __construct(array $arr = null) { // init empty record foreach ($this->fields as $key => $extkey) { $this->data[$key] = null; } + + if (!is_null($arr)) { + foreach ($arr as $key => $value) { + if (array_key_exists($key, $this->data)) { + $this->data[$key] = $value; + } + } + } } public function __isset($key) @@ -27,6 +35,16 @@ class AbstractRecordType implements \Iterator return $this->data[$key]; } + public function __set($key, $value) + { + if (array_key_exists($key, $this->fields)) { + if (!is_null($this->fields[$key])) { + settype($value, $this->fields[$key][1]); + } + $this->data[$key] = $value; + } + } + public function rewind() { reset($this->data); @@ -70,4 +88,18 @@ class AbstractRecordType implements \Iterator } } } + + public function getJSON() + { + $result = array( + '_type' => $this->type, + ); + foreach ($this->fields as $key => $extdata) { + if (is_null($extdata) || is_null($this->data[$key])) { + continue; + } + $result[$extdata[0]] = $this->data[$key]; + } + return $result; + } } diff --git a/lib/RecordType/Location.php b/lib/RecordType/Location.php index 6bacd22..aa494a2 100755 --- a/lib/RecordType/Location.php +++ b/lib/RecordType/Location.php @@ -21,12 +21,12 @@ class Location extends AbstractRecordType 'heading' => array('cog', 'int'), 'description' => array('desc', 'string'), 'event' => array('event', 'string'), - 'latitude' => array('lat', 'float'), - 'longitude' => array('lon', 'float'), + 'latitude' => array('lat', 'float'), // required in JSON + 'longitude' => array('lon', 'float'), // required in JSON 'radius' => array('rad', 'int'), 'trig' => array('t', 'string'), - 'tracker_id' => array('tid', 'string'), - 'epoch' => array('tst', 'int'), + 'tracker_id' => array('tid', 'string'), // required in JSON (http) + 'epoch' => array('tst', 'int'), // required in JSON 'vertical_accuracy' => array('vac', 'int'), 'velocity' => array('vel', 'int'), 'pressure' => array('p', 'float'), diff --git a/record.php b/record.php index 8f7eae0..126eea9 100644 --- a/record.php +++ b/record.php @@ -61,24 +61,7 @@ $response = array(); // Build list of buddies' last locations $buddies = $sql->getAllLatestMarkers(); foreach ($buddies as $buddy) { - $loc = array( - '_type' => 'location', - 'acc' => $buddy['accuracy'], - 'alt' => $buddy['altitude'], - 'batt' => $buddy['battery_level'], - 'cog' => $buddy['heading'], - 'lat' => $buddy['latitude'], - 'lon' => $buddy['longitude'], - 'rad' => $buddy['radius'], - 't' => $buddy['trig'], - 'tid' => strval($buddy['tracker_id']), - 'tst' => $buddy['epoch'], - 'vac' => $buddy['vertical_accuracy'], - 'vel' => $buddy['velocity'], - 'p' => $buddy['pressure'], - 'conn' => $buddy['connection'], - ); - $response[] = $loc; + $response[] = $buddy->getJSON(); } if (!is_null($response_msg)) {