|
@@ -0,0 +1,105 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace StatsBundle\Services;
|
|
|
+
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use Doctrine\ORM\EntityRepository;
|
|
|
+use StatsBundle\Entity\StatsDevice;
|
|
|
+use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
+use WebserviceBundle\Services\Webservice;
|
|
|
+
|
|
|
+class DeviceManager
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var EntityManagerInterface
|
|
|
+ */
|
|
|
+ private $em;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ValidatorInterface
|
|
|
+ */
|
|
|
+ private $validator;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var EntityRepository
|
|
|
+ */
|
|
|
+ private $deviceServerRepository;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var EntityRepository
|
|
|
+ */
|
|
|
+ private $statsDeviceRepository;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var Webservice
|
|
|
+ */
|
|
|
+ private $webservice;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param EntityManagerInterface $em
|
|
|
+ * @param ValidatorInterface $validator
|
|
|
+ * @param Webservice $webservice
|
|
|
+ */
|
|
|
+ public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, Webservice $webservice)
|
|
|
+ {
|
|
|
+ $this->em = $em;
|
|
|
+ $this->deviceServerRepository = $em->getRepository('StatsBundle:DeviceServer');
|
|
|
+ $this->statsDeviceRepository = $em->getRepository('StatsBundle:StatsDevice');
|
|
|
+ $this->validator = $validator;
|
|
|
+ $this->webservice = $webservice;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDevices()
|
|
|
+ {
|
|
|
+ $devices = array();
|
|
|
+ $deviceServers = $this->deviceServerRepository->findAll();
|
|
|
+ foreach ($deviceServers as $deviceServer) {
|
|
|
+ $deviceTypes = $deviceServer->getDeviceTypes();
|
|
|
+ foreach ($deviceTypes as $deviceType) {
|
|
|
+ $filters = array(
|
|
|
+ 'deviceType' => $deviceType,
|
|
|
+ );
|
|
|
+ $remoteDevices = $this->webservice->get($deviceServer->getUrl(), $filters);
|
|
|
+ foreach ($remoteDevices as $remoteDevice) {
|
|
|
+ $device = $this->create($remoteDevice['deviceType'], $remoteDevice['deviceId'], $remoteDevice['ip']);
|
|
|
+ if ($device) {
|
|
|
+ $devices[] = $device;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $devices;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $deviceType
|
|
|
+ * @param int $deviceId
|
|
|
+ * @param string $ip
|
|
|
+ *
|
|
|
+ * @return StatsDevice
|
|
|
+ */
|
|
|
+ public function create($deviceType, $deviceId, $ip)
|
|
|
+ {
|
|
|
+ $device = null;
|
|
|
+ if (is_null($this->statsDeviceRepository->findOneByIp($ip))) {
|
|
|
+ $device = new StatsDevice();
|
|
|
+ $device->setDeviceType($deviceType);
|
|
|
+ $device->setDeviceId($deviceId);
|
|
|
+ $device->setIp($ip);
|
|
|
+
|
|
|
+ if ($this->validator->validate($device)->count() == 0) {
|
|
|
+ $this->em->persist($device);
|
|
|
+ $this->em->flush($device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $device;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|