|
@@ -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);
|
|
|
+ }
|
|
|
+}
|