DeviceListener.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $this->runCommand('device:create', $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. $data = array('id' => $deviceId);
  98. return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
  99. }
  100. }
  101. }
  102. /**
  103. * @param LifecycleEventArgs $args
  104. */
  105. public function postUpdate(LifecycleEventArgs $args)
  106. {
  107. if (!$this->enabled) {
  108. return;
  109. }
  110. $entity = $args->getEntity();
  111. if ($entity instanceof DeviceInterface) {
  112. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  113. $url = "{$this->devicePutUrl}{$deviceId}";
  114. $this->send($entity, $url, HttpRequestInterface::METHOD_PUT);
  115. } else {
  116. $this->send($entity, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  117. }
  118. }
  119. }
  120. /**
  121. * @param DeviceInterface $entity
  122. * @param string $url
  123. * @param string $method
  124. * @param array $credentials username y password
  125. *
  126. * @return mixed
  127. */
  128. public function send($entity, $url, $method, $credentials = array())
  129. {
  130. if (!$this->enabled) {
  131. return;
  132. }
  133. if ($entity instanceof DeviceInterface) {
  134. $data = $entity->getDeviceData();
  135. $data = $this->addLocationData($entity, $data);
  136. return $this->webservice->makeGetRequest($url, $method, $data, $credentials);
  137. }
  138. }
  139. /**
  140. * Agrega la ubicación de $entity si implementa LocationInterface
  141. *
  142. * @param Entity $entity
  143. * @param array $data
  144. *
  145. * @return array
  146. */
  147. private function addLocationData($entity, $data)
  148. {
  149. if (!$this->enabled) {
  150. return;
  151. }
  152. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  153. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  154. $extraData = json_decode($data['extraData'], true);
  155. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  156. $data['extraData'] = json_encode($extraData);
  157. }
  158. return $data;
  159. }
  160. /**
  161. * @param object $entity
  162. *
  163. * @return mixed
  164. */
  165. private function getRemoteDeviceId($entity)
  166. {
  167. if (!$this->enabled) {
  168. return;
  169. }
  170. $deviceId = $entity->getId();
  171. $deviceType = get_class($entity);
  172. $tenancyId = $entity->getTenancyId();
  173. $filters = array('deviceId' => $deviceId, 'deviceType' => $deviceType, 'tenancyId' => $tenancyId);
  174. $data = $this->webservice->getData("device_post_url", $filters);
  175. // file_put_contents("/var/flowdat/error.log",json_encode($data));
  176. $deviceId = null;
  177. if (isset($data[0]))
  178. $deviceId = $data[0]['id'];
  179. return $deviceId;
  180. }
  181. /**
  182. * @param boolean $enabled
  183. */
  184. function remoteCheck($enabled = true)
  185. {
  186. $this->enabled = $enabled;
  187. }
  188. /**
  189. * Se utiliza para las pruebas de phpunit
  190. * @param Webservice $webservice
  191. */
  192. public function setWebservice($webservice)
  193. {
  194. $this->webservice = $webservice;
  195. }
  196. }