<?php
namespace App\Controller;
use App\Entity\Device;
use App\Entity\DeviceHistory;
use App\Entity\Owner;
use App\Entity\Telemetry;
use App\Message\DeviceAssignedMessage;
use App\Repository\AlarmRepository;
use App\Repository\DeviceRepository;
use App\Repository\DeviceTypeRepository;
use App\Repository\InventoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em, DeviceRepository $deviceRepository, MessageBusInterface $bus)
{
$this->em=$em;
$this->bus=$bus;
$this->deviceRepository=$deviceRepository;
}
/**
* @Route("/", name="index")
*/
public function index(DeviceRepository $deviceRepository, AlarmRepository $alarmRepository)
{
$devices=$deviceRepository->totalDevices();
$alarms=$alarmRepository->totalDevices();
$tracked=10;
return $this->render('home.html.twig', ['devices' => $devices,'alarms'=>$alarms,'tracked'=>$tracked]);
}
/**
* @Route("/home", name="home")
*/
public function home(DeviceTypeRepository $deviceTypeRepository): Response
{
//todo remove all test functions on this page
$deviceType=$deviceTypeRepository->findAll();
$device = new Device();
$device->setSerial(date('U'));
$device->setDeviceType($deviceType[0]);
$this->em->persist($device);
$this->em->flush();
$devices=$this->deviceRepository->findAll();
foreach ($devices as $d) {
$dev[]=['id'=>$d->getId(),'serial'=>$d->getSerial()];
}
return $this->json($dev);
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/HomeController.php',
]);
}
/**
* @Route("/home/{id}",name="get_device")
* @param Device $device
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getDevice(DeviceRepository $deviceRepository, $id): Response
{
//todo remove function, replaced by listener
$device=$deviceRepository->find($id);
$telemetry = new Telemetry();
$telemetry->setDevice($device);
$telemetry->setReceivedData('{"0.Name":"MAIN","0.m3":0.350,"0.Type":"LV","0.Itime":0,"0.Ftime":0,"Ch":0}');
$this->em->persist($telemetry);
$this->em->flush();
return $this->json(
[
'device'=>$device->getId(),
'telemetry'=>serialize($telemetry)
]
);
}
/**
* @Route("/assign/{serial}", name="home")
* @param InventoryRepository $inventoryRepository
*/
public function assignDevice($serial, InventoryRepository $inventoryRepository, DeviceRepository $deviceRepository)
{
$inventory=$inventoryRepository->findOneBySerial($serial);
if (!$inventory) {
return $this->json(['not found'], 404);
}
$device=$deviceRepository->findOneBySerial($serial);
if ($device) {
return $this->json(['unavailable'], 403);
}
$device = new Device();
$device->setDeviceType($inventory->getDeviceType());
$device->setSerial($serial);
$device->setShortName('tim: '.date('U'));
$this->em->persist($device);
$this->em->flush();
$this->bus->dispatch(new DeviceAssignedMessage($inventory->getIccid(), $serial));
return $this->json(['success'], 200);
}
}