Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ yarn-error.log
/storage/logs
/data.ms
run.json
composer_install.log
20 changes: 19 additions & 1 deletion app/Jobs/RealtimeData/DispatchAgency.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -55,7 +58,22 @@ public function handle(): void
$this->agency->headers = [];
}

$response = Http::withHeaders($this->agency->headers ?? [])->get($this->agency->realtime_url);
$response = Http::withHeaders($this->agency->headers ?? [])
->timeout(10)
->retry(3, 100, function (\Exception $exception, PendingRequest $request) {
if ($exception instanceof ConnectionException) {
return true;
}

if ($exception instanceof RequestException) {
$status = $exception->response->status();

return $status === 400 || $status >= 500;
}

return false;
}, throw: false)
->get($this->agency->realtime_url);

if ($response->failed()) {
Log::error('Error in DispatchAgency http request', [
Expand Down
30 changes: 22 additions & 8 deletions app/Jobs/RealtimeData/GtfsRtHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function handle()
'label' => $this->processField($vehicle->getVehicle()->getLabel(), 'label'),
'license_plate' => $this->processField($vehicle->getVehicle()->getLicensePlate()),
'position' => $this->processField(['lat' => $vehicle->getPosition()->getLatitude(), 'lon' => $vehicle->getPosition()->getLongitude()], 'position'),
'bearing' => $this->processField($vehicle->getPosition()->getBearing()),
'odometer' => $this->processField(round($vehicle->getPosition()->getOdometer() / 1000, 0)),
'speed' => $this->processField(round($vehicle->getPosition()->getSpeed() * 3.6, 0)),
'bearing' => $this->processField($vehicle->getPosition()->getBearing(), 'bearing'),
'odometer' => $this->processField($vehicle->getPosition()->getOdometer() / 1000, 'odometer'),
'speed' => $this->processField($vehicle->getPosition()->getSpeed() * 3.6, 'speed'),
'current_stop_sequence' => $this->processField($vehicle->getCurrentStopSequence()),
'current_status' => $this->processField($vehicle->getCurrentStatus()),
'timestamp' => $this->processField($vehicle->getTimestamp() ?? $this->time),
Expand All @@ -116,10 +116,6 @@ public function handle()

$activeArray[] = $vehicle->id;
} catch (Exception $e) {
// London Transit Commission often have vehicles with invalid coordinates, ignore these
if ($this->agency->slug === 'ltc' && str($e->getMessage())->contains("1264 Out of range value for column 'lon'")) {
return;
}

Log::warning('Vehicle in the refresh failed', [
'agency' => $this->agency->slug,
Expand Down Expand Up @@ -156,6 +152,18 @@ public function handle()

private function processField($value, ?string $transformer = null)
{
if ($transformer === 'speed') {
return (is_numeric($value) && $value >= 0 && $value <= 150) ? round((float) $value, 0) : null;
}

if ($transformer === 'odometer') {
return (is_numeric($value) && $value >= 0 && $value <= 10000000) ? round((float) $value, 0) : null;
}

if ($transformer === 'bearing') {
return (is_numeric($value) && $value >= 0 && $value <= 360) ? round((float) $value, 0) : null;
}

if (! filled($value)) {
return null;
}
Expand All @@ -165,7 +173,13 @@ private function processField($value, ?string $transformer = null)
}

if ($transformer === 'position' && filled($value['lat']) && filled($value['lon'])) {
return new Point(round($value['lat'], 5), round($value['lon'], 5));
$lat = round((float) $value['lat'], 5);
$lon = round((float) $value['lon'], 5);
if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
return null;
}

return new Point($lat, $lon);
}

if ($transformer === 'timestamp') {
Expand Down
20 changes: 19 additions & 1 deletion app/Jobs/RealtimeData/JavascriptGtfsRtHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ public function handle(): void

private function processField(mixed $value, ?string $transformer = null)
{
if ($transformer === 'speed') {
return (is_numeric($value) && $value >= 0 && $value <= 150) ? round((float) $value, 0) : null;
}

if ($transformer === 'odometer') {
return (is_numeric($value) && $value >= 0 && $value <= 10000000) ? round((float) $value, 0) : null;
}

if ($transformer === 'bearing') {
return (is_numeric($value) && $value >= 0 && $value <= 360) ? round((float) $value, 0) : null;
}

if (! filled($value)) {
return null;
}
Expand All @@ -156,7 +168,13 @@ private function processField(mixed $value, ?string $transformer = null)
}

if ($transformer === 'position' && filled($value['lat']) && filled($value['lon'])) {
return new Point(round($value['lat'], 5), round($value['lon'], 5));
$lat = round((float) $value['lat'], 5);
$lon = round((float) $value['lon'], 5);
if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
return null;
}

return new Point($lat, $lon);
}

if ($transformer === 'timestamp') {
Expand Down
20 changes: 17 additions & 3 deletions app/Jobs/RealtimeData/NextbusJsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function handle()
'position' => $this->processField(['lat' => $vehicle->lat, 'lon' => $vehicle->lon], 'position'),
'gtfs_route_id' => $this->retrieveRoute($vehicle->routeTag),
'gtfs_trip_id' => $this->retrieveTrip($vehicle->routeTag),
'bearing' => $this->processField($vehicle->heading),
'speed' => $this->processField($vehicle->speedKmHr),
'bearing' => $this->processField($vehicle->heading, 'bearing'),
'speed' => $this->processField($vehicle->speedKmHr, 'speed'),
'timestamp' => $this->processField(strval($timestamp - (int) $vehicle->secsSinceReport)),
'last_seen_at' => $this->processField($timestamp - (int) $vehicle->secsSinceReport, 'timestamp'),
]
Expand Down Expand Up @@ -110,12 +110,26 @@ public function handle()

private function processField($value, ?string $transformer = null)
{
if ($transformer === 'speed') {
return (is_numeric($value) && $value >= 0 && $value <= 150) ? round((float) $value, 0) : null;
}

if ($transformer === 'bearing') {
return (is_numeric($value) && $value >= 0 && $value <= 360) ? round((float) $value, 0) : null;
}

if (! filled($value)) {
return null;
}

if ($transformer === 'position' && filled($value['lat']) && filled($value['lon'])) {
return (new Point(round((float) $value['lat'], 5), round((float) $value['lon'], 5)))->toSqlExpression(DB::connection());
$lat = round((float) $value['lat'], 5);
$lon = round((float) $value['lon'], 5);
if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
return null;
}

return (new Point($lat, $lon))->toSqlExpression(DB::connection());
}

if ($transformer === 'timestamp') {
Expand Down
Loading