Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomahock committed Oct 30, 2023
1 parent 8fdf67a commit 0844e15
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
88 changes: 88 additions & 0 deletions app/Jobs/UpdateWeatherDataDaily.php
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();
}
}
}
}

}
}
54 changes: 54 additions & 0 deletions app/Models/WeatherDataDaily.php
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');
}
}

0 comments on commit 0844e15

Please sign in to comment.