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