123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace FTTHBundle\Controller;
- use MapBundle\Util\GeoDecode;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\Response;
- use MapBundle\Entity\Location;
- use FTTHBundle\Entity\ONU;
- use FTTHBundle\Entity\NAP;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use WebserviceBundle\Form\Type\RemoteClientType;
- use MapBundle\Form\Type\RemoteMapType;
- use FOS\RestBundle\Controller\Annotations\RouteResource;
- /**
- * ONU controller.
- * @RouteResource("ONU")
- */
- class ONUController extends Controller
- {
- public function importAction()
- {
- $adminPool = $this->get('sonata.admin.pool');
- $webservice = $this->get("webservice");
- $translator = $this->get('translator');
- $flashbag = $this->get('session')->getFlashBag();
- $urlObjects = $this->container->hasParameter('remote_get_objects_url');
- $urlObjectTypes = $this->container->hasParameter('remote_get_object_type_url');
- $_types = array();
- $disabled = false;
- $objectTypeId = 0;
- if (!$urlObjects) {
- $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
- $disabled = true;
- } else {
- if (!$urlObjectTypes) {
- $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
- $disabled = true;
- } else {
- $urlObjectTypes = $this->getParameter('remote_get_object_type_url');
- $filters = array('name' => 'ONU');
- $_types = $webservice->getData($urlObjectTypes, $filters, array(), null, null);
- }
- if (empty($_types)) {
- $flashbag->add("error", $translator->trans("msg_no_onu_in_map", array(), "FTTHBundle"));
- $disabled = true;
- $objectTypeId = 0;
- } else {
- $disabled = false;
- $objectTypeId = $_types[0]['id'];
- }
- }
- $form = $this->createFormBuilder()
- ->add('mapId', RemoteMapType::class, array('label' => $translator->trans("form.label_map", array(), "FTTHBundle"), 'disabled' => $disabled))
- ->add('clientId', RemoteClientType::class, array('label' => $translator->trans("form.label_client", array(), "FTTHBundle"), 'disabled' => $disabled))
- ->getForm();
- $flashbag->add("warning", $translator->trans("msg_select_map", array(), "FTTHBundle"));
- return $this->render('FTTHBundle:ONU:onu_import.html.twig', array(
- 'base_template' => $adminPool->getTemplate('layout'),
- 'admin_pool' => $adminPool,
- 'admin' => $adminPool->getAdminByClass("FTTHBundle\Entity\ONU"),
- 'objectTypeId' => $objectTypeId,
- 'form' => $form->createView(),
- 'disabled' => $disabled
- ));
- }
- /*
- * Queda pendiente enviar un comando para actualizar masivamente los devices
- * /admin/ftth/onu/onu_save?_=1508847664462&limit=100&mapId=3&offset=0&clientId=1
- */
- public function onuSaveAction(Request $request)
- {
- $em = $this->get('doctrine')->getManager();
- $webservice = $this->get("webservice");
- $tenancy_service = $this->get("base_tenancy.tenancy_service");
- $mapId = $request->get('mapId');
- $clientId = $request->get('clientId');
- if (is_null($mapId) || empty($mapId) || is_null($clientId) || empty($clientId)) {
- $response = new Response(
- json_encode(array()),
- Response::HTTP_OK,
- array('content-type' => 'application/json')
- );
- return $response;
- }
- $limit = $request->get('limit');
- $offset = $request->get('offset');
- $objectTypeId = $request->get('objectTypeId');
- $urlObjectTypes = $this->getParameter('remote_get_objects_url');
- $filters = array('type' => $objectTypeId, 'map' => $mapId);
- $_onus = $webservice->getData($urlObjectTypes, $filters, array(), $limit, $offset);
- $return = array();
- foreach ($_onus as $onu) {
- $object = $em->getRepository("FTTHBundle:ONU")->findOneByPonSerialNumber($onu['text']);
- if (is_null($object)) {
- $object = new ONU();
- $object->setClientId($clientId);
- $object->setTenancyId($tenancy_service->getTenancyIdCurrent());
- $object->setPonSerialNumber($onu['text']);
- $em->persist($object);
- $em->flush();
- $return[] = "ONU create {$onu['text']}";
- }
- $location = $object->getLocation();
- $action = "update";
- if (is_null($location)) {
- $location = new Location();
- $em->persist($location);
- $em->flush();
- $object->setLocation($location);
- $action = "create";
- }
- $data = json_decode($onu['vector']['data'], true);
- $mapLocation = $data[0];
- $mapLocation['zoom'] = 17;
- $mapLocation['onu'] = "{$object->getPonSerialNumber()}";
- $mapLocation['map_object_id'] = $onu['id'];
- $location->setJsonExtraData($mapLocation);
- $location->setObjectTypeId($objectTypeId);
- $location->setMapId($mapId);
- $em->persist($location);
- $return[] = "LOCATION (id {$location->getId()}) {$action} - ONU (id {$object->getId()}) {$onu['text']} - " . json_encode($mapLocation);
- }
- $em->flush();
- $response = new Response(
- json_encode($return),
- Response::HTTP_OK,
- array('content-type' => 'application/json')
- );
- return $response;
- }
- /**
- * @param Request $request
- * @return JsonResponse
- */
- public function getDistanceNapOnuAction(Request $request)
- {
- $lat = $request->query->get('lat', '');
- $lng = $request->query->get('lng', '');
- $napId = $request->query->get('napId', 0);
- if (isset($lat) && isset($lng)) {
- // calculo las distancias
- $naps = $this->calculateDistance($lat, $lng, $napId);
- // ordeno las distancias con valores positivos
- usort($naps, array($this, "orderDistances"));
- } else {
- $naps = [];
- }
- $response = new JsonResponse();
- $response->setData(['results' => json_encode($naps)]);
- return $response;
- }
- /**
- * Calcula la distancia de los NAP al punto pasado.
- * @param $lat
- * @param $lng
- * @param $napId
- * @return array Retorna una array con los datos de los NAP y con las distancias calculadas.
- */
- private function calculateDistance($lat, $lng, $napId)
- {
- $translator = $this->get('translator');
- $naps = array();
- $em = $this->container->get("doctrine.orm.entity_manager");
- $qb = $em->createQueryBuilder();
- $query = $qb->select('n')
- ->from(NAP::class, 'n');
- $napsQuery = $query->getQuery()->execute();
- $geodecode = new GeoDecode();
- foreach ($napsQuery as $nap) {
- if ($nap->getExtraData() != null) {
- $extraData = json_decode($nap->getExtraData(), true);
- $nap->setContainer($this->container);
- $dataNap = [
- 'id' => $nap->getId(),
- 'name' => $nap->getName(),
- 'distance' => -1,
- 'freePort' => $nap->getFreePort(),
- 'address' => '',
- 'olt' => ($nap->getOlt() != null ? $nap->getOlt()->getName() : "SIN OLT"),
- 'slot' => $nap->getSlot(),
- 'link' => $nap->getLink()
- ];
- if ($nap->getlat() != null && $nap->getlng() != null) {
- $distance = $geodecode->distanceGeoPoints($lat, $lng, $nap->getlat(), $nap->getlng(), true);
- if ($this->container->hasParameter('onu.distance.nap')) {
- if ($this->container->getParameter('onu.distance.nap') >= $distance ||
- $napId == $nap->getId()) {
- // controlo la distancia y el codigo de nap
- $dataNap['distance'] = $distance;
- } else {
- $dataNap['distance'] = $distance . " [" . $translator->trans("out_of_range", array(), "FTTHBundle") . "]";
- }
- $dataNap['address'] = $extraData['Address'];
- $naps[$nap->getId()] = $dataNap;
- } else {
- // si el codigo de napId coincide tengo que cargar la nap
- $dataNap['distance'] = $distance . " [" . "Distancia minima no configurada" . "]";
- $dataNap['address'] = $extraData['Address'];
- $naps[$nap->getId()] = $dataNap;
- }
- } else {
- $dataNap['distance'] = "-1" . " [" . "NAP no posicionada" . "]";
- $naps[$nap->getId()] = $dataNap;
- }
- }
- }
- return $naps;
- }
- /**
- * Ordena por las distancias, poniendo los -1 al final.
- * @param $a
- * @param $b
- */
- public function orderDistances($a, $b)
- {
- $a = (float)$a["distance"];
- $b = (float)$b["distance"];
- if ($a == $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- //return (float)$a["distance"] - (float)$b["distance"];
- }
- }
|