Add login and logout. #617
This commit is contained in:
parent
446065880c
commit
722ec653c5
1 changed files with 138 additions and 5 deletions
|
|
@ -4,8 +4,11 @@ namespace App\Controller\CAPI;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
use Catalyst\APIBundle\Controller\APIController;
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
use Catalyst\APIBundle\Response\APIResponse;
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
|
||||||
|
|
@ -16,6 +19,7 @@ use App\Entity\Battery;
|
||||||
use App\Entity\BatteryModel;
|
use App\Entity\BatteryModel;
|
||||||
use App\Entity\BatterySize;
|
use App\Entity\BatterySize;
|
||||||
use App\Entity\RiderAPISession;
|
use App\Entity\RiderAPISession;
|
||||||
|
use App\Entity\User;
|
||||||
|
|
||||||
use App\Service\RedisClientProvider;
|
use App\Service\RedisClientProvider;
|
||||||
use App\Service\RiderCache;
|
use App\Service\RiderCache;
|
||||||
|
|
@ -47,10 +51,10 @@ class RiderController extends APIController
|
||||||
// get capi user to link to rider api user
|
// get capi user to link to rider api user
|
||||||
$capi_user_id = $this->getUser()->getID();
|
$capi_user_id = $this->getUser()->getID();
|
||||||
|
|
||||||
// check if capi user already has a rider api user
|
// check if capi user already has a rider api session
|
||||||
$rider_api_user = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||||
|
|
||||||
if ($rider_api_user != null)
|
if ($rapi_session != null)
|
||||||
return new APIResponse(false, 'User already registered');
|
return new APIResponse(false, 'User already registered');
|
||||||
|
|
||||||
// retry until we get a unique id
|
// retry until we get a unique id
|
||||||
|
|
@ -102,12 +106,141 @@ class RiderController extends APIController
|
||||||
return new APIResponse(true, 'Rider API user created.', $data);
|
return new APIResponse(true, 'Rider API user created.', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function login(Request $req)
|
public function login(Request $req, EntityManagerInterface $em, EncoderFactoryInterface $ef,
|
||||||
|
RiderCache $rcache, RiderTracker $rider_tracker, MQTTClient $mclient,
|
||||||
|
RedisClientProvider $redis)
|
||||||
{
|
{
|
||||||
|
$required_params = [
|
||||||
|
'user',
|
||||||
|
'pass',
|
||||||
|
];
|
||||||
|
|
||||||
|
$missing = $this->checkMissingParameters($req, $required_params);
|
||||||
|
if (count($missing) > 0)
|
||||||
|
{
|
||||||
|
$params = implode(', ', $missing);
|
||||||
|
return new APIResponse(false, 'Missing parameter(s): ' . $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logout(Request $req)
|
// get capi user to link to rider api user
|
||||||
|
$capi_user_id = $this->getUser()->getID();
|
||||||
|
|
||||||
|
// check if capi user already has a rider api user
|
||||||
|
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||||
|
|
||||||
|
if ($rapi_session->hasRider())
|
||||||
|
return new APIResponse(false, 'Another rider is already logged in. Please logout first.');
|
||||||
|
|
||||||
|
// look for rider with username
|
||||||
|
$rider = $em->getRepository(Rider::class)->findOneBy(['username' => $req->request->get('user')]);
|
||||||
|
if ($rider == null)
|
||||||
|
return new APIResponse(false, 'Invalid username or password.');
|
||||||
|
|
||||||
|
// check if rider password is correct
|
||||||
|
$encoder = $ef->getEncoder(new User());
|
||||||
|
if (!$encoder->isPasswordValid($rider->getPassword(), $req->request->get('pass'), ''))
|
||||||
|
return new APIResponse(false, 'Invalid username or password.');
|
||||||
|
|
||||||
|
// assign rider to api session
|
||||||
|
$rapi_session->setRider($rider);
|
||||||
|
|
||||||
|
// set rider to available
|
||||||
|
$rider->setAvailable(true);
|
||||||
|
|
||||||
|
$rider_id = $rider->getID();
|
||||||
|
// cache rider location (default to hub)
|
||||||
|
// TODO: figure out longitude / latitude default
|
||||||
|
$rcache->addActiveRider($rider_id, 0, 0);
|
||||||
|
|
||||||
|
// send mqtt event to put rider on map
|
||||||
|
// get rider coordinates from redis
|
||||||
|
$coord = $rider_tracker->getRiderLocation($rider->getID());
|
||||||
|
|
||||||
|
$lng = $coord->getLongitude();
|
||||||
|
$lat = $coord->getLatitude();;
|
||||||
|
$channel = 'rider/' . $rider->getID() . '/availability';
|
||||||
|
$payload = [
|
||||||
|
'status' => 'rider_online',
|
||||||
|
'longitude' => $lng,
|
||||||
|
'latitude' => $lat,
|
||||||
|
];
|
||||||
|
$mclient->publish($channel, json_encode($payload));
|
||||||
|
|
||||||
|
// TODO: log rider logging in
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// update redis rider.id.<session id> with the rider id
|
||||||
|
$redis_client = $redis->getRedisClient();
|
||||||
|
$redis_key = 'rider.id.' . $rapi_session->getID();
|
||||||
|
$rider_id = $rider->getID();
|
||||||
|
|
||||||
|
$redis_client->set($redis_key, $rider_id);
|
||||||
|
|
||||||
|
$hub = $rider->getHub();
|
||||||
|
if ($hub == null)
|
||||||
|
$hub_data = null;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
|
$coord = $hub->getCoordinates();
|
||||||
|
$hub_data = [
|
||||||
|
'id' => $hub->getID(),
|
||||||
|
'name' => $hub->getName(),
|
||||||
|
'branch' => $hub->getBranch(),
|
||||||
|
'longitude' => $coord->getLongitude(),
|
||||||
|
'latitude' => $coord->getLatitude(),
|
||||||
|
'contact_nums' => $hub->getContactNumbers(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// data
|
||||||
|
$data = [
|
||||||
|
'hub' => $hub_data,
|
||||||
|
'rider_id' => $rider_id,
|
||||||
|
];
|
||||||
|
|
||||||
|
return new APIResponse(true, 'Rider logged in.', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logout(Request $req, EntityManagerInterface $em, RiderCache $rcache, MQTTClient $mclient)
|
||||||
|
{
|
||||||
|
$required_params = [];
|
||||||
|
$missing = $this->checkMissingParameters($req, $required_params);
|
||||||
|
if (count($missing) > 0)
|
||||||
|
{
|
||||||
|
$params = implode(', ', $missing);
|
||||||
|
return new APIResponse(false, 'Missing parameter(s): ' . $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get capi user to link to rider api user
|
||||||
|
$capi_user_id = $this->getUser()->getID();
|
||||||
|
|
||||||
|
// check if capi user already has a rider api user
|
||||||
|
$rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]);
|
||||||
|
|
||||||
|
// make rider unavailable
|
||||||
|
$rider = $rapi_session->getRider();
|
||||||
|
$rider->setAvailable(false);
|
||||||
|
|
||||||
|
// remove from cache
|
||||||
|
$rcache->removeActiveRider($rider->getID());
|
||||||
|
|
||||||
|
// remove rider from session
|
||||||
|
$rapi_session->setRider(null);
|
||||||
|
|
||||||
|
// TODO: log rider logging out
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// send mqtt event to remove rider from map
|
||||||
|
$channel = 'rider/' . $rider->getID() . '/availability';
|
||||||
|
$payload = [
|
||||||
|
'status' => 'rider_offline'
|
||||||
|
];
|
||||||
|
$mclient->publish($channel, json_encode($payload));
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
return new APIResponse(true, 'Rider logged out', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getJobOrder(Request $req)
|
public function getJobOrder(Request $req)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue