|
@@ -6,6 +6,10 @@ use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
use DeviceBundle\Interfaces\DeviceInterface;
|
|
|
use WebserviceBundle\Services\Webservice;
|
|
|
use Buzz\Message\RequestInterface as HttpRequestInterface;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Console\Application;
|
|
|
+use Symfony\Component\Console\Input\ArrayInput;
|
|
|
+use Symfony\Component\Console\Output\BufferedOutput;
|
|
|
+use Symfony\Component\HttpKernel\KernelInterface;
|
|
|
|
|
|
class DeviceListener
|
|
|
{
|
|
@@ -51,24 +55,62 @@ class DeviceListener
|
|
|
*/
|
|
|
public function postPersist(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
|
|
|
+ $entity = $args->getEntity();
|
|
|
+ $cmd_args = array(
|
|
|
+ 'type:' . get_class($entity),
|
|
|
+ '--id:' . $entity->getId(),
|
|
|
+ );
|
|
|
+ $this->runCommand('device:create', $cmd_args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Corro el comando para crear el device por AMQP
|
|
|
+ * @global kernel $kernel
|
|
|
+ *
|
|
|
+ * @param string $name
|
|
|
+ * @param array $args
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function runCommand($name, $cmd_args = array())
|
|
|
+ {
|
|
|
+ global $kernel;
|
|
|
+
|
|
|
+ $application = new Application($kernel);
|
|
|
+ $application->setAutoExit(false);
|
|
|
+
|
|
|
+ $input = new ArrayInput(array(
|
|
|
+ 'command' => 'amqp:remote',
|
|
|
+ 'name' => $name,
|
|
|
+ '--args' => $cmd_args,
|
|
|
+ ));
|
|
|
+
|
|
|
+ $output = new BufferedOutput();
|
|
|
+ $application->run($input, $output);
|
|
|
+
|
|
|
+ return $output->fetch();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param LifecycleEventArgs $args
|
|
|
+ *
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function preRemove(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
$entity = $args->getEntity();
|
|
|
if ($entity instanceof DeviceInterface) {
|
|
|
if ($deviceId = $this->getRemoteDeviceId($entity)) {
|
|
|
-
|
|
|
$data = array('id' => $deviceId);
|
|
|
+
|
|
|
return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
|
|
|
}
|
|
|
}
|
|
@@ -79,36 +121,40 @@ class DeviceListener
|
|
|
*/
|
|
|
public function postUpdate(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
$entity = $args->getEntity();
|
|
|
if ($entity instanceof DeviceInterface) {
|
|
|
if ($deviceId = $this->getRemoteDeviceId($entity)) {
|
|
|
$url = "{$this->devicePutUrl}{$deviceId}";
|
|
|
- $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
|
|
|
+ $this->send($entity, $url, HttpRequestInterface::METHOD_PUT);
|
|
|
} else {
|
|
|
- $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
|
|
|
+ $this->send($entity, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param LifecycleEventArgs $args
|
|
|
+ * @param DeviceInterface $entity
|
|
|
* @param string $url
|
|
|
* @param string $method
|
|
|
+ * @param array $credentials username y password
|
|
|
+ *
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- private function send(LifecycleEventArgs $args, $url, $method)
|
|
|
+ public function send($entity, $url, $method, $credentials = array())
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- $entity = $args->getEntity();
|
|
|
if ($entity instanceof DeviceInterface) {
|
|
|
$data = $entity->getDeviceData();
|
|
|
$data = $this->addLocationData($entity, $data);
|
|
|
|
|
|
- return $this->webservice->makeGetRequest($url, $method, $data);
|
|
|
+ return $this->webservice->makeGetRequest($url, $method, $data, $credentials);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -122,7 +168,9 @@ class DeviceListener
|
|
|
*/
|
|
|
private function addLocationData($entity, $data)
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
$locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
|
|
|
if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
|
|
@@ -136,17 +184,19 @@ class DeviceListener
|
|
|
|
|
|
/**
|
|
|
* @param object $entity
|
|
|
+ *
|
|
|
* @return mixed
|
|
|
*/
|
|
|
private function getRemoteDeviceId($entity)
|
|
|
{
|
|
|
- if (!$this->enabled) return;
|
|
|
+ if (!$this->enabled) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
$deviceId = $entity->getId();
|
|
|
$deviceType = get_class($entity);
|
|
|
$tenancyId = $entity->getTenancyId();
|
|
|
|
|
|
-
|
|
|
$filters = array('deviceId' => $deviceId, 'deviceType' => $deviceType, 'tenancyId' => $tenancyId);
|
|
|
$data = $this->webservice->getData("device_post_url", $filters);
|
|
|
|
|
@@ -159,6 +209,9 @@ class DeviceListener
|
|
|
return $deviceId;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param boolean $enabled
|
|
|
+ */
|
|
|
function remoteCheck($enabled = true)
|
|
|
{
|
|
|
$this->enabled = $enabled;
|