File manager - Edit - /var/www/html/spdc/api/app/Console/Commands/GenerateDataFromCemadenCommand.php
Back
<?php namespace App\Console\Commands; use App\Models\Alert; use App\Models\Monitoring; use Carbon\Carbon; use DateTime; use Exception; use Illuminate\Console\Command; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class GenerateDataFromCemadenCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'generate:data:cemaden'; /** * The console command description. * * @var string */ protected $description = 'This command receives data from the Cemaden API to generate monitoring data and alerts.'; /** * Execute the console command. * * @return int */ public function handle() { try { $data = $this->getCemadenData(); $monitoring = $this->filterCemadenData($data); $alerts = $this->generateMonitoringData($monitoring); $this->generateAlerts($alerts); $this->info('SUCCESS'); return Command::SUCCESS; } catch (Exception $exception) { $this->error($exception->getMessage()); Log::error($exception->getMessage()); } } private function getCemadenData() { try { $response = Http::withHeaders([ 'Accept' => 'application/json', ])->get('http://200.133.244.152/WsParceiro/CEMADEN/resources/parceiros/RJ/1'); return $response['cemaden']; } catch (Exception $exception) { throw new Exception($exception->getMessage(), JsonResponse::HTTP_BAD_REQUEST); } } private function filterCemadenData(array $cemadenData) { $names = []; foreach ($cemadenData as $data) { if (strcasecmp($data['cidade'], 'ANGRA DOS REIS') == 0) { $angra[] = $data; if (!in_array($data['nome'], $names)) { $names[] = $data['nome']; } } } if (count($angra) == 0) { throw new Exception('No measurements', JsonResponse::HTTP_INTERNAL_SERVER_ERROR); } $results = []; foreach ($names as $name) { $comparison = []; foreach ($angra as $data) { if ($name == $data['nome']) { if (count($comparison) > 0) { if ($comparison['chuva'] < $data['chuva']) { $comparison = $data; } } else { $comparison = $data; } } } $results[] = $comparison; } return $results; } private function generateMonitoringData(array $data) { $alertData = []; $today = Carbon::today(); $today = Carbon::parse($today)->format('Y-m-d'); foreach ($data as $measure) { $monitorign = Monitoring::where('name', '=', $measure['nome']) ->orderBy('id', 'DESC') ->first(); if ($monitorign != null) { $this->compareMonitoring($measure, $monitorign); } else { $this->createMonitorging($measure); } $value1 = $measure['chuva'] * 100; if ($value1 > 35) { $alertData[] = $measure; } } return $alertData; } private function generateAlerts(array $alerts) { foreach ($alerts as $alert) { $alertExists = $this->checkAlert($alert); if ($alertExists) { $this->compareNewAndOldAlert($alert); } else { $time = $alert['chuva'] * 100; if ($time >= 35 && $time < 85) { $this->createNewAlert($alert, 'alert', '1hr'); } if ($time >= 85 && $time < 205) { $this->createNewAlert($alert, 'alert', '24hr'); } if ($time >= 205) { $this->createNewAlert($alert, 'alert', '96hr'); } } } } private function createMonitorging(array $measure) { $measurementTime = rtrim($measure['dataHora'], ".0"); $format = Carbon::createFromFormat('Y-m-d H:i:s', $measurementTime . '00'); $teste = new Monitoring(); $teste->name = $measure['nome']; $teste->latitude = $measure['latitude']; $teste->longitude = $measure['longitude']; $teste->value = $measure['chuva']; $teste->measurement_time = $format; $teste->save(); } private function compareMonitoring(array $measure, Monitoring $monitorign) { $value1 = $measure['chuva'] * 100; $value2 = $monitorign->value * 100; if ($value1 > $value2) { if ($value1 >= 10 && $value2 <= 10) { $this->createMonitorging($measure); } else if ($value1 >= 30 && $value2 <= 30 && $value2 >= 10) { $this->createMonitorging($measure); } else if ($value1 >= 70 && $value2 <= 70 && $value2 >= 30) { $this->createMonitorging($measure); } } if ($value1 < $value2) { if ($value1 < 70 && $value2 >= 70) { $this->createMonitorging($measure); } else if ($value1 < 30 && $value2 >= 30) { $this->createMonitorging($measure); } else if ($value1 < 10 && $value2 >= 10 && $value2 <= 30) { $this->createMonitorging($measure); } } } private function checkAlert(array $newAlert) { $result = Alert::where('name', '=', $newAlert['nome']) ->where('is_finished', '=', false) ->orderBy('id', 'DESC') ->first(); if ($result == null) { return false; } $hasBeenExpired = $this->checkExpirationDate($result, $newAlert); if ($hasBeenExpired) { return false; } return true; } private function checkExpirationDate(Alert $alert) { $createdAt = $alert->created_at; $now = Carbon::now(); $interval = $createdAt->diff($now); if ($alert->duration == '1h' && $interval->days == 0 && $interval->h >= 1) { $alert->is_finished = true; $alert->save(); return true; } if ($alert->duration == '24h' && $interval->days > 0) { $alert->is_finished = true; $alert->save(); return true; } if ($alert->duration == '96h' && $interval->days > 4) { $alert->is_finished = true; $alert->save(); return true; } return false; } private function compareNewAndOldAlert(array $newAlert) { $oldAlert = Alert::where('name', '=', $newAlert['nome']) ->where('is_finished', '=', false) ->orderBy('id', 'DESC') ->first(); if ($oldAlert->type == 'alert') { $value1 = $newAlert['chuva'] * 100; $value2 = $oldAlert->value * 100; if ($value1 > $value2) { if ($value1 >= 50 && $value1 < 100) { $this->createNewAlert($newAlert, 'evacuation', '1hr'); } if ($value1 >= 100 && $value1 < 220) { $this->createNewAlert($newAlert, 'evacuation', '24hr'); } if ($value1 >= 220) { $this->createNewAlert($newAlert, 'evacuation', '96hr'); } } } } private function createNewAlert(array $alert, string $type, string $duration) { $measurementTime = rtrim($alert['dataHora'], ".0"); $format = Carbon::createFromFormat('Y-m-d H:i:s', $measurementTime . '00'); Alert::create([ 'latitude' => $alert['latitude'], 'longitude' => $alert['longitude'], 'name' => $alert['nome'], 'value' => $alert['chuva'], 'measurement_time' => $format, 'type' => $type, 'duration' => $duration, 'is_finished' => false, ]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings