Add sendRiderEvent call for MQTTClient service #151

This commit is contained in:
Kendrick Chan 2018-07-02 00:22:15 +08:00
parent 128dd352d9
commit cbdc2fd24e
4 changed files with 39 additions and 0 deletions

View file

@ -23,6 +23,7 @@ use App\Ramcar\TradeInType;
use App\Service\InvoiceCreator;
use App\Service\RisingTideGateway;
use App\Service\MQTTClient;
use App\Entity\MobileSession;
use App\Entity\Customer;

View file

@ -1168,6 +1168,7 @@ class JobOrderController extends BaseController
'event' => 'driver_assigned'
];
$mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload);
// return successful response
return $this->json([
@ -1361,6 +1362,7 @@ class JobOrderController extends BaseController
'driver_id' => $rider->getID(),
];
$mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload);
// return successful response
return $this->json([
@ -2135,6 +2137,7 @@ class JobOrderController extends BaseController
'jo_id' => $obj->getID(),
];
$mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload);
// return successful response
return $this->json([

View file

@ -106,10 +106,17 @@ class Rider
*/
protected $password;
// rider sessions
/**
* @ORM\OneToMany(targetEntity="RiderSession", mappedBy="rider")
*/
protected $sessions;
public function __construct()
{
$this->job_orders = new ArrayCollection();
$this->schedules = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->curr_rating = 0;
$this->flag_available = true;
$this->flag_active = true;
@ -307,4 +314,9 @@ class Rider
return $this->job_orders->matching($criteria)[0];
}
public function getSessions()
{
return $this->sessions;
}
}

View file

@ -8,6 +8,7 @@ use App\Entity\JobOrder;
class MQTTClient
{
const PREFIX = 'motolite.control.';
const RIDER_PREFIX = 'motorider.';
protected $mclient;
@ -36,6 +37,7 @@ class MQTTClient
if (count($sessions) == 0)
return;
// send to every customer session
foreach ($sessions as $sess)
{
$phone_num = $sess->getPhoneNumber();
@ -43,4 +45,25 @@ class MQTTClient
$this->publish($channel, json_encode($payload));
}
}
public function sendRiderEvent(JobOrder $job_order, $payload)
{
// check if a rider is available
$rider = $job_order->getRider();
if ($rider == null)
return;
// check if rider has sessions
$sessions = $rider->getSessions();
if (count($sessions) == 0)
return;
// send to every rider session
foreach ($sessions as $sess)
{
$sess_id = $sess->getID();
$channel = self::RIDER_PREFIX . $sess_id;
$this->publish($channel, json_encode($payload));
}
}
}