DeviceListener.php 6.3 KB

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