Parcourir la source

Command to update ponSerialNumbers and update css of dropdown workflow. Ref #26

Maximiliano Schvindt il y a 6 ans
Parent
commit
3383732acf
2 fichiers modifiés avec 124 ajouts et 2 suppressions
  1. 2 2
      composer.lock
  2. 122 0
      src/FTTHBundle/Command/MigrateSerialToPonCommand.php

+ 2 - 2
composer.lock

@@ -1947,7 +1947,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/BaseAdmin.git",
-                "reference": "a3268eb5d3806d8d811e8df78c148544dfc46f4d"
+                "reference": "0e77ef117751fc9360086347cb8554a507d84ce6"
             },
             "type": "library",
             "autoload": {
@@ -1962,7 +1962,7 @@
                 "bootstrap",
                 "sonata"
             ],
-            "time": "2019-03-15T17:10:29+00:00"
+            "time": "2019-03-28T11:53:21+00:00"
         },
         {
             "name": "ik/check-sintax-bundle",

+ 122 - 0
src/FTTHBundle/Command/MigrateSerialToPonCommand.php

@@ -0,0 +1,122 @@
+<?php
+
+namespace FTTHBundle\Command;
+
+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;
+
+class MigrateSerialToPonCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('migrate:serial:pon')
+            ->setDescription('Convert in Pon Serial Number the value in pon_serial_number_aux field in DB onu when value is a serial_number')
+            ->setHelp('Convert in Pon Serial Number the value in pon_serial_number_aux field in DB onu when value is a serial_number')
+            ->setDefinition(array())
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+
+        
+        $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
+        
+        $onus = $doctrine->getRepository('\FTTHBundle\Entity\ONU')->findAll();
+        $updates = array();
+
+        foreach($onus as $i => $onu) {
+            $value = $onu->getPonSerialNumberAux(); 
+            
+            if(strlen($value) == 16) {
+                $newValue = $this->psnCalulate($value);
+                $updates[$onu->getId()] = $newValue;
+            }
+            
+        }
+
+        $sql = null;
+        foreach($updates as $id => $value) {
+            if($id) {
+                $sql .= "UPDATE `onu` SET `pon_serial_number_aux` = '{$value}' WHERE `onu`.`id` = {$id};".PHP_EOL;
+            }
+        }
+
+        if($sql) {
+            $conn = $doctrine->getConnection();
+            $conn->query($sql);
+            $conn->close();
+        }
+    }
+
+    private function hex2str($hex) {
+        $hex = strtoupper($hex);
+        $str = "";
+        for($i=0;$i<strlen($hex);$i+=2)
+            $str .= chr(hexdec(substr($hex,$i,2)));
+
+        return $str;
+    }
+
+    private function str2hex($string){
+        $string = strtoupper($string);
+        $hex = "";
+        for ($i=0; $i < strlen($string); $i++)
+            $hex .= dechex(ord($string[$i]));
+
+        return $hex;
+    }
+
+    //HWTC22BDED0C > 4857544322BDED0C
+    private function snCalulate($value) {
+        $value = str_replace(" ","",$value);
+        
+        if(strlen($value) == 16)
+            return strtolower($value);
+        
+        if(strlen($value) == 12) {
+            $vendoId = $this->str2hex(substr($value,0,4));
+            $rest = substr($value,4);
+            return strtolower($vendoId.$rest);
+        }
+
+        return strtolower($value);
+    }
+
+    //4857544322BDED0C > HWTC22BDED0C
+    private function psnCalulate($value) {
+        $value = str_replace(" ","",$value);
+        
+        if(strlen($value) == 12)
+            return strtolower($value);
+        
+        if(strlen($value) == 16) {
+            $vendorId = substr($value,0,8);
+            $hex = strtoupper($vendorId);
+            $isNumeric = true;
+            for($i=0;$i<strlen($hex);$i+=2) {
+                if(!is_numeric(substr($hex,$i,2))) {
+                    $isNumeric = false;
+                    break;
+                }
+            }
+            
+            if($isNumeric) {
+                $vendoId = $this->hex2str($hex);
+                $rest = substr($value,8);
+                return strtolower($vendoId.$rest);
+            }
+        }
+
+        return strtolower($value);
+    }
+}