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; } }