setName('stats:ponport') ->setDescription('Update PonPort Stats') ->setHelp('Actualiza la tabla PonPort con los datos obtenidos del OLT definido') ->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) )) ; } /** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { 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'); $deviceOlt = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceId' => $oltDeviceId, 'deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT')); $tenancyId = $deviceOlt->getTenancyId(); $deviceServerId = $oltServerId; $key_pon_stats = "olt_stats_pons_d_{$oltDeviceId}_s_{$oltServerId}"; $ponsCached = $this->getData($key_pon_stats, true); if(empty($ponsCached)) { $this->output->writeln("Se requiere {$key_pon_stats}."); return true; } $ponPorts = $doctrine->getRepository('\StatsBundle\Entity\PonPort')->findBy(array('oltDeviceId' => $oltDeviceId, 'deviceServer' => $oltServerId)); $_ponPorts = array(); foreach($ponPorts as $k => $pp) { $_ponPorts[$pp->getPonPort()] = $pp->getId(); } foreach($ponsCached as $index => $stats) { $row = array(); $ponPort = $stats['ponPort']; if(isset($_ponPorts[$ponPort])) { $row['id'] = $_ponPorts[$ponPort]; } else { $row['id'] = "NULL"; } $row['deviceServer'] = $deviceServerId; $row['ponPort'] = "'{$ponPort}'"; $row['oltDeviceId'] = $oltDeviceId; $row['tenancyId'] = $tenancyId; $row['update'] = "'".date("Y-m-d H:i:s")."'"; (isset($stats['txPower']))? $row['txPower'] = $stats['txPower'] : $row['txPower'] = "NULL"; (empty($stats['rxPower']))? $row['rxPower'] = "NULL" : $row['rxPower'] = "'".str_replace('"','\"',json_encode($stats['rxPower']))."'"; (isset($stats['voltage']))? $row['voltage'] = $stats['voltage'] : $row['voltage'] = "NULL"; (isset($stats['temperature']))? $row['temperature'] = $stats['temperature'] : $row['temperature'] = "NULL"; (isset($stats['biasCurrent']))? $row['biasCurrent'] = $stats['biasCurrent'] : $row['biasCurrent'] = "NULL"; $data[] = "(".implode(",",$row).")".PHP_EOL; } $conn = $doctrine->getConnection(); $sql = "DELETE FROM `pon_port` WHERE device_server_id = {$deviceServerId} AND olt_device_id = {$oltDeviceId};"; $conn->query($sql); $conn->close(); $conn = $doctrine->getConnection(); $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).";"; //print_r($sql); $conn->query($sql); $conn->close(); } }