diff --git a/config/services.yaml b/config/services.yaml index 69b75878..cb7b5197 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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)%" diff --git a/public/assets/js/dashboard_map.js b/public/assets/js/dashboard_map.js index a0857a47..81230953 100644 --- a/public/assets/js/dashboard_map.js +++ b/public/assets/js/dashboard_map.js @@ -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); diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 3a3e5173..abdeb6fa 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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, ]); diff --git a/src/EventListener/JobOrderActiveCache.php b/src/EventListener/JobOrderActiveCacheListener.php similarity index 77% rename from src/EventListener/JobOrderActiveCache.php rename to src/EventListener/JobOrderActiveCacheListener.php index 3ce0502a..7b4c552a 100644 --- a/src/EventListener/JobOrderActiveCache.php +++ b/src/EventListener/JobOrderActiveCacheListener.php @@ -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() diff --git a/src/Service/JobOrderCache.php b/src/Service/JobOrderCache.php new file mode 100644 index 00000000..5e3a04f5 --- /dev/null +++ b/src/Service/JobOrderCache.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Service/RedisClientProvider.php b/src/Service/RedisClientProvider.php index 2572370e..80dd06fa 100644 --- a/src/Service/RedisClientProvider.php +++ b/src/Service/RedisClientProvider.php @@ -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) {