Преглед изворни кода

FD3-185 Comando para utilizar api geoserver y generar geoJson.

Maximiliano Schvindt пре 7 година
родитељ
комит
f30d817a11
5 измењених фајлова са 152 додато и 66 уклоњено
  1. 4 3
      app/AppKernel.php
  2. 1 0
      app/config/config.yml
  3. 8 0
      composer.json
  4. 23 1
      composer.lock
  5. 116 62
      src/StatsBundle/Command/StatsGeoOnuCommand.php

+ 4 - 3
app/AppKernel.php

@@ -28,8 +28,8 @@ class AppKernel extends Kernel
             new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
             new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
 
-	    new MigrationsBundle\MigrationsBundle(),
-	    new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
+	        new MigrationsBundle\MigrationsBundle(),
+	        new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
             
             new WebserviceBundle\WebserviceBundle(),
             new Base\AdminBundle\BaseAdminBundle(),
@@ -41,7 +41,8 @@ class AppKernel extends Kernel
             new StatsBundle\StatsBundle(),
             new RedisBundle\RedisBundle(),
             new FiberhomeBundle\FiberhomeBundle(),
-            new StatsDBundle\StatsDBundle()
+            new StatsDBundle\StatsDBundle(),
+            new GeoserverBundle\GeoserverBundle()
         ];
 
         if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {

+ 1 - 0
app/config/config.yml

@@ -17,6 +17,7 @@ imports:
    
     - { resource: "@StatsBundle/Resources/config/services.yml" }
     - { resource: '@BaseAdminBundle/Resources/config/services.yml' }
+    - { resource: '@GeoserverBundle/Resources/config/services.yml' }
 
 # Put parameters here that don't need to change on each machine where the app is deployed
 # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration

+ 8 - 0
composer.json

@@ -83,6 +83,13 @@
                     "local_pk": "./keys/bitbucket.id_rsa"
             }
         },
