From 00091ccc57570dea004430b969461100b5ec04e1 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 1 Oct 2020 21:56:27 +0800 Subject: [PATCH 1/5] Change mqtt session to persistent #514 --- public/assets/js/map_mqtt.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/public/assets/js/map_mqtt.js b/public/assets/js/map_mqtt.js index 8a38e8ff..30f646cd 100644 --- a/public/assets/js/map_mqtt.js +++ b/public/assets/js/map_mqtt.js @@ -3,6 +3,7 @@ class MapEventHandler { this.options = options; this.dashmap = dashmap; this.ssl = ssl; + this.subscribed = false; } connect(user_id, host, port) { @@ -21,7 +22,8 @@ class MapEventHandler { keepAliveInterval: 10, invocationContext: this, onSuccess: this.onConnect.bind(this), - reconnect: true + reconnect: true, + cleanSession: false }; this.mqtt.onMessageArrived = this.onMessage.bind(this); @@ -40,6 +42,11 @@ class MapEventHandler { console.log('mqtt connected!'); var my = icontext.invocationContext; + if (my.subscribed) { + console.log('already subscribed, not initializing subscriptions'); + return; + } + if (my.options.track_rider) { // subscribe to rider locations console.log('subscribing to ' + my.options.channels.rider_location); @@ -68,6 +75,7 @@ class MapEventHandler { my.mqtt.subscribe(my.options.channels.jo_origin); } + my.subscribed = true; } onMessage(msg) { From 3d96299c23b017eee663e6275ec794ef21ce5031 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 1 Oct 2020 22:31:48 +0800 Subject: [PATCH 2/5] Initialize rider availability in dashboard map #514 --- public/assets/js/dashboard_map.js | 7 +++++-- public/assets/js/map_mqtt.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/public/assets/js/dashboard_map.js b/public/assets/js/dashboard_map.js index 2df2d69f..bc5696e2 100644 --- a/public/assets/js/dashboard_map.js +++ b/public/assets/js/dashboard_map.js @@ -313,10 +313,13 @@ class DashboardMap { var lng = data.longitude; var name = ''; - if (data.has_jo) + if (data.has_jo) { + my.rider_availability[id] = false; my.putRiderActiveJOMarker(id, lat, lng); - else + } else { + my.rider_availability[id] = true; my.putRiderAvailableMarker(id, lat, lng); + } }); // console.log(rider_markers); diff --git a/public/assets/js/map_mqtt.js b/public/assets/js/map_mqtt.js index 30f646cd..f0aab74f 100644 --- a/public/assets/js/map_mqtt.js +++ b/public/assets/js/map_mqtt.js @@ -161,7 +161,7 @@ class MapEventHandler { display_marker = false; } } else { - console.log('rider not in availability check'); + console.log('rider not in availability check - ' + chan_split[1]); display_marker = false; } From e53bb8f69bd4944235109def4062bb1736aab8bb Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 1 Oct 2020 15:09:43 +0000 Subject: [PATCH 3/5] Revert "Merge branch '512-cmb-warranty-expiration-computation' into '488-cmb-live'" This reverts merge request !594 --- config/services.yaml | 6 ----- src/Ramcar/CMBWarrantyClass.php | 6 ++--- .../JobOrderHandler/CMBJobOrderHandler.php | 4 +-- .../RiderAPIHandler/CMBRiderAPIHandler.php | 4 +-- src/Service/WarrantyHandlerInterface.php | 27 ------------------- 5 files changed, 6 insertions(+), 41 deletions(-) delete mode 100644 src/Service/WarrantyHandlerInterface.php diff --git a/config/services.yaml b/config/services.yaml index 07354a8a..7d585e08 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -196,12 +196,6 @@ services: $upload_dir: "%jo_extra_upload_directory%" App\Service\RiderAPIHandlerInterface: "@App\\Service\\RiderAPIHandler\\CMBRiderAPIHandler" - - # warranty handler service - App\Service\WarrantyHandler\CMBWarrantyHandler: ~ - - # warranty handler interface - App\Service\WarrantyHandlerInterface: "@App\\Service\\WarrantyHandler\\CMBWarrantyHandler" # map manager #App\Service\GISManager\Bing: ~ diff --git a/src/Ramcar/CMBWarrantyClass.php b/src/Ramcar/CMBWarrantyClass.php index b3fbfc5c..1e2c69b4 100644 --- a/src/Ramcar/CMBWarrantyClass.php +++ b/src/Ramcar/CMBWarrantyClass.php @@ -4,14 +4,12 @@ namespace App\Ramcar; class CMBWarrantyClass extends NameValue { - const WTY_PASSENGER = 'passenger'; - const WTY_COMMERCIAL = 'commercial'; - const WTY_GRAB = 'grab'; + const WTY_PASSENGER = 'passenger'; + const WTY_COMMERCIAL = 'commercial'; const COLLECTION = [ 'passenger' => 'Passenger', 'commercial' => 'Commercial', - 'grab' => 'Grab', ]; } diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 4760d286..05d9ce45 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -44,7 +44,7 @@ use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; use App\Service\CustomerHandlerInterface; -use App\Service\WarrantyHandlerInterface; +use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -75,7 +75,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandlerInterface $wh, + string $country_code, WarrantyHandler $wh, CustomerHandlerInterface $cust_handler) { $this->em = $em; diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 6fc80932..72c6f949 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -20,7 +20,7 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandlerInterface; +use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; use App\Service\RiderTracker; @@ -61,7 +61,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandlerInterface $wh, JobOrderHandlerInterface $jo_handler, + WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic, string $upload_dir, RiderTracker $rider_tracker, NotificationManager $nm, UrlGeneratorInterface $router) diff --git a/src/Service/WarrantyHandlerInterface.php b/src/Service/WarrantyHandlerInterface.php deleted file mode 100644 index 13950ed5..00000000 --- a/src/Service/WarrantyHandlerInterface.php +++ /dev/null @@ -1,27 +0,0 @@ - Date: Fri, 2 Oct 2020 02:22:00 +0000 Subject: [PATCH 4/5] Add WarrantyHandler directory. #512 --- .../WarrantyHandler/CMBWarrantyHandler.php | 307 ++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 src/Service/WarrantyHandler/CMBWarrantyHandler.php diff --git a/src/Service/WarrantyHandler/CMBWarrantyHandler.php b/src/Service/WarrantyHandler/CMBWarrantyHandler.php new file mode 100644 index 00000000..0f9f84b6 --- /dev/null +++ b/src/Service/WarrantyHandler/CMBWarrantyHandler.php @@ -0,0 +1,307 @@ +em = $em; + } + + public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, + $batt_list, DateTime $date_purchase, $warranty_class) + { + // new warranty + $warranty = new Warranty(); + + foreach ($batt_list as $battery) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + } + + // compute expiry date + $date_expire = null; + if ((!empty($warranty_class)) && + (count($batt_list) != 0)) + { + $period = $this->getWarrantyPeriod($batt_list, $warranty_class); + $date_expire = $this->computeDateExpire($date_purchase, $period); + + $warranty->setDateExpire($date_expire); + } + + // set and save values + if (trim($serial) == '') + $warranty->setSerial(null); + else + $warranty->setSerial($serial); + $warranty->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase) + ->setWarrantyClass($warranty_class); + + $this->em->persist($warranty); + $this->em->flush(); + + // update customer vehicle with warranty info + $this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire); + + $this->em->clear(); + } + + public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire) + { + // find customer vehicle using plate number + // error_log('Finding customer vehicle with plate number ' . $plate_number); + $cv_q = $this->em->createQuery('select count(cv) from App\Entity\CustomerVehicle cv where cv.plate_number = :plate_number') + ->setParameter('plate_number', $plate_number); + $cv_result = $cv_q->getSingleScalarResult(); + + $battery_id = null; + if ($cv_result != 0) + { + if (!empty($batteries)) + { + // set current battery to the first battery in list. + // there are cases where multiple batteries linked to an SAP code. + $battery = $batteries[0]; + $battery_id = $battery->getID(); + } + //error_log('Serial/Warranty Code = ' . $serial); + $q = $this->em->createQuery('update App\Entity\CustomerVehicle cv + set cv.curr_battery = :batt_id, + cv.warranty_code = :serial, + cv.warranty_expiration = :expiry_date + where cv.plate_number = :plate_number') + ->setParameters([ + 'batt_id' => $battery_id, + 'serial' => $serial, + 'expiry_date' => $date_expire, + 'plate_number' => $plate_number]); + $q->execute(); + } + } + + public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) + { + // TODO: add serial and plate number to update + // TODO: check if data from existing warranty matches the new data + // check if details are complete + if (empty($warr->getFirstName())) + { + if (!empty($first_name)) + { + $warr->setFirstName($first_name); + } + } + if (empty($warr->getLastName())) + { + if (!empty($last_name)) + { + $warr->setLastName($last_name); + } + } + if (empty($warr->getMobileNumber())) + { + if (!empty($mobile_number)) + { + $warr->setMobileNumber($mobile_number); + } + } + if ((empty($warr->getBatteryModel())) || + (empty($warr->getBatterySize()))) + { + if (count($batt_list) != 0) + { + foreach ($batt_list as $battery) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + } + } + } + + $purchase_date = $warr->getDatePurchase(); + if (empty($purchase_date)) + { + if (!empty($date_purchase)) + { + $warr->setDatePurchase($date_purchase); + } + $purchase_date = $date_purchase; + } + + if (empty($warr->getDateExpire())) + { + $batteries = []; + if (count($batt_list) == 0) + { + $batteries = $this->getBatteriesForWarranty($warr); + } + else + { + $batteries = $batt_list; + } + + if (!empty($batteries)) + { + $period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass()); + if (!empty($purchase_date)) + { + $expire_date = $this->computeDateExpire($purchase_date, $period); + $warr->setDateExpire($expire_date); + } + } + } + + $this->em->persist($warr); + $this->em->flush(); + $this->em->clear(); + } + + public function computeDateExpire($date_create, $warranty_period) + { + $expire_date = clone $date_create; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } + + public function getWarrantyPeriod($batteries, $warranty_class) + { + // set to -1 to show that we haven't set a warranty period yet + // cannot set initial value to 0 because warranty tnv can be 0 + $least_warranty = -1; + $warr_period = 0; + foreach ($batteries as $battery) + { + // if multiple batteries, get the smallest warranty period + // check warranty class to get warranty period + if ($warranty_class == CMBWarrantyClass::WTY_PASSENGER) + { + $warr_period = $battery->getWarrantyPrivate(); + //error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == CMBWarrantyClass::WTY_COMMERCIAL) + { + $warr_period = $battery->getWarrantyCommercial(); + //error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == CMBWarrantyClass::WTY_GRAB) + { + $warr_period = $battery->getWarrantyTnv(); + //error_log('Warranty Period for TNV: ' . $warr_period); + } + + if ($least_warranty < 0) + { + // set least warranty to the first obtained warranty period + $least_warranty = $warr_period; + } + + if ($least_warranty > $warr_period) + { + $least_warranty = $warr_period; + } + } + + $warranty_period = $least_warranty; + + return $warranty_period; + } + + public function getBatteriesForWarranty($warr) + { + // find battery via sku/sap battery first + // if sku is null, use battery model and battery size to find battery + // if all three are null, do nothing + $batteries = null; + + $batt_model = $warr->getBatteryModel(); + $batt_size = $warr->getBatterySize(); + $warranty_class = $warr->getWarrantyClass(); + + if (empty($warranty_class)) + { + //error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + if ($batt_model == null) + { + //error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + //error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + + // find battery using battery model and battery size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + + if (empty($batteries)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); + return null; + } + + return $batteries; + } + + + public function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + +} From b212fa99bc74e08df8ba9d762e781e21654a0e0d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 2 Oct 2020 03:15:07 +0000 Subject: [PATCH 5/5] Recommit warranty computation changes. #512 --- config/cmb.services.yaml | 12 +++++++++ config/services.yaml | 6 +++++ src/Ramcar/CMBWarrantyClass.php | 6 +++-- .../JobOrderHandler/CMBJobOrderHandler.php | 4 +-- .../RiderAPIHandler/CMBRiderAPIHandler.php | 4 +-- src/Service/WarrantyHandlerInterface.php | 27 +++++++++++++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/Service/WarrantyHandlerInterface.php diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index 3d9fd7a3..07354a8a 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -196,6 +196,12 @@ services: $upload_dir: "%jo_extra_upload_directory%" App\Service\RiderAPIHandlerInterface: "@App\\Service\\RiderAPIHandler\\CMBRiderAPIHandler" + + # warranty handler service + App\Service\WarrantyHandler\CMBWarrantyHandler: ~ + + # warranty handler interface + App\Service\WarrantyHandlerInterface: "@App\\Service\\WarrantyHandler\\CMBWarrantyHandler" # map manager #App\Service\GISManager\Bing: ~ @@ -221,6 +227,12 @@ services: event: 'postPersist' entity: 'App\Entity\JobOrder' + App\Service\NotificationManager: + arguments: + $redis_prov: "@App\\Service\\RedisClientProvider" + $redis_mqtt_key: "mqtt_events" + $em: "@doctrine.orm.entity_manager" + App\Service\JobOrderCache: arguments: $redis_prov: "@App\\Service\\RedisClientProvider" diff --git a/config/services.yaml b/config/services.yaml index 7d585e08..07354a8a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -196,6 +196,12 @@ services: $upload_dir: "%jo_extra_upload_directory%" App\Service\RiderAPIHandlerInterface: "@App\\Service\\RiderAPIHandler\\CMBRiderAPIHandler" + + # warranty handler service + App\Service\WarrantyHandler\CMBWarrantyHandler: ~ + + # warranty handler interface + App\Service\WarrantyHandlerInterface: "@App\\Service\\WarrantyHandler\\CMBWarrantyHandler" # map manager #App\Service\GISManager\Bing: ~ diff --git a/src/Ramcar/CMBWarrantyClass.php b/src/Ramcar/CMBWarrantyClass.php index 1e2c69b4..c7544719 100644 --- a/src/Ramcar/CMBWarrantyClass.php +++ b/src/Ramcar/CMBWarrantyClass.php @@ -4,12 +4,14 @@ namespace App\Ramcar; class CMBWarrantyClass extends NameValue { - const WTY_PASSENGER = 'passenger'; - const WTY_COMMERCIAL = 'commercial'; + const WTY_PASSENGER = 'passenger'; + const WTY_COMMERCIAL = 'commercial'; + const WTY_GRAB = 'grab'; const COLLECTION = [ 'passenger' => 'Passenger', 'commercial' => 'Commercial', + 'grab' => 'Grab', ]; } diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 05d9ce45..4760d286 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -44,7 +44,7 @@ use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; use App\Service\CustomerHandlerInterface; -use App\Service\WarrantyHandler; +use App\Service\WarrantyHandlerInterface; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -75,7 +75,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh, + string $country_code, WarrantyHandlerInterface $wh, CustomerHandlerInterface $cust_handler) { $this->em = $em; diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 72c6f949..6fc80932 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -20,7 +20,7 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandler; +use App\Service\WarrantyHandlerInterface; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; use App\Service\RiderTracker; @@ -61,7 +61,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, + WarrantyHandlerInterface $wh, JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic, string $upload_dir, RiderTracker $rider_tracker, NotificationManager $nm, UrlGeneratorInterface $router) diff --git a/src/Service/WarrantyHandlerInterface.php b/src/Service/WarrantyHandlerInterface.php new file mode 100644 index 00000000..13950ed5 --- /dev/null +++ b/src/Service/WarrantyHandlerInterface.php @@ -0,0 +1,27 @@ +