setName('stats:onu:geo') ->setDescription('Update ONU Stats') ->setHelp('Genera un geoJson con las stats de las ONUs') ->setDefinition(array( new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT", 1) )) ; } /** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); $oltServerId = (int) $input->getOption('olt-server-id'); $now = date("d-m-Y H:i:s"); $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager'); $path = $this->getContainer()->getParameter('geoserver_path_shapes'); $devicesOlt = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT')); $metrics = array("tx" => "onu_tx_", "rx" => "onu_rx_", "temp" => "onu_temperature_", "volt" => "onu_voltage_", "status" => "onu_status_"); $data = array(); foreach($devicesOlt as $k => $deviceOlt) { $oltDeviceId = $deviceOlt->getDeviceId(); $tenancyId = $deviceOlt->getTenancyId(); if(isset($data[$tenancyId])) { $geo = $data[$tenancyId]; } else { $geo = array(); $geo['type'] = "FeatureCollection"; $geo['totalFeatures'] = 0; $geo['features'] = array(); $geo['crs'] = array('type' => "name", 'properties' => array('name' => "urn:ogc:def:crs:EPSG::4326")); $data[$tenancyId] = $geo; } $deviceServerId = $oltServerId; $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}"; $stats = array(); foreach($metrics as $m => $metric) { $key_onu_stats = "{$metric}d_{$oltDeviceId}_s_{$oltServerId}"; $stats[$metric] = $this->getData($key_onu_stats, true); } $onus = $this->getData($key_olt_scan, true); $devices = $this->getDevices($oltServerId); foreach($onus as $index => $onu) { $sn = $onu['serialNumber']; $lowSn = strtolower($sn); if(!isset($devices[$lowSn])) continue; $row = array(); $row['type'] = "Feature"; $row['id'] = "onu.{$lowSn}"; $lat = $devices[$lowSn]['lat']; $lng = $devices[$lowSn]['lng']; $row['geometry'] = array('type' => "Point", 'coordinates' => array(0 => $lng, 1 => $lat)); $row['geometry_name'] = "the_geom"; $row['properties'] = array(); foreach($metrics as $m => $metric) { if(isset($stats[$metric][$sn])) { $row['properties'][$m] = $stats[$metric][$sn]; } else { $row['properties'][$m] = "null"; } } $geo['features'][] = $row; } $geo['totalFeatures'] = count($geo['features']); $data[$tenancyId] = $geo; } //print_r($data); foreach($data as $tenancy => $geoData) { if($geoData['totalFeatures'] > 0) { $geoJson = json_encode($geoData); $file_name = "onu_stats_tenancy_{$tenancy}"; $dir = "$path/deviceServer_{$oltServerId}"; if(!is_dir($dir)) { mkdir($dir); chown($dir, "www-data"); } $file = fopen($dir."/"."{$file_name}.json", "a+"); fwrite($file, $geoJson); if($this->getContainer()->getParameter('geoserver_service') && file_exists($dir."/"."{$file_name}.json")) { $this->generateShapes($dir, $file_name); $workspace = "deviceServer_{$oltServerId}"; $geoserver = $this->getContainer()->get('geoserver.api'); $geoserver->createWorkspace($workspace); $geoserver->updateShape($workspace); } } } } public function generateShapes($dir, $file_name) { $process = new Process("ogr2ogr -f 'ESRI Shapefile' {$dir}/{$file_name}.shp {$dir}/{$file_name}.json"); $process->run(); //while ($process->isRunning()) {print_r("Procesando..".PHP_EOL);} if (!$process->isSuccessful()) {throw new ProcessFailedException($process);} echo $process->getOutput(); } public function getDevices($oltServerId) { $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager'); $data = array(); $onus = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\ONU')); foreach($onus as $k => $device) { $extra = $device->jsonExtraData(); if(isset($extra['ponSerialNumber'])) { $sn = strtolower($extra['ponSerialNumber']); if(isset($extra['location']) && isset($extra['location']['extraData'])) { if(isset($extra['location']['extraData']['lat'])) { $lat = $extra['location']['extraData']['lat']; } else { continue; } if(isset($extra['location']['extraData']['lng'])) { $lng = $extra['location']['extraData']['lng']; } else { continue; } if(is_null($lat) || is_null($lng)) continue; } else { continue; } $data[$sn] = array('deviceId' => $device->getDeviceId(), 'lat' => $lat, 'lng' => $lng); } } //print_r($data);die; return $data; } }