DeviceListener.php 6.3 KB

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