Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
replace locationmanager with locationmapper and devicemapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Henni committed Sep 4, 2015
1 parent b4279de commit 8be110d
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 148 deletions.
31 changes: 14 additions & 17 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

use \OCP\AppFramework\App;
use \OCA\Maps\Db\CacheManager;
use \OCA\Maps\Db\LocationManager;
use \OCA\Maps\Db\DeviceMapper;
use \OCA\Maps\Db\LocationMapper;
use \OCA\Maps\Controller\PageController;
use \OCA\Maps\Controller\LocationController;

Expand All @@ -36,40 +37,36 @@ public function __construct (array $urlParams=array()) {
$c->query('Request'),
$c->query('UserId'),
$c->query('CacheManager'),
$c->query('LocationManager')
$c->query('DeviceMapper')
);
});
$container->registerService('LocationController', function($c) {
return new LocationController(
$c->query('AppName'),
$c->query('Request'),
$c->query('LocationManager'),
$c->query('LocationMapper'),
$c->query('DeviceMapper'),
$c->query('UserId')
);
});

$container->registerService('CacheManager', function($c) {
return new CacheManager(
$c->query('ServerContainer')->getDb()
);
});
$container->registerService('LocationManager', function($c) {
return new LocationManager(
$container->registerService('LocationMapper', function($c) {
return new LocationMapper(
$c->query('ServerContainer')->getDb()
);
});

/**
* Core
*/
$container->registerService('UserId', function($c) {
return \OCP\User::getUser();
});
$container->registerService('Db', function() {
return new Db();
$container->registerService('DeviceMapper', function($c) {
return new DeviceMapper(
$c->query('ServerContainer')->getDb()
);
});

}


}
}
71 changes: 48 additions & 23 deletions controller/locationcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace OCA\Maps\Controller;

use OCA\Maps\Db\LocationManager;
use OCA\Maps\Db\Device;
use OCA\Maps\Db\DeviceMapper;
use OCA\Maps\Db\Location;
use OCA\Maps\Db\LocationMapper;
use \OCP\IRequest;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\ApiController;
Expand All @@ -20,10 +23,13 @@
class LocationController extends ApiController {

private $userId;
private $locationManager;
public function __construct($appName, IRequest $request, LocationManager $locationManager, $userId) {
private $locationMapper;
private $deviceMapper;

public function __construct($appName, IRequest $request, LocationMapper $locationMapper, DeviceMapper $deviceMapper, $userId) {
parent::__construct($appName, $request);
$this->locationManager = $locationManager;
$this->locationMapper = $locationMapper;
$this->deviceMapper = $deviceMapper;
$this->userId = $userId;
}

Expand All @@ -37,27 +43,35 @@ public function __construct($appName, IRequest $request, LocationManager $locati
* @param $altitude int
* @param $speed int
* @param $hash string
* @return JSONResponse
*/
public function update($lat, $lon, $timestamp, $hdop, $altitude, $speed, $hash) {
$location['lat'] = $lat;
$location['lng'] = $lon;

$location = new Location();
$location->lat = $lat;
$location->lng = $lon;
if((string)(float)$timestamp === $timestamp) {
if(strtotime(date('d-m-Y H:i:s',$timestamp)) === (int)$timestamp) {
$location['timestamp'] = (int)$timestamp;
$location->timestamp = (int)$timestamp;
} elseif(strtotime(date('d-m-Y H:i:s',$timestamp/1000)) === (int)floor($timestamp/1000)) {
$location['timestamp'] = (int)floor($timestamp/1000);
$location->timestamp = (int)floor($timestamp/1000);
}
} else {
$location['timestamp'] = strtotime($timestamp);
$location->timestamp = strtotime($timestamp);
}
$location['hdop'] = $hdop;
$location['altitude'] = $altitude;
$location['speed'] = $speed;
$location['device_hash'] = $hash;
$location->hdop = $hdop;
$location->altitude = $altitude;
$location->speed = $speed;
$location->deviceHash = $hash;

/* Only save location if hash exists in db */
if ( $this->locationManager->checkHash($location['device_hash']) ){
$this->locationManager->save($location);
try {
$this->deviceMapper->findByHash($hash);
return new JSONResponse($this->locationMapper->insert($location));
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
return new JSONResponse([
'error' => $e->getMessage()
]);
}
}

Expand All @@ -68,10 +82,16 @@ public function update($lat, $lon, $timestamp, $hdop, $altitude, $speed, $hash)
* @return JSONResponse
*/
public function addDevice($name){
$deviceName = $name;
$hash = uniqid();
$deviceId = $this->locationManager->addDevice($deviceName,$hash,$this->userId);
$response = array('id'=> $deviceId,'hash'=>$hash);
$device = new Device();
$device->name = $name;
$device->hash = uniqid();
$device->created = time();
$device->userId = $this->userId;

/* @var $device Device */
$device = $this->deviceMapper->insert($device);

$response = array('id'=> $device->getId(),'hash'=>$device->hash);
return new JSONResponse($response);
}

Expand All @@ -81,7 +101,7 @@ public function addDevice($name){
* @return JSONResponse
*/
public function loadDevices(){
$response = $this->locationManager->loadAll($this->userId);
$response = $this->deviceMapper->findAll($this->userId);
return new JSONResponse($response);
}

Expand All @@ -100,8 +120,9 @@ public function loadLocations($devices, $from, $till, $limit){
$till = ($till != '') ? strtotime($till) : strtotime('now');
$limit = ($limit != '') ? (int) $limit : 2000;
$response = array();
foreach($deviceIds as $device){
$response[$device] = $this->locationManager->loadHistory($device,$from,$till,$limit);
foreach($deviceIds as $deviceId){
$hash = $this->deviceMapper->findById($deviceId)->hash;
$response[$deviceId] = $this->locationMapper->findBetween($hash, $from, $till, $limit);
}
return new JSONResponse($response);
}
Expand All @@ -113,7 +134,11 @@ public function loadLocations($devices, $from, $till, $limit){
* @return JSONResponse
*/
public function removeDevice($deviceId){
$this->locationManager->remove($deviceId,$this->userId);
/* @var $device Device */
$device = $this->deviceMapper->findById($deviceId);
if($device->userId == $this->userId) {
$this->deviceMapper->delete($device);
}
return new JSONResponse();
}

Expand Down
12 changes: 8 additions & 4 deletions controller/pagecontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\Maps\Controller;

use \OCA\Maps\Db\DeviceMapper;
use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Controller;
Expand All @@ -20,12 +21,14 @@ class PageController extends Controller {

private $userId;
private $cacheManager;
private $locationManager;
public function __construct($appName, IRequest $request, $userId, $cacheManager,$locationManager) {
private $deviceMapper;
public function __construct($appName, IRequest $request, $userId,
CacheManager $cacheManager,
DeviceMapper $deviceMapper) {
parent::__construct($appName, $request);
$this -> userId = $userId;
$this -> cacheManager = $cacheManager;
$this -> locationManager = $locationManager;
$this -> deviceMapper = $deviceMapper;
}

/**
Expand All @@ -39,7 +42,8 @@ public function __construct($appName, IRequest $request, $userId, $cacheManager,
* @NoCSRFRequired
*/
public function index() {
$params = array('user' => $this -> userId,'devices'=>$this->locationManager->loadAll($this->userId));

$params = array('user' => $this -> userId,'devices'=>$this->deviceMapper->findAll($this->userId));
$response = new TemplateResponse('maps', 'main', $params);
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
Expand Down
2 changes: 1 addition & 1 deletion db/cachemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class CacheManager {
private $userid;
private $db;
public function __construct($db) {
public function __construct(IDb $db) {
$this -> db = $db;
}

Expand Down
11 changes: 11 additions & 0 deletions db/device.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace OCA\Maps\Db;

use OCP\AppFramework\Db\Entity;

class Device extends Entity {
public $userId;
public $name;
public $hash;
public $created;
}
49 changes: 49 additions & 0 deletions db/devicemapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace OCA\Maps\Db;

use OCP\AppFramework\Db\Mapper;
use OCP\IDBConnection;

class DeviceMapper extends Mapper {

public function __construct(IDBConnection $db) {
parent::__construct($db, 'maps_location_track_users', '\OCA\Maps\Db\Device');
}

/**
* @param string $hash
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Device
*/
public function findByHash($hash) {
$sql = 'SELECT * FROM `*PREFIX*maps_location_track_users` '.
'WHERE `hash` = ?';
return $this->findEntity($sql, [$hash]);
}

/**
* @param int $id
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Device
*/
public function findById($id) {
$sql = 'SELECT * FROM `*PREFIX*maps_location_track_users` '.
'WHERE `id` = ?';
return $this->findEntity($sql, [$id]);
}

/**
* @param string $userId
* @param int $limit
* @param int $offset
* @return Device[]
*/
public function findAll($userId, $limit=null, $offset=null) {
$sql = 'SELECT * FROM `*PREFIX*maps_location_track_users`'.
'WHERE `user_id` = ?';
return $this->findEntities($sql, [$userId], $limit, $offset);
}

}
14 changes: 14 additions & 0 deletions db/location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace OCA\Maps\Db;

use OCP\AppFramework\Db\Entity;

class Location extends Entity {
public $deviceHash;
public $lat;
public $lng;
public $timestamp;
public $hdop;
public $altitude;
public $speed;
}
Loading

0 comments on commit 8be110d

Please sign in to comment.