+        {
+            "type": "vcs",
+			"url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/GeoserverBundle.git",
+            "options": {
+                    "local_pk": "./keys/bitbucket.id_rsa"
+            }
+        },
         {
             "type": "vcs",
             "url": "ssh://git@bitbucket.org/ikflowdat/migrations.git",
@@ -105,6 +112,7 @@
         "ik/owner-voter-bundle": "dev-master",
         "ik/redis-bundle": "dev-master",
         "ik/stats/fiberhome-bundle": "dev-master",
+        "ik/stats/geoserver-bundle": "dev-master",
         "ik/statsd-bundle": "dev-master",
         "ik/webservice-bundle": "dev-master",
         "incenteev/composer-parameter-handler": "^2.0",

+ 23 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "1cd1f64212239b5b6289234519b4822c",
+    "content-hash": "fae3f8bf069c907f35389e125a8de067",
     "packages": [
         {
             "name": "behat/transliterator",
@@ -1578,6 +1578,27 @@
             ],
             "time": "2017-08-23 14:30:32"
         },
+        {
+            "name": "ik/stats/geoserver-bundle",
+            "version": "dev-master",
+            "source": {
+                "type": "git",
+                "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/GeoserverBundle.git",
+                "reference": "d0e5ae5191f80dce9af3eec156198cfef359dbd9"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "GeoserverBundle\\": ""
+                }
+            },
+            "description": "Flowdat 3 Geoserver Bundle",
+            "keywords": [
+                "Flowdat 3",
+                "Geoserver"
+            ],
+            "time": "2017-08-24 18:28:51"
+        },
         {
             "name": "ik/statsd-bundle",
             "version": "dev-master",
@@ -6017,6 +6038,7 @@
         "ik/owner-voter-bundle": 20,
         "ik/redis-bundle": 20,
         "ik/stats/fiberhome-bundle": 20,
+        "ik/stats/geoserver-bundle": 20,
         "ik/statsd-bundle": 20,
         "ik/webservice-bundle": 20,
         "opensolutions/oss-snmp": 20,

+ 116 - 62
src/StatsBundle/Command/StatsGeoOnuCommand.php

@@ -6,7 +6,8 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
-use StatsBundle\Services\DeviceManager;
+use Symfony\Component\Process\Process;
+use Symfony\Component\Process\Exception\ProcessFailedException;
 
 class StatsGeoOnuCommand extends BaseCommand
 {
@@ -18,7 +19,6 @@ class StatsGeoOnuCommand extends BaseCommand
             ->setDescription('Update ONU Stats')
             ->setHelp('Genera un geoJson con las stats de las ONUs')
             ->setDefinition(array(
-                new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT", 1),
                 new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT", 1)
             ))
         ;
@@ -32,90 +32,144 @@ class StatsGeoOnuCommand extends BaseCommand
     {
         parent::execute($input, $output);
 
-        $oltDeviceId = (int) $input->getOption('olt-device-id');
         $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');
         
-        $deviceOlt = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceId' => $oltDeviceId, 'deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT'));
-
-        $tenancyId = $deviceOlt->getTenancyId();
-        $deviceServerId = $oltServerId;
-        
-        $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}";
-
+        $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 = $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);
-
-        $geo = array();
-        $geo['type'] = "FeatureCollection";
-        $geo['totalFeatures'] = 0;
-        $geo['features'] = array();
 
+        $data = array();
 
-        $count = 0;
-        
-        foreach($onus as $index => $onu) {
-            $sn = $onu['serialNumber'];
-            $lowSn = strtolower($sn);
-            
-            if(!isset($devices[$lowSn])) 
-                continue;
+        foreach($devicesOlt as $k => $deviceOlt) {
+            $oltDeviceId = $deviceOlt->getDeviceId();
+            $tenancyId = $deviceOlt->getTenancyId();
             
-            $row = array();
+            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;
+            }
 
-            $row['type'] = "Feature";
-            $row['id'] = "onu.{$lowSn}";
-            
-            
-            $lat = $devices[$lowSn]['lat'];
-            $lng = $devices[$lowSn]['lng'];
+            $deviceServerId = $oltServerId;
             
-            $row['geometry'] = array('type' => "Point", 'coordinates' => array(0 => $lng, 1 => $lat));
-            $row['geometry_name'] = "the_geom";
+            $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}";
             
-            $row['properties'] = array();
+            $stats = array();
             
             foreach($metrics as $m => $metric) {
-                if(isset($stats[$metric][$sn])) {
-                    $row['properties'][$m] = $stats[$metric][$sn];
-                } else {
-                    $row['properties'][$m] = "null";
+                $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']);
             
-            $geo['features'][] = $row;
-            $count++;
+
+            $data[$tenancyId] = $geo;
+
         }
 
-        $geo['totalFeatures'] = $count;
-        $geo['crs'] = array('type' => "name", 'properties' => array('name' => "urn:ogc:def:crs:EPSG::4326"));
+        //print_r($data);
 
+        foreach($data as $tenancy => $geoData) {
+            if($geoData['totalFeatures'] > 0) {
 
-        if($count > 0) {
-            $data = json_encode($geo);
-        
-            $file_name = time().".json";
-            $dir = "/var/flowdat/geoFd3/{$oltDeviceId}_{$oltServerId}";
-            if(!is_dir($dir))
-                mkdir($dir);
-    
-            $file = fopen($dir."/".$file_name, "a+");
-            fwrite($file, $data);
-        
+                $geoJson = json_encode($geoData);
+
+                $file_name = "onu_stats_tenancy_{$tenancy}".".json";
+                $shape_name = "onu_stats_tenancy_{$tenancy}".".shp";
+                $dir = "$path/deviceServer_{$oltServerId}";
+
+                if(!is_dir($dir)) {
+                    mkdir($dir);
+                    chown($dir, "www-data");
+                }
+                
+                $file = fopen($dir."/".$file_name, "a+");
+                fwrite($file, $geoJson);
+                
+                
+                if($this->getContainer()->getParameter('geoserver_service') && file_exists($dir."/".$file_name)) {
+                    $this->generateShapes($dir, $file_name, $shape_name);
+                    $workspace = "deviceServer_{$oltServerId}";
+
+                    $geoserver = $this->getContainer()->get('geoserver.api');
+                    $geoserver->createWorkspace($workspace);
+                    $geoserver->updateShape($workspace);
+                }
+
+            }
         }
+    }
+
+    public function generateShapes($dir, $file_name, $shape_name) 
+    {
+        $process = new Process("ogr2ogr -f 'ESRI Shapefile' {$dir}/{$shape_name} {$dir}/{$file_name}");
+
+        //$process->run();
+        $process->start();
 
+        while ($process->isRunning()) {
+            // waiting for process to finish
+        }
+                    
+        if (!$process->isSuccessful()) {throw new ProcessFailedException($process);}
+                    
+        echo $process->getOutput();
+
+        $files = scandir($dir); 
+        foreach($files as $file)
+        {
+            if(is_file($dir."/".$file)) {
+                chmod($dir."/".$file, 0777);
+                chgrp($dir."/".$file, "www-data");
+                chown($dir."/".$file, "www-data");
+            }
+        }
     }
 
     public function getDevices($oltServerId)