123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace DeviceBundle\EventListener;
- use Doctrine\ORM\Event\LifecycleEventArgs;
- use DeviceBundle\Interfaces\DeviceInterface;
- use WebserviceBundle\Services\Webservice;
- use Buzz\Message\RequestInterface as HttpRequestInterface;
- use Symfony\Bundle\FrameworkBundle\Console\Application;
- use Symfony\Component\Console\Input\ArgvInput;
- use Symfony\Component\Console\Output\BufferedOutput;
- use Base\AdminBundle\Interfaces\SoftDeleteInterface;
- class DeviceListener
- {
- /**
- * @var Webservice
- */
- private $webservice;
- /**
- * @var string
- */
- private $devicePostUrl;
- /**
- * @var string
- */
- private $deviceDeletePostUrl;
- /**
- * @var string
- */
- private $devicePutUrl;
- private $serviceContainer;
- /**
- * @param Webservice $webservice
- * @param string $devicePostUrl
- * @param string $deviceDeletePostUrl
- * @param string $devicePutUrl
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl, $serviceContainer)
- {
- $this->webservice = $webservice;
- $this->devicePostUrl = $devicePostUrl;
- $this->deviceDeletePostUrl = $deviceDeletePostUrl;
- $this->devicePutUrl = $devicePutUrl;
- $this->enabled = true;
- $this->serviceContainer = $serviceContainer;
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postPersist(LifecycleEventArgs $args)
- {
- if (!$this->enabled) {
- return;
- }
- $entity = $args->getEntity();
- if($entity instanceof SoftDeleteInterface && $entity->isDeleted()) {
- return;
- }
- if ($entity instanceof DeviceInterface) {
- $cmd_args = array(
- '--type:' . get_class($entity),
- '--id:' . $entity->getId(),
- );
- return $this->runCommand('device:crud', $cmd_args);
- }
- }
- /**
- * Corro el comando para crear el device por AMQP
- * @global kernel $kernel
- *
- * @param string $name
- * @param array $args
- *
- * @return string
- */
- public function runCommand($name, $cmd_args = array())
- {
- $kernel = $this->serviceContainer->get('kernel');
- $application = new Application($kernel);
- $application->setAutoExit(false);
- $args = [
- '',
- 'amqp:remote',
- $name,
- '--route=' . getenv("AMQP_KEY"),
- ];
- foreach ($cmd_args as $cmd_arg) {
- $args[] = "--args={$cmd_arg}";
- }
- $input = new ArgvInput($args);
- $output = new BufferedOutput();
- $application->run($input, $output);
- return $output->fetch();
- }
- /**
- * @param LifecycleEventArgs $args
- *
- * @return mixed
- */
- public function preRemove(LifecycleEventArgs $args)
- {
- if (!$this->enabled) {
- return;
- }
- $entity = $args->getEntity();
- if ($entity instanceof DeviceInterface) {
- if ($deviceId = $this->getRemoteDeviceId($entity)) {
- $cmd_args = array(
- '--type:' . get_class($entity),
- '--id:' . $entity->getId(),
- '--url:' . $this->deviceDeletePostUrl . $deviceId,
- '--method:' . HttpRequestInterface::METHOD_DELETE,
- );
- return $this->runCommand('device:crud', $cmd_args);
- }
- }
- return;
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postUpdate(LifecycleEventArgs $args)
- {
- if (!$this->enabled) {
- return;
- }
- $entity = $args->getEntity();
- if($entity instanceof SoftDeleteInterface && $entity->isDeleted()) {
- return;
- }
- if ($entity instanceof DeviceInterface) {
- $cmd_args = array(
- '--type:' . get_class($entity),
- '--id:' . $entity->getId(),
- );
- if ($deviceId = $this->getRemoteDeviceId($entity)) {
- $cmd_args[] = "--url:{$this->devicePutUrl}{$deviceId}";
- $cmd_args[] = '--method:' . HttpRequestInterface::METHOD_PUT;
- }
- return $this->runCommand('device:crud', $cmd_args);
- }
- return;
- }
- /**
- * @param DeviceInterface $entity
- * @param string $url
- * @param string $method
- * @param array $credentials username y password
- *
- * @return mixed
- */
- public function send($entity, $url, $method, $credentials = array())
- {
- if (!$this->enabled) {
- return;
- }
- if ($entity instanceof DeviceInterface) {
- $data = $entity->getDeviceData();
- $data = $this->addLocationData($entity, $data);
- $data = $this->addExtraDataFields($entity, $data);
- return $this->webservice->makeGetRequest($url, $method, $data, $credentials);
- }
- }
- /**
- * Agrega la ubicación de $entity si implementa LocationInterface
- *
- * @param Entity $entity
- * @param array $data
- *
- * @return array
- */
- private function addLocationData($entity, $data)
- {
- if (!$this->enabled) {
- return;
- }
- $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
- if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
- $extraData = json_decode($data['extraData'], true);
- $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
- $data['extraData'] = json_encode($extraData);
- }
- return $data;
- }
- private function addExtraDataFields($entity, $data)
- {
- if(!$this->enabled){
- return;
- }
- $extraData = json_decode($data['extraData'], true);
- $className = get_class($entity);
- if($className == 'FTTHBundle\Entity\OLT' && !array_key_exists('timeOltScan', $extraData)){
- $extraData['timeOltScan'] = 10;
- }else if($className == 'FTTHBundle\Entity\NAS' && !array_key_exists('executeSnmp', $extraData)){
- $extraData['executeSnmp'] = true;
- }
- $data['extraData'] = json_encode($extraData);
- return $data;
- }
- /**
- * @param object $entity
- *
- * @return mixed
- */
- private function getRemoteDeviceId($entity)
- {
- if (!$this->enabled) {
- return;
- }
- $deviceId = $entity->getId();
- $deviceType = get_class($entity);
- $tenancyId = $entity->getTenancyId();
- $filters = array(
- 'deviceId' => $deviceId,
- 'deviceType' => $deviceType,
- 'tenancyId' => $tenancyId
- );
- $data = $this->webservice->getData("api_device_post_url", $filters);
- $deviceId = null;
- if (isset($data[0])) {
- $deviceId = $data[0]['id'];
- }
- return $deviceId;
- }
- /**
- * @param boolean $enabled
- */
- function remoteCheck($enabled = true)
- {
- $this->enabled = $enabled;
- }
- /**
- * Se utiliza para las pruebas de phpunit
- * @param Webservice $webservice
- */
- public function setWebservice($webservice)
- {
- $this->webservice = $webservice;
- }
- }
|