src/Centralstage/DeviceBundle/EventSubscriber/DevicepushSubscriber.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Centralstage\DeviceBundle\EventSubscriber;
  3. use App\Centralstage\DeviceBundle\Event\DevicepushEvent as Devicepush;
  4. use App\Centralstage\DeviceBundle\Models\DeviceDataModel;
  5. use App\Centralstage\CoreBundle\Utils\FileHelper;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class DevicepushSubscriber implements EventSubscriberInterface
  9. {
  10.     CONST collectionFolder "collection/";
  11.     private $fileSizeInBytes  5242880// 5mb in bytes
  12.     //private $fileSizeInBytes  = 1000000; // 1mb in bytes
  13.     public function onDevicePush(Devicepush $devicepush): void
  14.     {   
  15.         try{
  16.             //DATA
  17.             $EventData       $devicepush->getData();
  18.             $deviceDetails   $EventData["deviceDetails"];
  19.             $rawData         $EventData["rawData"];
  20.             $timestamp       $EventData["devicetimestamp"];
  21.             $currentDT       $EventData["currentDT"];
  22.             
  23.             unset($EventData); //free memory
  24.             if(is_string($rawData) && !empty($timestamp)){
  25.                 $NewFile_Bool true;
  26.                 $dataArray    json_decode($rawDatatrue);
  27.                 if(isset($dataArray["deviceStateTable"])){
  28.                     $deviceStateTable $dataArray["deviceStateTable"];
  29.                     $folderPath       DeviceDataModel::getFolderPath($deviceDetails$currentDT);
  30.                     $collectionDir    $folderPath.self::collectionFolder;
  31.                     if(!file_exists($collectionDir)){ //check dir existing 
  32.                         FileHelper::createDir($collectionDir);
  33.                     }else{
  34.                         /*** $lastDataFile    = isset($deviceDetails["deviceLastDataID"]) ? (file_exists($folderPath.$deviceDetails["deviceLastDataID"].".json") ? file_get_contents($folderPath.$deviceDetails["deviceLastDataID"].".json") : false)  : false;
  35.                         $currentFile     = is_string($lastDataFile) ? json_decode($lastDataFile, true) : false; ****/
  36.                         $currentFile     = !empty($deviceDetails["deviceLastDataID"]) ? $deviceDetails["deviceLastDataID"] : false;
  37.                         if(!empty($currentFile)){
  38.                             /** $currentFile    = isset($currentFile["timestamp"]) ? $currentFile["timestamp"] : 0; **/
  39.                             $selectedFile   $collectionDir.$currentFile.".json";
  40.                             $fileSize       FileHelper::getFileSize($selectedFile);
  41.                             if(!empty($fileSize) && ($fileSize $this->fileSizeInBytes)){ // less than 5mb size in bytes 
  42.                                 $NewFileName  $collectionDir.$timestamp.".json";
  43.                                 $data         json_decode(file_get_contents($selectedFiletrue), true);
  44.                                 if(!isset($data[$timestamp])){
  45.                                     $data[$timestamp] = [];
  46.                                 }
  47.                                 $data[$timestamp][] = $deviceStateTable;
  48.                                 $JsonData           json_encode($data);
  49.                                 file_put_contents($selectedFile$JsonData);
  50.                                 unset($data$JsonData); //free Storage 
  51.                                 FileHelper::renameFile($selectedFile$NewFileName);
  52.                                 $NewFile_Bool false;
  53.                             }
  54.                         }
  55.                     }
  56.                     if($NewFile_Bool){
  57.                         $result      = [$timestamp => [$deviceStateTable]];
  58.                         $result      json_encode($result);
  59.                         $outputfile  "{$collectionDir}{$timestamp}.json";
  60.                         file_put_contents($outputfile$result);
  61.                     }
  62.                 }
  63.             }
  64.         }catch (\Exception $exception) { $error $exception->getMessage(); }
  65.     }
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             'device.collection.push' => 'onDevicePush'
  70.         ];
  71.     }
  72. }