DeviceListener.php 3.9 KB

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