Преглед изворни кода

FD3-223 Se renombro el comando para crear devices. Se edita o elimina un device por AMQP

Guillermo Espinoza пре 7 година
родитељ
комит
46120d9bc8
2 измењених фајлова са 45 додато и 20 уклоњено
  1. 21 11
      Command/CreateDeviceCommand.php
  2. 24 9
      EventListener/DeviceListener.php

+ 21 - 11
Command/CreateDeviceCommand.php

@@ -8,18 +8,19 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Buzz\Message\RequestInterface as HttpRequestInterface;
 
-class CreateDeviceCommand extends ContainerAwareCommand
+class CRUDDeviceCommand extends ContainerAwareCommand
 {
 
     protected function configure()
     {
         $this
-            ->setName('device:create')
-            ->setDescription('Create Device command')
-            ->setHelp('Create Device command')
+            ->setName('device:crud')
+            ->setDescription('CRUD Device command')
+            ->setHelp('Create, update or delete a device')
             ->addArgument('type', InputOption::VALUE_REQUIRED, 'Device Type. e.g. FTTHBundle:ONU, FTTHBundle:OLT')
             ->addOption('id', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Device id. e.g. --id=1 --id=2')
-            ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST url for device creation')
+            ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST | PUT | DELETE url for device creation')
+            ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'Http method. e.g. POST, PUT, DELETE', HttpRequestInterface::METHOD_POST)
             ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username', 'admin')
             ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password', 'adminpass')
         ;
@@ -31,15 +32,17 @@ class CreateDeviceCommand extends ContainerAwareCommand
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
+        $type = $input->getArgument('type');
         $ids = $input->getOption('id');
         $url = $input->getOption('url');
+        $method = $input->getOption('method');
         $username = $input->getOption('username');
         $password = $input->getOption('password');
         try {
             // busco por device type e id
             $container = $this->getContainer();
             $em = $container->get('doctrine.orm.entity_manager');
-            $repository = $em->getRepository($input->getArgument('type'));
+            $repository = $em->getRepository($type);
             $entities = array();
             if (!empty($ids)) {
                 if (!is_array($ids)) {
@@ -49,7 +52,7 @@ class CreateDeviceCommand extends ContainerAwareCommand
                     if ($entity = $repository->find($id)) {
                         $entities[] = $entity;
                     } else {
-                        $output->writeln("Entity id <error>{$id}</error> not found!");
+                        $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
                     }
                 }
             } else {
@@ -62,13 +65,20 @@ class CreateDeviceCommand extends ContainerAwareCommand
             }
             $deviceListener = $container->get('device.device_listener');
             foreach ($entities as $entity) {
-                $response = $deviceListener->send($entity, $url, HttpRequestInterface::METHOD_POST, compact('username', 'password'));
+                $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
                 if (json_decode($response)) {
-                    $output->writeln('<info>Device created!</info>');
-                    $output->writeln($response);
+                    if ($method == HttpRequestInterface::METHOD_POST) {
+                        $output->writeln('<info>Device created!</info>');
+                    }
+                    if ($method == HttpRequestInterface::METHOD_PUT) {
+                        $output->writeln('<info>Device updated!</info>');
+                    }
+                } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
+                    $output->writeln('<info>Device deleted!</info>');
                 } else {
-                    $output->writeln("Error: Device for entity id <error>{$id}</error> not created!");
+                    $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
                 }
+                $output->writeln($response);
             }
         } catch (\Exception $ex) {
             $output->writeln($ex->getMessage());

+ 24 - 9
EventListener/DeviceListener.php

@@ -68,7 +68,8 @@ class DeviceListener
             'type:' . get_class($entity),
             '--id:' . $entity->getId(),
         );
-        $this->runCommand('device:create', $cmd_args);
+        
+        return $this->runCommand('device:crud', $cmd_args);
     }
     
     /**
@@ -113,9 +114,14 @@ class DeviceListener
         $entity = $args->getEntity();
         if ($entity instanceof DeviceInterface) {
             if ($deviceId = $this->getRemoteDeviceId($entity)) {
-                $data = array('id' => $deviceId);
+                $cmd_args = array(
+                    'type:' . get_class($entity),
+                    '--id:' . $entity->getId(),
+                    '--url:' . $this->deviceDeletePostUrl,
+                    '--method:' . HttpRequestInterface::METHOD_DELETE,
+                );
                 
-                return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
+                return $this->runCommand('device:crud', $cmd_args);
             }
         }
     }
@@ -131,12 +137,16 @@ class DeviceListener
 
         $entity = $args->getEntity();
         if ($entity instanceof DeviceInterface) {
+            $cmd_args = array(
+                'type:' . get_class($entity),
+                '--id:' . $entity->getId(),
+            );
             if ($deviceId = $this->getRemoteDeviceId($entity)) {
-                $url = "{$this->devicePutUrl}{$deviceId}";
-                $this->send($entity, $url, HttpRequestInterface::METHOD_PUT);
-            } else {
-                $this->send($entity, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
+                $cmd_args[] = "--url:{$this->devicePutUrl}{$deviceId}";
+                $cmd_args[] = '--method:' . HttpRequestInterface::METHOD_PUT;
             }
+            
+            return $this->runCommand('device:crud', $cmd_args);
         }
     }
 
@@ -201,14 +211,19 @@ class DeviceListener
         $deviceType = get_class($entity);
         $tenancyId = $entity->getTenancyId();
 
-        $filters = array('deviceId' => $deviceId, 'deviceType' => $deviceType, 'tenancyId' => $tenancyId);
+        $filters = array(
+            'deviceId' => $deviceId, 
+            'deviceType' => $deviceType, 
+            'tenancyId' => $tenancyId
+        );
         $data = $this->webservice->getData("device_post_url", $filters);
 
 //        file_put_contents("/var/flowdat/error.log",json_encode($data));
 
         $deviceId = null;
-        if (isset($data[0]))
+        if (isset($data[0])) {
             $deviceId = $data[0]['id'];
+        }
 
         return $deviceId;
     }