StatsGeoOnuCommand.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace StatsBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Process\Process;
  8. use Symfony\Component\Process\Exception\ProcessFailedException;
  9. class StatsGeoOnuCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('stats:onu:geo')
  15. ->setDescription('Update ONU Stats')
  16. ->setHelp('Genera un geoJson con las stats de las ONUs')
  17. ->setDefinition(array(
  18. new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT", 1)
  19. ))
  20. ;
  21. }
  22. /**
  23. * @param InputInterface $input
  24. * @param OutputInterface $output
  25. */
  26. protected function execute(InputInterface $input, OutputInterface $output)
  27. {
  28. parent::execute($input, $output);
  29. $oltServerId = (int) $input->getOption('olt-server-id');
  30. $now = date("d-m-Y H:i:s");
  31. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  32. $path = $this->getContainer()->getParameter('geoserver_path_shapes');
  33. $devicesOlt = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT'));
  34. $metrics = array("tx" => "onu_tx_", "rx" => "onu_rx_", "temp" => "onu_temperature_", "volt" => "onu_voltage_", "status" => "onu_status_");
  35. $data = array();
  36. foreach($devicesOlt as $k => $deviceOlt) {
  37. $oltDeviceId = $deviceOlt->getDeviceId();
  38. $tenancyId = $deviceOlt->getTenancyId();
  39. if(isset($data[$tenancyId])) {
  40. $geo = $data[$tenancyId];
  41. } else {
  42. $geo = array();
  43. $geo['type'] = "FeatureCollection";
  44. $geo['totalFeatures'] = 0;
  45. $geo['features'] = array();
  46. $geo['crs'] = array('type' => "name", 'properties' => array('name' => "urn:ogc:def:crs:EPSG::4326"));
  47. $data[$tenancyId] = $geo;
  48. }
  49. $deviceServerId = $oltServerId;
  50. $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}";
  51. $stats = array();
  52. foreach($metrics as $m => $metric) {
  53. $key_onu_stats = "{$metric}d_{$oltDeviceId}_s_{$oltServerId}";
  54. $stats[$metric] = $this->getData($key_onu_stats, true);
  55. }
  56. $onus = $this->getData($key_olt_scan, true);
  57. $devices = $this->getDevices($oltServerId);
  58. foreach($onus as $index => $onu) {
  59. $sn = $onu['serialNumber'];
  60. $lowSn = strtolower($sn);
  61. if(!isset($devices[$lowSn]))
  62. continue;
  63. $row = array();
  64. $row['type'] = "Feature";
  65. $row['id'] = "onu.{$lowSn}";
  66. $lat = $devices[$lowSn]['lat'];
  67. $lng = $devices[$lowSn]['lng'];
  68. $row['geometry'] = array('type' => "Point", 'coordinates' => array(0 => $lng, 1 => $lat));
  69. $row['geometry_name'] = "the_geom";
  70. $row['properties'] = array();
  71. foreach($metrics as $m => $metric) {
  72. if(isset($stats[$metric][$sn])) {
  73. $row['properties'][$m] = $stats[$metric][$sn];
  74. } else {
  75. $row['properties'][$m] = "null";
  76. }
  77. }
  78. $geo['features'][] = $row;
  79. }
  80. $geo['totalFeatures'] = count($geo['features']);
  81. $data[$tenancyId] = $geo;
  82. }
  83. //print_r($data);
  84. foreach($data as $tenancy => $geoData) {
  85. if($geoData['totalFeatures'] > 0) {
  86. $geoJson = json_encode($geoData);
  87. $file_name = "onu_stats_tenancy_{$tenancy}";
  88. $dir = "$path/deviceServer_{$oltServerId}";
  89. if(!is_dir($dir)) {
  90. mkdir($dir);
  91. chown($dir, "www-data");
  92. }
  93. $file = fopen($dir."/"."{$file_name}.json", "a+");
  94. fwrite($file, $geoJson);
  95. if($this->getContainer()->getParameter('geoserver_service') && file_exists($dir."/"."{$file_name}.json")) {
  96. $this->generateShapes($dir, $file_name);
  97. $workspace = "deviceServer_{$oltServerId}";
  98. $geoserver = $this->getContainer()->get('geoserver.api');
  99. $geoserver->createWorkspace($workspace);
  100. $geoserver->updateShape($workspace);
  101. }
  102. }
  103. }
  104. }
  105. public function generateShapes($dir, $file_name)
  106. {
  107. $process = new Process("ogr2ogr -f 'ESRI Shapefile' {$dir}/{$file_name}.shp {$dir}/{$file_name}.json");
  108. $process->run();
  109. //while ($process->isRunning()) {print_r("Procesando..".PHP_EOL);}
  110. if (!$process->isSuccessful()) {throw new ProcessFailedException($process);}
  111. echo $process->getOutput();
  112. }
  113. public function getDevices($oltServerId)
  114. {
  115. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  116. $data = array();
  117. $onus = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\ONU'));
  118. foreach($onus as $k => $device) {
  119. $extra = $device->jsonExtraData();
  120. if(isset($extra['ponSerialNumber'])) {
  121. $sn = strtolower($extra['ponSerialNumber']);
  122. if(isset($extra['location']) && isset($extra['location']['extraData']))
  123. {
  124. if(isset($extra['location']['extraData']['lat'])) {
  125. $lat = $extra['location']['extraData']['lat'];
  126. } else {
  127. continue;
  128. }
  129. if(isset($extra['location']['extraData']['lng'])) {
  130. $lng = $extra['location']['extraData']['lng'];
  131. } else {
  132. continue;
  133. }
  134. if(is_null($lat) || is_null($lng)) continue;
  135. } else {
  136. continue;
  137. }
  138. $data[$sn] = array('deviceId' => $device->getDeviceId(), 'lat' => $lat, 'lng' => $lng);
  139. }
  140. }
  141. //print_r($data);die;
  142. return $data;
  143. }
  144. }