diff --git a/app/Jobs/UpdateWeatherDataDaily.php b/app/Jobs/UpdateWeatherDataDaily.php new file mode 100644 index 0000000..54a6fd5 --- /dev/null +++ b/app/Jobs/UpdateWeatherDataDaily.php @@ -0,0 +1,88 @@ + [ + 'User-Agent' => 'Fogos.pt/3.0', + ], + 'verify' => false, + ]; + + try{ + $client = new \GuzzleHttp\Client(); + $res = $client->request('GET', $url, $options); + + $data = $res->getBody()->getContents(); + } + catch(\GuzzleHttp\Exception\RequestException $e) { + Log::error('Error occurred in request.', ['url' => $url, 'statusCode' => $e->getCode(), 'message' => $e->getMessage()]); + return; + } + + $data = json_decode($data); + + foreach($data as $date => $stations){ + $ddate = Carbon::parse($date); + print_r($ddate); + foreach($stations as $stationId => $d){ + + if($d){ + $weatherData = WeatherDataDaily::where('stationId', $stationId) + ->where('date', $ddate) + ->get(); + + if(!isset($weatherData[0])){ + $weatherData = new WeatherDataDaily(); + + $weatherData->hum_min = $d->hum_min; + $weatherData->idDireccVento = $d->idDireccVento; + $weatherData->temp_med = $d->temp_med; + $weatherData->pressao = $d->pressao; + $weatherData->vento_int_max_inst = $d->vento_int_max_inst; + $weatherData->temp_min = $d->temp_min; + $weatherData->rad_total = $d->rad_total; + $weatherData->temp_max = $d->temp_max; + $weatherData->vento_int_med = $d->vento_int_med; + $weatherData->hum_med = $d->hum_med; + $weatherData->vento_dir_max = $d->vento_dir_max; + $weatherData->prec_max_inst = $d->prec_max_inst; + $weatherData->prec_quant = $d->prec_quant; + $weatherData->hum_max = $d->hum_max; + $weatherData->date = Carbon::parse($ddate->startOfDay()); + $weatherData->stationId = $stationId; + + $weatherData->save(); + } + } + } + } + + } +} diff --git a/app/Models/WeatherDataDaily.php b/app/Models/WeatherDataDaily.php new file mode 100644 index 0000000..301349d --- /dev/null +++ b/app/Models/WeatherDataDaily.php @@ -0,0 +1,54 @@ + null, + 1 => 'N', + 2 => 'NE', + 3 => 'E', + 4 => 'SE', + 5 => 'S', + 6 => 'SW', + 7 => 'W', + 8 => 'NW', + 9 => 'N', + ]; + + protected $fillable = [ + 'hum_min', + 'idDireccVento', + 'temp_med', + 'pressao', + 'vento_int_max_inst', + 'temp_min', + 'rad_total', + 'temp_max', + 'vento_int_med', + 'hum_med', + 'vento_dir_max', + 'prec_max_inst', + 'prec_quant', + 'hum_max', + 'date', + 'stationId' + ]; + + public function station() + { + return $this->hasOne(WeatherStation::class, 'id', 'stationId'); + } +}