StatsPonPortCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 StatsBundle\Services\DeviceManager;
  8. class StatsPonPortCommand extends BaseCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('stats:ponport')
  14. ->setDescription('Update PonPort Stats')
  15. ->setHelp('Actualiza la tabla PonPort con los datos obtenidos del OLT definido')
  16. ->setDefinition(array(
  17. new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT", 1),
  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. $oltDeviceId = (int) $input->getOption('olt-device-id');
  30. $oltServerId = (int) $input->getOption('olt-server-id');
  31. $now = date("d-m-Y H:i:s");
  32. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  33. $deviceOlt = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceId' => $oltDeviceId, 'deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT'));
  34. $tenancyId = $deviceOlt->getTenancyId();
  35. $deviceServerId = $oltServerId;
  36. $flag = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  37. $key_pon_stats = "olt_stats_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  38. /* Control de bloqueo */
  39. if($this->lock($flag)) {return;}
  40. $ponsCached = $this->getData($key_pon_stats, true);
  41. if(empty($ponsCached)) {
  42. $this->output->writeln("Se requiere {$key_pon_stats}.");
  43. $this->removeLock($flag);
  44. return true;
  45. }
  46. $ponPorts = $doctrine->getRepository('\StatsBundle\Entity\PonPort')->findBy(array('oltDeviceId' => $oltDeviceId, 'deviceServer' => $oltServerId));
  47. $_ponPorts = array();
  48. foreach($ponPorts as $k => $pp) {
  49. $_ponPorts[$pp->getPonPort()] = $pp->getId();
  50. }
  51. foreach($ponsCached as $index => $stats) {
  52. $row = array();
  53. $ponPort = $stats['ponPort'];
  54. if(isset($_ponPorts[$ponPort])) {
  55. $row['id'] = $_ponPorts[$ponPort];
  56. } else {
  57. $row['id'] = "NULL";
  58. }
  59. $row['deviceServer'] = $deviceServerId;
  60. $row['ponPort'] = "'{$ponPort}'";
  61. $row['oltDeviceId'] = $oltDeviceId;
  62. $row['tenancyId'] = $tenancyId;
  63. $row['update'] = "'".date("Y-m-d H:i:s")."'";
  64. (isset($stats['txPower']))? $row['txPower'] = $stats['txPower'] : $row['txPower'] = "NULL";
  65. (empty($stats['rxPower']))? $row['rxPower'] = "NULL" : $row['rxPower'] = "'".str_replace('"','\"',json_encode($stats['rxPower']))."'";
  66. (isset($stats['voltage']))? $row['voltage'] = $stats['voltage'] : $row['voltage'] = "NULL";
  67. (isset($stats['temperature']))? $row['temperature'] = $stats['temperature'] : $row['temperature'] = "NULL";
  68. (isset($stats['biasCurrent']))? $row['biasCurrent'] = $stats['biasCurrent'] : $row['biasCurrent'] = "NULL";
  69. $data[] = "(".implode(",",$row).")".PHP_EOL;
  70. }
  71. $conn = $doctrine->getConnection();
  72. $sql = "DELETE FROM `pon_port` WHERE device_server_id = {$deviceServerId} AND olt_device_id = {$oltDeviceId};";
  73. $conn->query($sql);
  74. $conn->close();
  75. $conn = $doctrine->getConnection();
  76. $sql = "INSERT LOW_PRIORITY IGNORE INTO `pon_port` (`id`,`device_server_id`,`pon_port`,`olt_device_id`,`tenancy_id`,`updated`,`tx_power`,`rx_power`,`voltage`,`temperature`,`bias_current`) VALUES ". implode(",", $data).";";
  77. //print_r($sql);
  78. $conn->query($sql);
  79. $conn->close();
  80. }
  81. }