ONUController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace FTTHBundle\Controller;
  3. use MapBundle\Util\GeoDecode;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use MapBundle\Entity\Location;
  10. use FTTHBundle\Entity\ONU;
  11. use FTTHBundle\Entity\NAP;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\Extension\Core\Type\DateType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use WebserviceBundle\Form\Type\RemoteClientType;
  16. use MapBundle\Form\Type\RemoteMapType;
  17. use FOS\RestBundle\Controller\Annotations\RouteResource;
  18. /**
  19. * ONU controller.
  20. * @RouteResource("ONU")
  21. */
  22. class ONUController extends Controller
  23. {
  24. public function importAction()
  25. {
  26. $adminPool = $this->get('sonata.admin.pool');
  27. $webservice = $this->get("webservice");
  28. $translator = $this->get('translator');
  29. $flashbag = $this->get('session')->getFlashBag();
  30. $urlObjects = $this->container->hasParameter('remote_get_objects_url');
  31. $urlObjectTypes = $this->container->hasParameter('remote_get_object_type_url');
  32. $_types = array();
  33. $disabled = false;
  34. $objectTypeId = 0;
  35. if (!$urlObjects) {
  36. $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
  37. $disabled = true;
  38. } else {
  39. if (!$urlObjectTypes) {
  40. $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
  41. $disabled = true;
  42. } else {
  43. $urlObjectTypes = $this->getParameter('remote_get_object_type_url');
  44. $filters = array('name' => 'ONU');
  45. $_types = $webservice->getData($urlObjectTypes, $filters, array(), null, null);
  46. }
  47. if (empty($_types)) {
  48. $flashbag->add("error", $translator->trans("msg_no_onu_in_map", array(), "FTTHBundle"));
  49. $disabled = true;
  50. $objectTypeId = 0;
  51. } else {
  52. $disabled = false;
  53. $objectTypeId = $_types[0]['id'];
  54. }
  55. }
  56. $form = $this->createFormBuilder()
  57. ->add('mapId', RemoteMapType::class, array('label' => $translator->trans("form.label_map", array(), "FTTHBundle"), 'disabled' => $disabled))
  58. ->add('clientId', RemoteClientType::class, array('label' => $translator->trans("form.label_client", array(), "FTTHBundle"), 'disabled' => $disabled))
  59. ->getForm();
  60. $flashbag->add("warning", $translator->trans("msg_select_map", array(), "FTTHBundle"));
  61. return $this->render('FTTHBundle:ONU:onu_import.html.twig', array(
  62. 'base_template' => $adminPool->getTemplate('layout'),
  63. 'admin_pool' => $adminPool,
  64. 'admin' => $adminPool->getAdminByClass("FTTHBundle\Entity\ONU"),
  65. 'objectTypeId' => $objectTypeId,
  66. 'form' => $form->createView(),
  67. 'disabled' => $disabled
  68. ));
  69. }
  70. /*
  71. * Queda pendiente enviar un comando para actualizar masivamente los devices
  72. * /admin/ftth/onu/onu_save?_=1508847664462&limit=100&mapId=3&offset=0&clientId=1
  73. */
  74. public function onuSaveAction(Request $request)
  75. {
  76. $em = $this->get('doctrine')->getManager();
  77. $webservice = $this->get("webservice");
  78. $tenancy_service = $this->get("base_tenancy.tenancy_service");
  79. $mapId = $request->get('mapId');
  80. $clientId = $request->get('clientId');
  81. if (is_null($mapId) || empty($mapId) || is_null($clientId) || empty($clientId)) {
  82. $response = new Response(
  83. json_encode(array()),
  84. Response::HTTP_OK,
  85. array('content-type' => 'application/json')
  86. );
  87. return $response;
  88. }
  89. $limit = $request->get('limit');
  90. $offset = $request->get('offset');
  91. $objectTypeId = $request->get('objectTypeId');
  92. $urlObjectTypes = $this->getParameter('remote_get_objects_url');
  93. $filters = array('type' => $objectTypeId, 'map' => $mapId);
  94. $_onus = $webservice->getData($urlObjectTypes, $filters, array(), $limit, $offset);
  95. $return = array();
  96. foreach ($_onus as $onu) {
  97. $object = $em->getRepository("FTTHBundle:ONU")->findOneByPonSerialNumber($onu['text']);
  98. if (is_null($object)) {
  99. $object = new ONU();
  100. $object->setClientId($clientId);
  101. $object->setTenancyId($tenancy_service->getTenancyIdCurrent());
  102. $object->setPonSerialNumber($onu['text']);
  103. $em->persist($object);
  104. $em->flush();
  105. $return[] = "ONU create {$onu['text']}";
  106. }
  107. $location = $object->getLocation();
  108. $action = "update";
  109. if (is_null($location)) {
  110. $location = new Location();
  111. $em->persist($location);
  112. $em->flush();
  113. $object->setLocation($location);
  114. $action = "create";
  115. }
  116. $data = json_decode($onu['vector']['data'], true);
  117. $mapLocation = $data[0];
  118. $mapLocation['zoom'] = 17;
  119. $mapLocation['onu'] = "{$object->getPonSerialNumber()}";
  120. $mapLocation['map_object_id'] = $onu['id'];
  121. $location->setJsonExtraData($mapLocation);
  122. $location->setObjectTypeId($objectTypeId);
  123. $location->setMapId($mapId);
  124. $em->persist($location);
  125. $return[] = "LOCATION (id {$location->getId()}) {$action} - ONU (id {$object->getId()}) {$onu['text']} - " . json_encode($mapLocation);
  126. }
  127. $em->flush();
  128. $response = new Response(
  129. json_encode($return),
  130. Response::HTTP_OK,
  131. array('content-type' => 'application/json')
  132. );
  133. return $response;
  134. }
  135. /**
  136. * @param Request $request
  137. * @return JsonResponse
  138. */
  139. public function getDistanceNapOnuAction(Request $request)
  140. {
  141. $lat = $request->query->get('lat', '');
  142. $lng = $request->query->get('lng', '');
  143. $napId = $request->query->get('napId', 0);
  144. if (isset($lat) && isset($lng)) {
  145. // calculo las distancias
  146. $naps = $this->calculateDistance($lat, $lng, $napId);
  147. // ordeno las distancias con valores positivos
  148. usort($naps, array($this, "orderDistances"));
  149. } else {
  150. $naps = [];
  151. }
  152. $response = new JsonResponse();
  153. $response->setData(['results' => json_encode($naps)]);
  154. return $response;
  155. }
  156. /**
  157. * Calcula la distancia de los NAP al punto pasado.
  158. * @param $lat
  159. * @param $lng
  160. * @param $napId
  161. * @return array Retorna una array con los datos de los NAP y con las distancias calculadas.
  162. */
  163. private function calculateDistance($lat, $lng, $napId)
  164. {
  165. $translator = $this->get('translator');
  166. $naps = array();
  167. $em = $this->container->get("doctrine.orm.entity_manager");
  168. $qb = $em->createQueryBuilder();
  169. $query = $qb->select('n')
  170. ->from(NAP::class, 'n');
  171. $napsQuery = $query->getQuery()->execute();
  172. $geodecode = new GeoDecode();
  173. foreach ($napsQuery as $nap) {
  174. if ($nap->getExtraData() != null) {
  175. $extraData = json_decode($nap->getExtraData(), true);
  176. $nap->setContainer($this->container);
  177. $dataNap = [
  178. 'id' => $nap->getId(),
  179. 'name' => $nap->getName(),
  180. 'distance' => -1,
  181. 'freePort' => $nap->getFreePort(),
  182. 'address' => '',
  183. 'olt' => ($nap->getOlt() != null ? $nap->getOlt()->getName() : "SIN OLT"),
  184. 'slot' => $nap->getSlot(),
  185. 'link' => $nap->getLink()
  186. ];
  187. if ($nap->getlat() != null && $nap->getlng() != null) {
  188. $distance = $geodecode->distanceGeoPoints($lat, $lng, $nap->getlat(), $nap->getlng(), true);
  189. if ($this->container->hasParameter('onu.distance.nap')) {
  190. if ($this->container->getParameter('onu.distance.nap') >= $distance ||
  191. $napId == $nap->getId()) {
  192. // controlo la distancia y el codigo de nap
  193. $dataNap['distance'] = $distance;
  194. } else {
  195. $dataNap['distance'] = $distance . " [" . $translator->trans("out_of_range", array(), "FTTHBundle") . "]";
  196. }
  197. $dataNap['address'] = $extraData['Address'];
  198. $naps[$nap->getId()] = $dataNap;
  199. } else {
  200. // si el codigo de napId coincide tengo que cargar la nap
  201. $dataNap['distance'] = $distance . " [" . "Distancia minima no configurada" . "]";
  202. $dataNap['address'] = $extraData['Address'];
  203. $naps[$nap->getId()] = $dataNap;
  204. }
  205. } else {
  206. $dataNap['distance'] = "-1" . " [" . "NAP no posicionada" . "]";
  207. $naps[$nap->getId()] = $dataNap;
  208. }
  209. }
  210. }
  211. return $naps;
  212. }
  213. /**
  214. * Ordena por las distancias, poniendo los -1 al final.
  215. * @param $a
  216. * @param $b
  217. */
  218. public function orderDistances($a, $b)
  219. {
  220. $a = (float)$a["distance"];
  221. $b = (float)$b["distance"];
  222. if ($a == $b) {
  223. return 0;
  224. }
  225. return ($a < $b) ? -1 : 1;
  226. //return (float)$a["distance"] - (float)$b["distance"];
  227. }
  228. }