src/Controller/HomeController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Device;
  4. use App\Entity\DeviceHistory;
  5. use App\Entity\Owner;
  6. use App\Entity\Telemetry;
  7. use App\Message\DeviceAssignedMessage;
  8. use App\Repository\AlarmRepository;
  9. use App\Repository\DeviceRepository;
  10. use App\Repository\DeviceTypeRepository;
  11. use App\Repository\InventoryRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Messenger\MessageBusInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class HomeController extends AbstractController
  18. {
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     public function __construct(EntityManagerInterface $emDeviceRepository $deviceRepositoryMessageBusInterface $bus)
  24.     {
  25.         $this->em=$em;
  26.         $this->bus=$bus;
  27.         $this->deviceRepository=$deviceRepository;
  28.     }
  29.     /**
  30.      * @Route("/", name="index")
  31.      */
  32.     public function index(DeviceRepository $deviceRepositoryAlarmRepository $alarmRepository)
  33.     {
  34.         $devices=$deviceRepository->totalDevices();
  35.         $alarms=$alarmRepository->totalDevices();
  36.         $tracked=10;
  37.         return $this->render('home.html.twig', ['devices' => $devices,'alarms'=>$alarms,'tracked'=>$tracked]);
  38.     }
  39.     /**
  40.      * @Route("/home", name="home")
  41.      */
  42.     public function home(DeviceTypeRepository $deviceTypeRepository): Response
  43.     {
  44.         //todo remove all test functions on this page
  45.         $deviceType=$deviceTypeRepository->findAll();
  46.         $device = new Device();
  47.         $device->setSerial(date('U'));
  48.         $device->setDeviceType($deviceType[0]);
  49.         $this->em->persist($device);
  50.         $this->em->flush();
  51.         $devices=$this->deviceRepository->findAll();
  52.         foreach ($devices as $d) {
  53.             $dev[]=['id'=>$d->getId(),'serial'=>$d->getSerial()];
  54.         }
  55.         return $this->json($dev);
  56.         return $this->json([
  57.             'message' => 'Welcome to your new controller!',
  58.             'path' => 'src/Controller/HomeController.php',
  59.         ]);
  60.     }
  61.     /**
  62.      * @Route("/home/{id}",name="get_device")
  63.      * @param Device $device
  64.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  65.      */
  66.     public function getDevice(DeviceRepository $deviceRepository$id): Response
  67.     {
  68.         //todo remove function, replaced by listener
  69.         $device=$deviceRepository->find($id);
  70.         $telemetry = new Telemetry();
  71.         $telemetry->setDevice($device);
  72.         $telemetry->setReceivedData('{"0.Name":"MAIN","0.m3":0.350,"0.Type":"LV","0.Itime":0,"0.Ftime":0,"Ch":0}');
  73.         $this->em->persist($telemetry);
  74.         $this->em->flush();
  75.         return $this->json(
  76.             [
  77.                 'device'=>$device->getId(),
  78.                 'telemetry'=>serialize($telemetry)
  79.         ]
  80.         );
  81.     }
  82.     /**
  83.      * @Route("/assign/{serial}", name="home")
  84.      * @param InventoryRepository $inventoryRepository
  85.      */
  86.     public function assignDevice($serialInventoryRepository $inventoryRepositoryDeviceRepository $deviceRepository)
  87.     {
  88.         $inventory=$inventoryRepository->findOneBySerial($serial);
  89.         if (!$inventory) {
  90.             return $this->json(['not found'], 404);
  91.         }
  92.         $device=$deviceRepository->findOneBySerial($serial);
  93.         if ($device) {
  94.             return $this->json(['unavailable'], 403);
  95.         }
  96.         $device = new Device();
  97.         $device->setDeviceType($inventory->getDeviceType());
  98.         $device->setSerial($serial);
  99.         $device->setShortName('tim: '.date('U'));
  100.         $this->em->persist($device);
  101.         $this->em->flush();
  102.         $this->bus->dispatch(new DeviceAssignedMessage($inventory->getIccid(), $serial));
  103.         return $this->json(['success'], 200);
  104.     }
  105. }