131 lines
3.7 KiB
PHP
131 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Mosquitto\Client as MosquittoClient;
|
|
use App\Entity\JobOrder;
|
|
|
|
class MQTTClient
|
|
{
|
|
const PREFIX = 'motolite.control.';
|
|
const PREFIX_APIv2 = 'motolite.control.v2.';
|
|
const RIDER_PREFIX = 'motorider_';
|
|
|
|
// protected $mclient;
|
|
protected $redis;
|
|
protected $key;
|
|
|
|
public function __construct(RedisClientProvider $redis_client, $key)
|
|
{
|
|
$this->redis = $redis_client->getRedisClient();
|
|
$this->key = $key;
|
|
}
|
|
|
|
public function __destruct()
|
|
{
|
|
// $this->mclient->disconnect();
|
|
}
|
|
|
|
public function publish($channel, $message)
|
|
{
|
|
// $this->mclient->publish($channel, $message);
|
|
|
|
$data = $channel . '|' . $message;
|
|
$this->redis->lpush($this->key, $data);
|
|
}
|
|
|
|
public function sendEvent(JobOrder $job_order, $payload)
|
|
{
|
|
error_log("CUSTOMER ID: " . $job_order->getCustomer()->getID());
|
|
//error_log('sending mqtt event: ');
|
|
//error_log(print_r($payload, true));
|
|
|
|
// append jo id to payload
|
|
$payload['jo_id'] = $job_order->getID();
|
|
|
|
// get all new and legacy mobile sessions
|
|
$legacy_sessions = $job_order->getCustomer()->getMobileSessions();
|
|
|
|
$new_sessions = [];
|
|
$cust_user = $job_order->getCustomer()->getCustomerUser();
|
|
if (!empty($cust_user)) {
|
|
error_log("CUSTOMER USER ID: " . $cust_user->getID());
|
|
$new_sessions = $cust_user->getMobileSessions();
|
|
}
|
|
|
|
// TODO: make this more elegant. looping through each instead of merging the two because doctrine returns PersistentCollection if empty, and array if not
|
|
$sessions = [];
|
|
foreach ($legacy_sessions as $sess) {
|
|
error_log("FOUND LEGACY SESSION: " . $sess->getID());
|
|
$sessions[] = [
|
|
'data' => $sess,
|
|
'legacy' => true,
|
|
];
|
|
}
|
|
|
|
foreach ($new_sessions as $sess) {
|
|
error_log("FOUND NEW SESSION: " . $sess->getID());
|
|
$sessions[] = [
|
|
'data' => $sess,
|
|
'legacy' => false,
|
|
];
|
|
}
|
|
|
|
if (empty($sessions))
|
|
{
|
|
error_log("no sessions to send mqtt event to");
|
|
return;
|
|
}
|
|
|
|
$channels = [];
|
|
|
|
// send to every customer session
|
|
foreach ($sessions as $sess)
|
|
{
|
|
$phone_num = $sess['data']->getPhoneNumber();
|
|
|
|
// use appropriate prefix so we have different channel names for new api
|
|
$prefix = $sess->legacy ? self::PREFIX : self::PREFIX_APIv2;
|
|
$channel = $prefix . $phone_num;
|
|
|
|
error_log("SENDING TO CHANNEL: " . $channel);
|
|
|
|
// gather channels, so we only send once
|
|
$channels[$channel] = json_encode($payload);
|
|
}
|
|
|
|
foreach ($channels as $channel => $json_payload)
|
|
{
|
|
$this->publish($channel, $json_payload);
|
|
|
|
// error_log('sent to ' . $channel);
|
|
}
|
|
}
|
|
|
|
public function sendRiderEvent(JobOrder $job_order, $payload)
|
|
{
|
|
// check if a rider is available
|
|
$rider = $job_order->getRider();
|
|
if ($rider == null)
|
|
return;
|
|
|
|
/*
|
|
// NOTE: this is for the old rider app
|
|
// 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));
|
|
}
|
|
*/
|
|
|
|
// NOTE: this is for the new rider app
|
|
$this->publish('rider/' . $rider->getID(), json_encode($payload));
|
|
}
|
|
}
|