-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\WeatherData; | ||
use App\Models\WeatherDataDaily; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class UpdateWeatherDataDaily extends Job | ||
{ | ||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$url = "https://api.ipma.pt/public-data/observation/surface-stations/daily-observations.json"; | ||
|
||
$options = [ | ||
'headers' => [ | ||
'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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Jenssegers\Mongodb\Eloquent\Model; | ||
|
||
class WeatherDataDaily extends Model | ||
{ | ||
protected $connection = 'mongodb'; | ||
protected $collection = 'weatherDataDaily'; | ||
protected $primaryKey = '_id'; | ||
|
||
public const CREATED_AT = 'created'; | ||
public const UPDATED_AT = 'updated'; | ||
|
||
protected $dates = ['date', 'created', 'updated']; | ||
|
||
public const WIND_DIRECTIONS = [ | ||
0 => 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'); | ||
} | ||
} |