From 2d6193318a64d1c5241b4fa126ff0b10c45e1a43 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Mon, 27 Jan 2020 04:04:30 +0800 Subject: [PATCH] Add ability to remove customer markers via status publish #299 --- public/assets/js/dashboard_map.js | 14 ++++++++++++ public/assets/js/map_mqtt.js | 13 +++++++++-- .../JobOrderActiveCacheListener.php | 22 ++++++++++++++----- src/Service/JobOrderCache.php | 8 +++++++ templates/home.html.twig | 3 ++- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/public/assets/js/dashboard_map.js b/public/assets/js/dashboard_map.js index 81230953..b3e03735 100644 --- a/public/assets/js/dashboard_map.js +++ b/public/assets/js/dashboard_map.js @@ -88,6 +88,20 @@ class DashboardMap { ); } + removeCustomerMarker(id) { + console.log('removing customer marker for ' + id); + var layer_group = this.layer_groups.customer; + var markers = this.cust_markers; + + // no customer marker with that id + if (!markers.hasOwnProperty(id)) { + console.log('no such marker to remove'); + return; + } + + layer_group.removeLayer(markers[id]); + } + putRiderAvailableMarker(id, lat, lng) { this.putMarker( id, diff --git a/public/assets/js/map_mqtt.js b/public/assets/js/map_mqtt.js index 8af023df..7b6c9331 100644 --- a/public/assets/js/map_mqtt.js +++ b/public/assets/js/map_mqtt.js @@ -34,6 +34,7 @@ class MapEventHandler { // subscribe to jo locations console.log('subscribing to ' + my.options.channels.jo_location); my.mqtt.subscribe(my.options.channels.jo_location); + my.mqtt.subscribe(my.options.channels.jo_status); } onMessage(msg) { @@ -77,10 +78,10 @@ class MapEventHandler { handleJobOrder(chan_split, payload) { console.log("jo message"); + var id = chan_split[1]; switch (chan_split[2]) { case "location": - var my = this; - var id = chan_split[1]; + // var my = this; console.log("got location for jo " + id + " - " + payload); var pl_split = payload.split(':'); @@ -96,6 +97,14 @@ class MapEventHandler { this.dashmap.putCustomerMarker(id, lat, lng); break; + case "status": + switch (payload) { + case 'cancel': + case 'fulfill': + case 'delete': + this.dashmap.removeCustomerMarker(id); + break; + } } } } diff --git a/src/EventListener/JobOrderActiveCacheListener.php b/src/EventListener/JobOrderActiveCacheListener.php index 7b4c552a..541ce158 100644 --- a/src/EventListener/JobOrderActiveCacheListener.php +++ b/src/EventListener/JobOrderActiveCacheListener.php @@ -36,8 +36,10 @@ class JobOrderActiveCacheListener break; // inactive case JOStatus::CANCELLED: + $this->processInactiveJO($jo, 'cancel'); + break; case JOStatus::FULFILLED: - $this->processInactiveJO($jo); + $this->processInactiveJO($jo, 'fulfill'); break; } } @@ -59,8 +61,10 @@ class JobOrderActiveCacheListener break; // inactive case JOStatus::CANCELLED: + $this->processInactiveJO($jo, 'cancel'); + break; case JOStatus::FULFILLED: - $this->processInactiveJO($jo); + $this->processInactiveJO($jo, 'fulfill'); break; } } @@ -68,14 +72,17 @@ class JobOrderActiveCacheListener // when a job order is deleted public function postRemove(JobOrder $jo, LifecycleEventArgs $args) { + $this->processInactiveJO($jo, 'delete'); } protected function processActiveJO($jo) { // save in cache - $jo_cache->addActiveJobOrder($jo); + $this->jo_cache->addActiveJobOrder($jo); // publish to mqtt + $coords = $jo->getCoordinates(); + // TODO: do we put the key in config? $this->mqtt->publish( 'jo/' . $jo->getID() . '/location', @@ -83,11 +90,16 @@ class JobOrderActiveCacheListener ); } - protected function processInactiveJO($jo) + protected function processInactiveJO($jo, $status = 'cancel') { - // TODO: remove from redis cache + // remove from redis cache + $this->jo_cache->removeActiveJobOrder($jo); // TODO: publich to mqtt + $this->mqtt->publish( + 'jo/' . $jo->getID() . '/status', + $status + ); } } diff --git a/src/Service/JobOrderCache.php b/src/Service/JobOrderCache.php index 5e3a04f5..32d55e68 100644 --- a/src/Service/JobOrderCache.php +++ b/src/Service/JobOrderCache.php @@ -55,4 +55,12 @@ class JobOrderCache // error_log(print_r($all_jo, true)); return $jo_locs; } + + public function removeActiveJobOrder(JobOrder $jo) + { + $this->redis->zrem( + $this->active_jo_key, + $jo->getID() + ); + } } diff --git a/templates/home.html.twig b/templates/home.html.twig index 50f5a177..3bcb6c3b 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -45,7 +45,8 @@ function initEventHandler(dashmap) { var options = { 'channels': { 'rider_location': 'rider/+/location', - 'jo_location': 'jo/+/location' + 'jo_location': 'jo/+/location', + 'jo_status': 'jo/+/status' }, };