DeviceListener.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace DeviceBundle\EventListener;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use DeviceBundle\Interfaces\DeviceInterface;
  5. use WebserviceBundle\Services\Webservice;
  6. use Buzz\Message\RequestInterface as HttpRequestInterface;
  7. use Symfony\Bundle\FrameworkBundle\Console\Application;
  8. use Symfony\Component\Console\Input\ArgvInput;
  9. use Symfony\Component\Console\Output\BufferedOutput;
  10. use Base\AdminBundle\Interfaces\SoftDeleteInterface;
  11. class DeviceListener
  12. {
  13. /**
  14. * @var Webservice
  15. */
  16. private $webservice;
  17. /**
  18. * @var string
  19. */
  20. private $devicePostUrl;
  21. /**
  22. * @var string
  23. */
  24. private $deviceDeletePostUrl;
  25. /**
  26. * @var string
  27. */
  28. private $devicePutUrl;
  29. private $serviceContainer;
  30. /**
  31. * @param Webservice $webservice
  32. * @param string $devicePostUrl
  33. * @param string $deviceDeletePostUrl
  34. * @param string $devicePutUrl
  35. * @param ContainerInterface $serviceContainer
  36. */
  37. public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl, $serviceContainer)
  38. {
  39. $this->webservice = $webservice;
  40. $this->devicePostUrl = $devicePostUrl;
  41. $this->deviceDeletePostUrl = $deviceDeletePostUrl;
  42. $this->devicePutUrl = $devicePutUrl;
  43. $this->enabled = true;
  44. $this->serviceContainer = $serviceContainer;
  45. }
  46. /**
  47. * @param LifecycleEventArgs $args
  48. */
  49. public function postPersist(LifecycleEventArgs $args)
  50. {
  51. if (!$this->enabled) {
  52. return;
  53. }
  54. $entity = $args->getEntity();
  55. if($entity instanceof SoftDeleteInterface && $entity->isDeleted()) {
  56. return;
  57. }
  58. if ($entity instanceof DeviceInterface) {
  59. $cmd_args = array(
  60. '--type:' . get_class($entity),
  61. '--id:' . $entity->getId(),
  62. );
  63. return $this->runCommand('device:crud', $cmd_args);
  64. }
  65. }
  66. /**
  67. * Corro el comando para crear el device por AMQP
  68. * @global kernel $kernel
  69. *
  70. * @param string $name
  71. * @param array $args
  72. *
  73. * @return string
  74. */
  75. public function runCommand($name, $cmd_args = array())
  76. {
  77. $kernel = $this->serviceContainer->get('kernel');
  78. $application = new Application($kernel);
  79. $application->setAutoExit(false);
  80. $args = [
  81. '',
  82. 'amqp:remote',
  83. $name,
  84. '--route=' . getenv("AMQP_KEY"),
  85. ];
  86. foreach ($cmd_args as $cmd_arg) {
  87. $args[] = "--args={$cmd_arg}";
  88. }
  89. $input = new ArgvInput($args);
  90. $output = new BufferedOutput();
  91. $application->run($input, $output);
  92. return $output->fetch();
  93. }
  94. /**
  95. * @param LifecycleEventArgs $args
  96. *
  97. * @return mixed
  98. */
  99. public function preRemove(LifecycleEventArgs $args)
  100. {
  101. if (!$this->enabled) {
  102. return;
  103. }
  104. $entity = $args->getEntity();
  105. if ($entity instanceof DeviceInterface) {
  106. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  107. $cmd_args = array(
  108. '--type:' . get_class($entity),
  109. '--id:' . $entity->getId(),
  110. '--url:' . $this->deviceDeletePostUrl . $deviceId,
  111. '--method:' . HttpRequestInterface::METHOD_DELETE,
  112. );
  113. return $this->runCommand('device:crud', $cmd_args);
  114. }
  115. }
  116. return;
  117. }
  118. /**
  119. * @param LifecycleEventArgs $args
  120. */
  121. public function postUpdate(LifecycleEventArgs $args)
  122. {
  123. if (!$this->enabled) {
  124. return;
  125. }
  126. $entity = $args->getEntity();
  127. if($entity instanceof SoftDeleteInterface && $entity->isDeleted()) {
  128. return;
  129. }
  130. if ($entity instanceof DeviceInterface) {
  131. $cmd_args = array(
  132. '--type:' . get_class($entity),
  133. '--id:' . $entity->getId(),
  134. );
  135. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  136. $cmd_args[] = "--url:{$this->devicePutUrl}{$deviceId}";
  137. $cmd_args[] = '--method:' . HttpRequestInterface::METHOD_PUT;
  138. }
  139. return $this->runCommand('device:crud', $cmd_args);
  140. }
  141. return;
  142. }
  143. /**
  144. * @param DeviceInterface $entity
  145. * @param string $url
  146. * @param string $method
  147. * @param array $credentials username y password
  148. *
  149. * @return mixed
  150. */
  151. public function send($entity, $url, $method, $credentials = array())
  152. {
  153. if (!$this->enabled) {
  154. return;
  155. }
  156. if ($entity instanceof DeviceInterface) {
  157. $data = $entity->getDeviceData();
  158. $data = $this->addLocationData($entity, $data);
  159. $data = $this->addExtraDataFields($entity, $data);
  160. return $this->webservice->makeGetRequest($url, $method, $data, $credentials);
  161. }
  162. }
  163. /**
  164. * Agrega la ubicación de $entity si implementa LocationInterface
  165. *
  166. * @param Entity $entity
  167. * @param array $data
  168. *
  169. * @return array
  170. */
  171. private function addLocationData($entity, $data)
  172. {
  173. if (!$this->enabled) {
  174. return;
  175. }
  176. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  177. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  178. $extraData = json_decode($data['extraData'], true);
  179. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  180. $data['extraData'] = json_encode($extraData);
  181. }
  182. return $data;
  183. }
  184. private function addExtraDataFields($entity, $data)
  185. {
  186. if(!$this->enabled){
  187. return;
  188. }
  189. $extraData = json_decode($data['extraData'], true);
  190. $className = get_class($entity);
  191. if($className == 'FTTHBundle\Entity\OLT' && !array_key_exists('timeOltScan', $extraData)){
  192. $extraData['timeOltScan'] = 10;
  193. }else if($className == 'FTTHBundle\Entity\NAS' && !array_key_exists('executeSnmp', $extraData)){
  194. $extraData['executeSnmp'] = true;
  195. }
  196. $data['extraData'] = json_encode($extraData);
  197. return $data;
  198. }
  199. /**
  200. * @param object $entity
  201. *
  202. * @return mixed
  203. */
  204. private function getRemoteDeviceId($entity)
  205. {
  206. if (!$this->enabled) {
  207. return;
  208. }
  209. $deviceId = $entity->getId();
  210. $deviceType = get_class($entity);
  211. $tenancyId = $entity->getTenancyId();
  212. $filters = array(
  213. 'deviceId' => $deviceId,
  214. 'deviceType' => $deviceType,
  215. 'tenancyId' => $tenancyId
  216. );
  217. $data = $this->webservice->getData("api_device_post_url", $filters);
  218. $deviceId = null;
  219. if (isset($data[0])) {
  220. $deviceId = $data[0]['id'];
  221. }
  222. return $deviceId;
  223. }
  224. /**
  225. * @param boolean $enabled
  226. */
  227. function remoteCheck($enabled = true)
  228. {
  229. $this->enabled = $enabled;
  230. }
  231. /**
  232. * Se utiliza para las pruebas de phpunit
  233. * @param Webservice $webservice
  234. */
  235. public function setWebservice($webservice)
  236. {
  237. $this->webservice = $webservice;
  238. }
  239. }