DeviceListener.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @param LifecycleEventArgs $args
  40. */
  41. public function postPersist(LifecycleEventArgs $args)
  42. {
  43. if(!$this->enabled) return ;
  44. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  45. }
  46. /**
  47. * @param LifecycleEventArgs $args
  48. */
  49. public function preRemove(LifecycleEventArgs $args)
  50. {
  51. if(!$this->enabled) return ;
  52. $entity = $args->getEntity();
  53. if ($entity instanceof DeviceInterface) {
  54. if($deviceId = $this->getRemoteDeviceId($entity)) {
  55. $data = array('id' => $deviceId);
  56. return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
  57. }
  58. }
  59. }
  60. /**
  61. * @param LifecycleEventArgs $args
  62. */
  63. public function postUpdate(LifecycleEventArgs $args)
  64. {
  65. if(!$this->enabled) return ;
  66. $entity = $args->getEntity();
  67. if ($entity instanceof DeviceInterface) {
  68. if($deviceId = $this->getRemoteDeviceId($entity)) {
  69. $url = "{$this->devicePutUrl}{$deviceId}";
  70. $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
  71. } else {
  72. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  73. }
  74. }
  75. }
  76. /**
  77. * @param LifecycleEventArgs $args
  78. * @param string $url
  79. * @param string $method
  80. */
  81. private function send(LifecycleEventArgs $args, $url, $method)
  82. {
  83. if(!$this->enabled) return ;
  84. $entity = $args->getEntity();
  85. if ($entity instanceof DeviceInterface) {
  86. $data = $entity->getDeviceData();
  87. $data = $this->addLocationData($entity, $data);
  88. return $this->webservice->makeGetRequest($url, $method, $data);
  89. }
  90. }
  91. /**
  92. * Agrega la ubicación de $entity si implementa LocationInterface
  93. *
  94. * @param Entity $entity
  95. * @param array $data
  96. *
  97. * @return array
  98. */
  99. private function addLocationData($entity, $data)
  100. {
  101. $this->enabled = true;
  102. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  103. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  104. $extraData = json_decode($data['extraData'], true);
  105. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  106. $data['extraData'] = json_encode($extraData);
  107. }
  108. return $data;
  109. }
  110. /**
  111. * @param object $entity
  112. */
  113. private function getRemoteDeviceId($entity)
  114. {
  115. $this->enabled = true;
  116. $deviceId = $entity->getId();
  117. $deviceType = get_class($entity);
  118. $tenancyId = $entity->getTenancyId();
  119. $filters = array('deviceId'=>$deviceId,'deviceType'=>$deviceType,'tenancyId'=>$tenancyId);
  120. $data = $this->webservice->getData("device_post_url",$filters);
  121. // file_put_contents("/var/flowdat/error.log",json_encode($data));
  122. $deviceId = null;
  123. if(isset($data[0]))
  124. $deviceId = $data[0]['id'];
  125. return $deviceId;
  126. }
  127. function remoteCheck($enabled = true){
  128. $this->enabled = true;
  129. }
  130. }