DeviceListener.php 5.8 KB

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