StatsPonPortCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $key_pon_stats = "olt_stats_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  37. $ponsCached = $this->getData($key_pon_stats, true);
  38. if(empty($ponsCached)) {
  39. $this->output->writeln("Se requiere {$key_pon_stats}.");
  40. return true;
  41. }
  42. $ponPorts = $doctrine->getRepository('\StatsBundle\Entity\PonPort')->findBy(array('oltDeviceId' => $oltDeviceId, 'deviceServer' => $oltServerId));
  43. $_ponPorts = array();
  44. foreach($ponPorts as $k => $pp) {
  45. $_ponPorts[$pp->getPonPort()] = $pp->getId();
  46. }
  47. foreach($ponsCached as $index => $stats) {
  48. $row = array();
  49. $ponPort = $stats['ponPort'];
  50. if(isset($_ponPorts[$ponPort])) {
  51. $row['id'] = $_ponPorts[$ponPort];
  52. } else {
  53. $row['id'] = "NULL";
  54. }
  55. $row['deviceServer'] = $deviceServerId;
  56. $row['ponPort'] = "'{$ponPort}'";
  57. $row['oltDeviceId'] = $oltDeviceId;
  58. $row['tenancyId'] = $tenancyId;
  59. $row['update'] = "'".date("Y-m-d H:i:s")."'";
  60. (isset($stats['txPower']))? $row['txPower'] = $stats['txPower'] : $row['txPower'] = "NULL";
  61. (empty($stats['rxPower']))? $row['rxPower'] = "NULL" : $row['rxPower'] = "'".str_replace('"','\"',json_encode($stats['rxPower']))."'";
  62. (isset($stats['voltage']))? $row['voltage'] = $stats['voltage'] : $row['voltage'] = "NULL";
  63. (isset($stats['temperature']))? $row['temperature'] = $stats['temperature'] : $row['temperature'] = "NULL";
  64. (isset($stats['biasCurrent']))? $row['biasCurrent'] = $stats['biasCurrent'] : $row['biasCurrent'] = "NULL";
  65. $data[] = "(".implode(",",$row).")".PHP_EOL;
  66. }
  67. $conn = $doctrine->getConnection();
  68. $sql = "DELETE FROM `pon_port` WHERE device_server_id = {$deviceServerId} AND olt_device_id = {$oltDeviceId};";
  69. $conn->query($sql);
  70. $conn->close();
  71. $conn = $doctrine->getConnection();
  72. $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).";";
  73. //print_r($sql);
  74. $conn->query($sql);
  75. $conn->close();
  76. }
  77. }