1
0

DB Query returns Location objects.

This commit is contained in:
Markus Birth 2018-08-12 22:16:47 +02:00
parent 22598acfc9
commit 20713d79c9
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
4 changed files with 46 additions and 24 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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'),

View File

@ -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)) {