Add rider cache service #299
This commit is contained in:
parent
bdb306e5e1
commit
4719d89368
7 changed files with 134 additions and 10 deletions
|
|
@ -222,4 +222,10 @@ services:
|
|||
App\Service\JobOrderCache:
|
||||
arguments:
|
||||
$redis_prov: "@App\\Service\\RedisClientProvider"
|
||||
$active_jo_key: "%env(JO_ACTIVE_CACHE_KEY)%"
|
||||
$active_jo_key: "%env(LOCATION_JO_ACTIVE_KEY)%"
|
||||
|
||||
App\Service\RiderCache:
|
||||
arguments:
|
||||
$redis_prov: "@App\\Service\\RedisClientProvider"
|
||||
$loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%"
|
||||
$status_key: "%env(STATUS_RIDER_KEY)%"
|
||||
|
|
|
|||
|
|
@ -136,10 +136,12 @@ class DashboardMap {
|
|||
my.layer_groups.rider_available.clearLayers();
|
||||
my.layer_groups.rider_active_jo.clearLayers();
|
||||
my.layer_groups.customer.clearLayers();
|
||||
// get riders and mark
|
||||
|
||||
// get riders and job orders
|
||||
var riders = response.riders;
|
||||
var jos = response.jos;
|
||||
|
||||
// job orders
|
||||
$.each(jos, function(id, data) {
|
||||
var lat = data.latitude;
|
||||
var lng = data.longitude;
|
||||
|
|
@ -147,6 +149,18 @@ class DashboardMap {
|
|||
my.putCustomerMarker(id, lat, lng);
|
||||
});
|
||||
|
||||
// riders
|
||||
$.each(riders, function(id, data) {
|
||||
var lat = data.latitude;
|
||||
var lng = data.longitude;
|
||||
|
||||
if (data.has_jo)
|
||||
my.putRiderActiveJOMarker(id, lat, lng);
|
||||
else
|
||||
my.putRiderAvailableMarker(id, lat, lng);
|
||||
});
|
||||
|
||||
/*
|
||||
$.each(riders, function(rider_id, rider_data) {
|
||||
// rider location
|
||||
var point = rider_data['loc'];
|
||||
|
|
@ -168,6 +182,7 @@ class DashboardMap {
|
|||
my.putRiderAvailableMarker(rider_id, lat, lng);
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
// console.log(rider_markers);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ class MapEventHandler {
|
|||
var lat = parseFloat(pl_split[0]);
|
||||
var lng = parseFloat(pl_split[1]);
|
||||
|
||||
this.dashmap.putRiderAvaialbleMarker(chan_split[1], lat, lng);
|
||||
this.dashmap.putRiderAvailableMarker(chan_split[1], lat, lng);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use App\Service\RiderTracker;
|
||||
use App\Service\GISManagerInterface;
|
||||
use App\Service\JobOrderCache;
|
||||
use App\Service\RiderCache;
|
||||
|
||||
use App\Entity\Rider;
|
||||
|
||||
|
|
@ -38,13 +39,27 @@ class HomeController extends Controller
|
|||
// get active JOs from cache
|
||||
}
|
||||
|
||||
public function getRiderLocations(JobOrderCache $jo_cache, EntityManagerInterface $em, RiderTracker $rider_tracker)
|
||||
public function getRiderLocations(JobOrderCache $jo_cache, RiderCache $rider_cache, EntityManagerInterface $em, RiderTracker $rider_tracker)
|
||||
{
|
||||
// TODO: get active riders from cache
|
||||
// get active JOs from cache
|
||||
$active_jos = $jo_cache->getAllActiveJobOrders();
|
||||
$riders = $rider_cache->getAllActiveRiders();
|
||||
|
||||
// TODO: get active JOs from cache
|
||||
// TODO: optimize this
|
||||
// get all riders and figure out if they have active jos
|
||||
foreach ($riders as $rider_id => $rider_data)
|
||||
{
|
||||
$rider = $em->getRepository(Rider::class)->find($rider_id);
|
||||
$jo = $rider->getActiveJobOrder();
|
||||
if ($jo == null)
|
||||
$riders[$rider_id]['has_jo'] = false;
|
||||
else
|
||||
$riders[$rider_id]['has_jo'] = true;
|
||||
}
|
||||
|
||||
// get active riders from cache
|
||||
// get all riders
|
||||
/*
|
||||
$riders = $em->getRepository(Rider::class)->findAll();
|
||||
|
||||
$locations = [];
|
||||
|
|
@ -94,10 +109,11 @@ class HomeController extends Controller
|
|||
];
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
return $this->json([
|
||||
'jos' => $active_jos,
|
||||
'riders' => $locations,
|
||||
'riders' => $riders,
|
||||
]);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ use App\Ramcar\JOEventType;
|
|||
use App\Service\InvoiceGeneratorInterface;
|
||||
use App\Service\MQTTClient;
|
||||
use App\Service\RedisClientProvider;
|
||||
use App\Service\RiderCache;
|
||||
|
||||
use App\Entity\RiderSession;
|
||||
use App\Entity\Customer;
|
||||
|
|
@ -209,7 +210,7 @@ class RAPIController extends Controller
|
|||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function login(Request $req, EncoderFactoryInterface $ef, RedisClientProvider $redis)
|
||||
public function login(Request $req, EncoderFactoryInterface $ef, RedisClientProvider $redis, RiderCache $rcache)
|
||||
{
|
||||
$required_params = [
|
||||
'user',
|
||||
|
|
@ -251,6 +252,11 @@ class RAPIController extends Controller
|
|||
|
||||
$rider->setAvailable(true);
|
||||
|
||||
$rider_id = $rider->getID();
|
||||
// cache rider location (default to hub)
|
||||
// TODO: figure out longitude / latitude default
|
||||
$rcache->addActiveRider($rider_id, 0, 0);
|
||||
|
||||
// TODO: log rider logging in
|
||||
|
||||
$em->flush();
|
||||
|
|
@ -289,7 +295,7 @@ class RAPIController extends Controller
|
|||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function logout(Request $req)
|
||||
public function logout(Request $req, RiderCache $rcache)
|
||||
{
|
||||
$required_params = [];
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
|
@ -301,6 +307,9 @@ class RAPIController extends Controller
|
|||
$rider = $this->session->getRider();
|
||||
$rider->setAvailable(false);
|
||||
|
||||
// remove from cache
|
||||
$rcache->removeActiveRider($rider->getID());
|
||||
|
||||
// remove rider from session
|
||||
$this->session->setRider(null);
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class JobOrderCache
|
|||
$this->active_jo_key,
|
||||
0,
|
||||
0,
|
||||
22000,
|
||||
41000,
|
||||
'km',
|
||||
['WITHCOORD' => true]
|
||||
);
|
||||
|
|
|
|||
78
src/Service/RiderCache.php
Normal file
78
src/Service/RiderCache.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Service\RedisClientProvider;
|
||||
use App\Entity\Rider;
|
||||
|
||||
class RiderCache
|
||||
{
|
||||
protected $redis;
|
||||
protected $loc_key;
|
||||
protected $status_key;
|
||||
|
||||
public function __construct(RedisClientProvider $redis_prov, $loc_key, $status_key)
|
||||
{
|
||||
$this->redis = $redis_prov->getRedisClient();
|
||||
$this->loc_key = $loc_key;
|
||||
$this->status_key = $status_key;
|
||||
}
|
||||
|
||||
public function addActiveRider($id, $lat, $lng)
|
||||
{
|
||||
$coords = $jo->getCoordinates();
|
||||
|
||||
$this->redis->geoadd(
|
||||
$this->loc_key,
|
||||
$lng,
|
||||
$lat,
|
||||
$id
|
||||
);
|
||||
}
|
||||
|
||||
public function getAllActiveRiders()
|
||||
{
|
||||
$all_riders = $this->redis->georadius(
|
||||
$this->loc_key,
|
||||
0,
|
||||
0,
|
||||
41000,
|
||||
'km',
|
||||
['WITHCOORD' => true]
|
||||
);
|
||||
|
||||
$locs = [];
|
||||
foreach ($all_riders as $data)
|
||||
{
|
||||
$id = $data[0];
|
||||
$lng = $data[1][0];
|
||||
$lat = $data[1][1];
|
||||
|
||||
$locs[$id] = [
|
||||
'longitude' => $lng,
|
||||
'latitude' => $lat,
|
||||
];
|
||||
}
|
||||
|
||||
// error_log(print_r($all_riders, true));
|
||||
return $locs;
|
||||
}
|
||||
|
||||
public function removeActiveRider($id)
|
||||
{
|
||||
$this->redis->zrem(
|
||||
$this->loc_key,
|
||||
$id
|
||||
);
|
||||
}
|
||||
|
||||
public function incJobOrderCount($id, $status)
|
||||
{
|
||||
$this->redis->hincrby($this->status_key, $id, 1);
|
||||
}
|
||||
|
||||
public function decJobOrderCount($id, $status)
|
||||
{
|
||||
$this->redis->hincrby($this->status_key, $id, -1);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue