DeviceListener.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\ArrayInput;
  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. $input = new ArrayInput(array(
  81. 'command' => 'amqp:remote',
  82. 'name' => $name,
  83. '--args' => $cmd_args,
  84. '--route' => getenv("AMQP_KEY"),
  85. ));
  86. $output = new BufferedOutput();
  87. $application->run($input, $output);
  88. return $output->fetch();
  89. }
  90. /**
  91. * @param LifecycleEventArgs $args
  92. *
  93. * @return mixed
  94. */
  95. public function preRemove(LifecycleEventArgs $args)
  96. {
  97. if (!$this->enabled) {
  98. return;
  99. }
  100. $entity = $args->getEntity();
  101. if ($entity instanceof DeviceInterface) {
  102. var_dump(get_class($entity).PHP_EOL);
  103. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  104. $cmd_args = array(
  105. '--type:' . get_class($entity),
  106. '--id:' . $entity->getId(),
  107. '--url:' . $this->deviceDeletePostUrl . $deviceId,
  108. '--method:' . HttpRequestInterface::METHOD_DELETE,
  109. );
  110. return $this->runCommand('device:crud', $cmd_args);
  111. }
  112. }
  113. return;
  114. }
  115. /**
  116. * @param LifecycleEventArgs $args
  117. */
  118. public function postUpdate(LifecycleEventArgs $args)
  119. {
  120. if (!$this->enabled) {
  121. return;
  122. }
  123. $entity = $args->getEntity();
  124. if($entity instanceof SoftDeleteInterface && $entity->isDeleted()) {
  125. return;
  126. }
  127. if ($entity instanceof DeviceInterface) {
  128. $cmd_args = array(
  129. '--type:' . get_class($entity),
  130. '--id:' . $entity->getId(),
  131. );
  132. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  133. $cmd_args[] = "--url:{$this->devicePutUrl}{$deviceId}";
  134. $cmd_args[] = '--method:' . HttpRequestInterface::METHOD_PUT;
  135. }
  136. return $this->runCommand('device:crud', $cmd_args);
  137. }
  138. return;
  139. }
  140. /**
  141. * @param DeviceInterface $entity
  142. * @param string $url
  143. * @param string $method
  144. * @param array $credentials username y password
  145. *
  146. * @return mixed
  147. */
  148. public function send($entity, $url, $method, $credentials = array())
  149. {
  150. if (!$this->enabled) {
  151. return;
  152. }
  153. if ($entity instanceof DeviceInterface) {
  154. $data = $entity->getDeviceData();
  155. $data = $this->addLocationData($entity, $data);
  156. return $this->webservice->makeGetRequest($url, $method, $data, $credentials);
  157. }
  158. }
  159. /**
  160. * Agrega la ubicación de $entity si implementa LocationInterface
  161. *
  162. * @param Entity $entity
  163. * @param array $data
  164. *
  165. * @return array
  166. */
  167. private function addLocationData($entity, $data)
  168. {
  169. if (!$this->enabled) {
  170. return;
  171. }
  172. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  173. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  174. $extraData = json_decode($data['extraData'], true);
  175. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  176. $data['extraData'] = json_encode($extraData);
  177. }
  178. return $data;
  179. }
  180. /**
  181. * @param object $entity
  182. *
  183. * @return mixed
  184. */
  185. private function getRemoteDeviceId($entity)
  186. {
  187. if (!$this->enabled) {
  188. return;
  189. }
  190. $deviceId = $entity->getId();
  191. $deviceType = get_class($entity);
  192. $tenancyId = $entity->getTenancyId();
  193. $filters = array(
  194. 'deviceId' => $deviceId,
  195. 'deviceType' => $deviceType,
  196. 'tenancyId' => $tenancyId
  197. );
  198. $data = $this->webservice->getData("device_post_url", $filters);
  199. $deviceId = null;
  200. if (isset($data[0])) {
  201. $deviceId = $data[0]['id'];
  202. }
  203. return $deviceId;
  204. }
  205. /**
  206. * @param boolean $enabled
  207. */
  208. function remoteCheck($enabled = true)
  209. {
  210. $this->enabled = $enabled;
  211. }
  212. /**
  213. * Se utiliza para las pruebas de phpunit
  214. * @param Webservice $webservice
  215. */
  216. public function setWebservice($webservice)
  217. {
  218. $this->webservice = $webservice;
  219. }
  220. }