DeviceListener.php 6.3 KB

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