diff --git a/config/routes/rider.yaml b/config/routes/rider.yaml index 1934a1b1..5cc772ae 100644 --- a/config/routes/rider.yaml +++ b/config/routes/rider.yaml @@ -56,3 +56,8 @@ rider_priority_down_jo: path: /riders/{id}/priority_down/{jo_id} controller: App\Controller\RiderController::priorityDownJO methods: [GET] + +rider_ajax_available: + path: /riders/{id}/available + controller: App\Controller\RiderController::ajaxAvailable + methods: [GET] diff --git a/public/assets/js/map_mqtt.js b/public/assets/js/map_mqtt.js index 250e00c6..d277013c 100644 --- a/public/assets/js/map_mqtt.js +++ b/public/assets/js/map_mqtt.js @@ -36,6 +36,10 @@ class MapEventHandler { // subscribe to rider status console.log('subscribing to ' + my.options.channels.rider_status); my.mqtt.subscribe(my.options.channels.rider_status); + + // subscribe to rider availability + console.log('subscribing to ' + my.options.channels.rider_availability); + my.mqtt.subscribe(my.options.channels.rider_availability); } if (my.options.track_jo) { @@ -106,6 +110,43 @@ class MapEventHandler { break; } break; + case "availability": + console.log("got availability for rider " + chan_split[1] + " - " + payload); + var obj = JSON.parse(payload); + + var status = obj.status; + console.log("status " + status); + switch (status) { + case 'rider_offline': + this.dashmap.rider_availability[chan_split[1]] = false; + this.dashmap.removeRiderMarker(chan_split[1]); + break; + case 'rider_online': + this.dashmap.rider_availability[chan_split[1]] = true; + var lat = parseFloat(obj.latitude); + var lng = parseFloat(obj.longitude); + + // check if rider is available / unavailable + var dashmap = this.dashmap; + var url = dashmap.options.rider_availability_url; + var rider_availability_url = url.replace('[id]', chan_split[1]); + $.get(rider_availability_url).done(function(data) { + console.log('rider availability - ' + data); + switch (data) { + case 'available': + console.log('putting available marker ' + chan_split[1] + ' ' + lat + ':' + lng); + dashmap.switchRiderStatus(chan_split[1], 'available'); + dashmap.putRiderAvailableMarker(chan_split[1], lat, lng); + break; + case 'unavailable': + console.log('putting active jo marker ' + chan_split[1] + ' ' + lat + ':' + lng); + dashmap.switchRiderStatus(chan_split[1], 'jo'); + dashmap.putRiderActiveJOMarker(chan_split[1], lat, lng); + break; + } + }); + } + break; } } diff --git a/src/Controller/RiderController.php b/src/Controller/RiderController.php index 43f5d6d6..247123d5 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -596,4 +596,23 @@ class RiderController extends Controller return $this->redirecttoRoute('rider_update', ['id' => $rider->getID()]); } + + /** + * @ParamConverter("rider", class="App\Entity\Rider") + */ + public function ajaxAvailable(EntityManagerInterface $em, Rider $rider) + { + $jo = $rider->getRiderActiveJobOrder(); + if ($jo == null || $jo->isClosed()) + $avail = 'available'; + else + $avail = 'unavailable'; + + $response = new Response( + $avail, + Response::HTTP_OK, + ['content-type' => 'text/plain'] + ); + return $response; + } } diff --git a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php index 4a86ceba..ae6d2199 100644 --- a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php @@ -22,6 +22,7 @@ use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; use App\Service\RisingTideGateway; +use App\Service\RiderTracker; use App\Entity\RiderSession; use App\Entity\Rider; @@ -47,12 +48,14 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface protected $ic; protected $session; protected $rt; + protected $rider_tracker; public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic, RisingTideGateway $rt) + InvoiceGeneratorInterface $ic, RisingTideGateway $rt, + RiderTracker $rider_tracker) { $this->em = $em; $this->redis = $redis; @@ -64,6 +67,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->jo_handler = $jo_handler; $this->ic = $ic; $this->rt = $rt; + $this->rider_tracker = $rider_tracker; // one device = one session, since we have control over the devices // when a rider logs in, we just change the rider assigned to the device @@ -186,6 +190,20 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface // TODO: figure out longitude / latitude default $this->rcache->addActiveRider($rider_id, 0, 0); + // send mqtt event to put rider on map + // get rider coordinates from redis + $coord = $this->rider_tracker->getRiderLocation($rider->getID()); + + $lng = $coord->getLongitude(); + $lat = $coord->getLatitude();; + $channel = 'rider/' . $rider->getID() . '/availability'; + $payload = [ + 'status' => 'rider_online', + 'longitude' => $lng, + 'latitude' => $lat, + ]; + $this->mclient->publish($channel, json_encode($payload)); + // TODO: log rider logging in $this->em->flush(); @@ -243,6 +261,13 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->em->flush(); + // send mqtt event to remove rider from map + $channel = 'rider/' . $rider->getID() . '/availability'; + $payload = [ + 'status' => 'rider_offline' + ]; + $this->mclient->publish($channel, json_encode($payload)); + return $data; } diff --git a/templates/home.html.twig b/templates/home.html.twig index ebe8cff3..4a546931 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -35,7 +35,8 @@ function initMap(r_markers, c_markers, icons) { 'zoom': 13, 'rider_popup_url': '/riders/[id]/popup', 'cust_popup_url': '/job-order/[id]/popup', - 'icons': icons + 'icons': icons, + 'rider_availability_url': '{{ absolute_url('/riders/[id]/available')|raw }}' }; var dashmap = new DashboardMap(options, r_markers, c_markers); @@ -54,7 +55,8 @@ function initEventHandler(dashmap, icons, ssl) { 'rider_status': 'rider/+/status', 'jo_location': 'jo/+/location', 'jo_status': 'jo/+/status', - 'jo_origin': 'jo/+/origin' + 'jo_origin': 'jo/+/origin', + 'rider_availability': 'rider/+/availability', }, };