Commit 75a8e304b1b5cac52cbd30b4c8cfa50d55c4f53e

Authored by Eduardo Santos
2 parents 8207a89e 61df3268
Exists in master and in 2 other branches 3,1, 3.1

Adiciona API para inserção de impressão.

Showing 1 changed file with 30 additions and 20 deletions   Show diff stats
Controller/ApiController.php
@@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12 use Symfony\Component\HttpFoundation\Request; 12 use Symfony\Component\HttpFoundation\Request;
13 use Symfony\Component\HttpFoundation\JsonResponse; 13 use Symfony\Component\HttpFoundation\JsonResponse;
14 use Swpb\Bundle\CocarBundle\Entity\PrinterCounter; 14 use Swpb\Bundle\CocarBundle\Entity\PrinterCounter;
  15 +use Swpb\Bundle\CocarBundle\Entity\Printer;
15 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 16 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
16 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 17 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
17 18
@@ -55,7 +56,7 @@ class ApiController extends Controller { @@ -55,7 +56,7 @@ class ApiController extends Controller {
55 /** 56 /**
56 * @param $ip_addr 57 * @param $ip_addr
57 * @Route("/printer/{ip_addr}", name="printer_counter_update") 58 * @Route("/printer/{ip_addr}", name="printer_counter_update")
58 - * @Method("PUT") 59 + * @Method("POST")
59 */ 60 */
60 public function printerAction($ip_addr, Request $request) { 61 public function printerAction($ip_addr, Request $request) {
61 $em = $this->getDoctrine()->getManager(); 62 $em = $this->getDoctrine()->getManager();
@@ -81,40 +82,49 @@ class ApiController extends Controller { @@ -81,40 +82,49 @@ class ApiController extends Controller {
81 82
82 $logger->debug("Atualizando informações para a impressora com IP = $ip_addr\n".$status); 83 $logger->debug("Atualizando informações para a impressora com IP = $ip_addr\n".$status);
83 84
84 - try {  
85 - $printer = $em->getRepository('CocarBundle:Printer')->findOneBy(array('host' => $ip_addr));  
86 - }  
87 - catch(\Doctrine\ORM\NoResultException $e) {  
88 - $logger->error("COLETA: Impressora não cadastrada: $ip_addr \n$e");  
89 - $error_msg = '{  
90 - "message": "Impressora não cadastrada",  
91 - "codigo": 2  
92 - }'; 85 + $printer = $em->getRepository('CocarBundle:Printer')->findOneBy(array('host' => $ip_addr));
  86 + if (empty($printer)) {
  87 + $logger->error("COLETA: Impressora não cadastrada: $ip_addr. Inserindo....");
93 88
94 89
95 - $response = new JsonResponse();  
96 - $response->setStatusCode('500');  
97 - $response->setContent($error_msg);  
98 - return $response; 90 + // Insere impressora que não estiver cadastrada
  91 + $printer = new Printer();
  92 +
  93 + // FIXME: Deve ser retornado pelo Cocar
  94 + $data = new \DateTime();
  95 + $printer->setCommunitySnmpPrinter('public');
  96 + $printer->setHost($ip_addr);
  97 + $printer->setDescription('Impressora detectada automaticamente em '.$data->format('d/m/Y'));
  98 + $printer->setName("Impressora $ip_addr");
  99 +
99 } 100 }
100 101
101 102
102 $counter = $this->getDoctrine()->getManager()->getRepository('CocarBundle:PrinterCounter')->findBy(array( 103 $counter = $this->getDoctrine()->getManager()->getRepository('CocarBundle:PrinterCounter')->findBy(array(
103 - 'printer' => $printer, 104 + 'printer' => $printer->getId(),
104 'date' => $dados['counter_time'] 105 'date' => $dados['counter_time']
105 )); 106 ));
106 107
107 if(empty($counter)) { 108 if(empty($counter)) {
108 $counter = new PrinterCounter; 109 $counter = new PrinterCounter;
109 } else { 110 } else {
110 - $this->get('logger')->error("Entrada repetida para impressora $printer e data ".$dados['counter_time']);  
111 - return true; 111 + $this->get('logger')->error("Entrada repetida para impressora". $printer->getId() ." e data ".$dados['counter_time']);
  112 + $response = new JsonResponse();
  113 + $response->setStatusCode('200');
  114 +
  115 + return $response;
112 } 116 }
113 117
114 // Atualiza impressora sempre que alterar o serial 118 // Atualiza impressora sempre que alterar o serial
115 - $printer->setName($dados['model']);  
116 - $printer->setSerie($dados['serial']);  
117 - $printer->setDescription($dados['description']); 119 + if (!empty($dados['model'])) {
  120 + $printer->setName($dados['model']);
  121 + }
  122 + if (!empty($dados['serial'])) {
  123 + $printer->setSerie($dados['serial']);
  124 + }
  125 + if (!empty($dados['description'])) {
  126 + $printer->setDescription($dados['description']);
  127 + }
118 128
119 // Grava o contador 129 // Grava o contador
120 $counter->setPrinter($printer); 130 $counter->setPrinter($printer);