DeviceListener.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. class DeviceListener
  8. {
  9. /**
  10. * @var Webservice
  11. */
  12. private $webservice;
  13. /**
  14. * @var string
  15. */
  16. private $devicePostUrl;
  17. /**
  18. * @var string
  19. */
  20. private $deviceDeletePostUrl;
  21. /**
  22. * @var string
  23. */
  24. private $devicePutUrl;
  25. /**
  26. * @param Webservice $webservice
  27. * @param string $devicePostUrl
  28. * @param string $deviceDeletePostUrl
  29. */
  30. public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl)
  31. {
  32. $this->webservice = $webservice;
  33. $this->devicePostUrl = $devicePostUrl;
  34. $this->deviceDeletePostUrl = $deviceDeletePostUrl;
  35. $this->devicePutUrl = $devicePutUrl;
  36. $this->enabled = true;
  37. }
  38. /**
  39. * Se utiliza para las pruebas de phpunit
  40. * @param Webservice $webservice
  41. */
  42. public function setWebservice($webservice)
  43. {
  44. $this->webservice = $webservice;
  45. }
  46. /**
  47. * @param LifecycleEventArgs $args
  48. */
  49. public function postPersist(LifecycleEventArgs $args)
  50. {
  51. if (!$this->enabled) return;
  52. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  53. }
  54. /**
  55. * @param LifecycleEventArgs $args
  56. */
  57. public function preRemove(LifecycleEventArgs $args)
  58. {
  59. if (!$this->enabled) return;
  60. $entity = $args->getEntity();
  61. if ($entity instanceof DeviceInterface) {
  62. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  63. $data = array('id' => $deviceId);
  64. return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
  65. }
  66. }
  67. }
  68. /**
  69. * @param LifecycleEventArgs $args
  70. */
  71. public function postUpdate(LifecycleEventArgs $args)
  72. {
  73. if (!$this->enabled) return;
  74. $entity = $args->getEntity();
  75. if ($entity instanceof DeviceInterface) {
  76. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  77. $url = "{$this->devicePutUrl}{$deviceId}";
  78. $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
  79. } else {
  80. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  81. }
  82. }
  83. }
  84. /**
  85. * @param LifecycleEventArgs $args
  86. * @param string $url
  87. * @param string $method
  88. */
  89. private function send(LifecycleEventArgs $args, $url, $method)
  90. {
  91. if (!$this->enabled) return;
  92. $entity = $args->getEntity();
  93. if ($entity instanceof DeviceInterface) {
  94. $data = $entity->getDeviceData();
  95. $data = $this->addLocationData($entity, $data);
  96. return $this->webservice->makeGetRequest($url, $method, $data);
  97. }
  98. }
  99. /**
  100. * Agrega la ubicación de $entity si implementa LocationInterface
  101. *
  102. * @param Entity $entity
  103. * @param array $data
  104. *
  105. * @return array
  106. */
  107. private function addLocationData($entity, $data)
  108. {
  109. if (!$this->enabled) return;
  110. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  111. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  112. $extraData = json_decode($data['extraData'], true);
  113. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  114. $data['extraData'] = json_encode($extraData);
  115. }
  116. return $data;
  117. }
  118. /**
  119. * @param object $entity
  120. */
  121. private function getRemoteDeviceId($entity)
  122. {
  123. if (!$this->enabled) return;
  124. $deviceId = $entity->getId();
  125. $deviceType = get_class($entity);
  126. $tenancyId = $entity->getTenancyId();
  127. $filters = array('deviceId' => $deviceId, 'deviceType' => $deviceType, 'tenancyId' => $tenancyId);
  128. $data = $this->webservice->getData("device_post_url", $filters);
  129. // file_put_contents("/var/flowdat/error.log",json_encode($data));
  130. $deviceId = null;
  131. if (isset($data[0]))
  132. $deviceId = $data[0]['id'];
  133. return $deviceId;
  134. }
  135. function remoteCheck($enabled = true)
  136. {
  137. $this->enabled = $enabled;
  138. }
  139. }