Files
dev-chiefworks 47f4fad75c Added Other AP
2022-04-26 11:30:34 -04:00

128 lines
3.8 KiB
PHP

<?php
class RadiusApi extends Api
{
public $apiName = 'radius';
const RADIUS_LIMIT = 10000;
const DEFAULT_DIR = 'any';
const DEFAULT_LIMIT = 10;
public function indexAction()
{
return $this->response(
array(
'error' => 'Data not found'
), 404);
}
/**
* Method GET
* Get single record (by id)
* http://DOMAIN/radius/1
* @return string
*/
public function viewAction()
{
//id must be the first parameter after /radius/x
return $this->response(
array(
'error'=> 'Data not found'
), 404);
}
public function createAction()
{
$message = 'Data not found';
$code = 404;
$lat = $this->requestParams['lat'] ?? NULL;
$lng = $this->requestParams['lng'] ?? NULL;
$rad = $this->requestParams['radius'] ?? NULL;
$typ = $this->requestParams['type'] ?? self::DEFAULT_DIR;
$lim = $this->requestParams['limit'] ?? self::DEFAULT_LIMIT;
try {
if ($lat==NULL || $lng==NULL) {
throw new RuntimeException('Invalid GPS coordinates',500);
}
if ($rad<10 || $rad>self::RADIUS_LIMIT) {
throw new RuntimeException('Invalid radius',500);
}
if ($typ!='in' && $typ!='out') {
$typ = self::DEFAULT_DIR;
}
if ($limit<1 || $limit>self::DEFAULT_LIMIT) {
$limit = self::DEFAULT_LIMIT;
}
$db = new Db();
$res = NULL;
$err = NULL;
if ($typ=='in') {
list($res,$err) = Radius::inboundTrips($db->getConnectGPS(), $lat, $lng, $rad);
} else if ($typ=='out') {
list($res,$err) = Radius::outboundTrips($db->getConnectGPS(), $lat, $lng, $rad);
} else {
list($res,$err) = Radius::anyTrips($db->getConnectGPS(), $lat, $lng, $rad);
}
if (!$res) {
if ($err!="") {
throw new RuntimeException($err, 500);
}
throw new RuntimeException('Data not found', 404);
}
$ids = [];
foreach ($res as $f) {
$ids[] = $f["id"];
}
// Get $lim latest trips
list($res,$err) = Radius::limitTrips($db->getConnect(), $ids, $lim, 'travel_date', 'DESC');
if (!$res) {
if ($err!="") {
throw new RuntimeException($err, 500);
}
throw new RuntimeException('Data not found', 404);
}
$trips = [];
foreach ($res as $f) {
$id = $f["id"];
$trip = Trips::getById($db->getConnect(), (int)$id);
if(is_array($trip) && count($trip)>0){
$trips[] = $trip;
}
}
return $this->response(
array(
"lat" => $lat,
"lng" => $lng,
"radius" => $rad,
"type" => $typ,
"limit" => $lim,
"trips" => $trips
), 200);
} catch (RuntimeException $e) {
$message = $e->getMessage();
$code = $e->getCode();
}
return $this->response(
array(
"error" => $message
), $code);
}
public function updateAction()
{
return $this->response(
array(
"error" => "Update error"
), 400);
}
public function deleteAction()
{
return $this->response(
array(
"error" => "Delete error"
), 500);
}
}