Have active job orders loaded from cache in dashboard map #299

This commit is contained in:
Kendrick Chan 2020-01-26 00:58:00 +08:00
parent 9e00c3f115
commit cba80ddd90
6 changed files with 103 additions and 25 deletions

View file

@ -204,10 +204,9 @@ services:
App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet"
#App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google"
App\EventListener\JobOrderActiveCache:
App\EventListener\JobOrderActiveCacheListener:
arguments:
$redis: "@App\\Service\\RedisClientProvider"
$key: "%env(JO_ACTIVE_CACHE_KEY)%"
$jo_cache: "@App\\Service\\JobOrderCache"
$mqtt: "@App\\Service\\MQTTClient"
tags:
- name: 'doctrine.orm.entity_listener'
@ -219,3 +218,8 @@ services:
- name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'App\Entity\JobOrder'
App\Service\JobOrderCache:
arguments:
$redis_prov: "@App\\Service\\RedisClientProvider"
$active_jo_key: "%env(JO_ACTIVE_CACHE_KEY)%"

View file

@ -124,6 +124,14 @@ class DashboardMap {
my.layer_groups.customer.clearLayers();
// get riders and mark
var riders = response.riders;
var jos = response.jos;
$.each(jos, function(id, data) {
var lat = data.latitude;
var lng = data.longitude;
my.putCustomerMarker(id, lat, lng);
});
$.each(riders, function(rider_id, rider_data) {
// rider location
@ -140,7 +148,7 @@ class DashboardMap {
if (rider_data['has_jo']) {
var jo_data = rider_data['jo'];
my.putCustomerMarker(jo_data['id'], clat, clng);
// my.putCustomerMarker(jo_data['id'], clat, clng);
my.putRiderActiveJOMarker(rider_id, lat, lng);
} else {
my.putRiderAvailableMarker(rider_id, lat, lng);

View file

@ -9,6 +9,7 @@ use Doctrine\ORM\EntityManagerInterface;
use App\Service\RiderTracker;
use App\Service\GISManagerInterface;
use App\Service\JobOrderCache;
use App\Entity\Rider;
@ -18,8 +19,11 @@ class HomeController extends Controller
/**
* @Menu(selected="home")
*/
public function index(EntityManagerInterface $em, RiderTracker $rider_tracker,
GISManagerInterface $gis_manager)
public function index(
EntityManagerInterface $em,
RiderTracker $rider_tracker,
GISManagerInterface $gis_manager
)
{
// get map
$params['map_js_file'] = $gis_manager->getJSInitFile();
@ -27,9 +31,18 @@ class HomeController extends Controller
return $this->render('home.html.twig', $params);
}
public function getRiderLocations(EntityManagerInterface $em, RiderTracker $rider_tracker)
public function getMapLocations(JobOrderCache $jo_cache)
{
$active_jos = $jo_cache->getAllActiveJobOrders();
// get active JOs from cache
}
public function getRiderLocations(JobOrderCache $jo_cache, EntityManagerInterface $em, RiderTracker $rider_tracker)
{
// TODO: get active riders from cache
$active_jos = $jo_cache->getAllActiveJobOrders();
// TODO: get active JOs from cache
// get all riders
$riders = $em->getRepository(Rider::class)->findAll();
@ -83,6 +96,7 @@ class HomeController extends Controller
}
return $this->json([
'jos' => $active_jos,
'riders' => $locations,
]);

View file

@ -4,22 +4,18 @@ namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Service\RedisClientProvider;
use App\Service\JobOrderCache;
use App\Ramcar\JOStatus;
use App\Entity\JobOrder;
class JobOrderActiveCache
class JobOrderActiveCacheListener
{
protected $redis;
protected $key;
protected $mqtt;
public function __construct(RedisClientProvider $redis, $key, $mqtt)
public function __construct(JobOrderCache $jo_cache, $mqtt)
{
$this->redis = $redis->getRedisClient();
$this->key = $key;
$this->jo_cache = $jo_cache;
$this->mqtt = $mqtt;
}
@ -76,18 +72,11 @@ class JobOrderActiveCache
protected function processActiveJO($jo)
{
$coords = $jo->getCoordinates();
// put in redis cache
error_log('add ' . $this->key . ' - (' . $coords->getLongitude() . ', ' . $coords->getLatitude() . ') - ' . $jo->getID());
$this->redis->geoadd(
$this->key,
$coords->getLongitude(),
$coords->getLatitude(),
$jo->getID()
);
// save in cache
$jo_cache->addActiveJobOrder($jo);
// publish to mqtt
// TODO: do we put the key in config?
$this->mqtt->publish(
'jo/' . $jo->getID() . '/location',
$coords->getLatitude() . ':' . $coords->getLongitude()

View file

@ -0,0 +1,58 @@
<?php
namespace App\Service;
use App\Service\RedisClientProvider;
use App\Entity\JobOrder;
class JobOrderCache
{
protected $redis;
protected $active_jo_key;
public function __construct(RedisClientProvider $redis_prov, $active_jo_key)
{
$this->redis = $redis_prov->getRedisClient();
$this->active_jo_key = $active_jo_key;
}
public function addActiveJobOrder(JobOrder $jo)
{
$coords = $jo->getCoordinates();
$this->redis->geoadd(
$this->active_jo_key,
$coords->getLongitude(),
$coords->getLatitude(),
$jo->getID()
);
}
public function getAllActiveJobOrders()
{
$all_jo = $this->redis->georadius(
$this->active_jo_key,
0,
0,
22000,
'km',
['WITHCOORD' => true]
);
$jo_locs = [];
foreach ($all_jo as $jo_data)
{
$id = $jo_data[0];
$lng = $jo_data[1][0];
$lat = $jo_data[1][1];
$jo_locs[$id] = [
'longitude' => $lng,
'latitude' => $lat,
];
}
// error_log(print_r($all_jo, true));
return $jo_locs;
}
}

View file

@ -18,12 +18,17 @@ class RedisClientProvider
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->redis = null;
$this->connect();
}
protected function connect()
{
// already connected
if ($this->redis != null)
return $this->redis;
// if password is specified attempt connection
if (strlen($this->password) > 0)
{