Skip to content

Commit

Permalink
🐛 catch error while calculating live position
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Jan 14, 2025
1 parent 0c17764 commit 211329c
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions app/Http/Controllers/Backend/Support/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ private function filterStopoversFromStatus(): ?array {
return $newStopovers;
}

/**
* @throws JsonException
*/
public function calculateLivePosition(): ?LivePointDto {
$newStopovers = $this->filterStopoversFromStatus();

Expand All @@ -86,43 +83,46 @@ public function calculateLivePosition(): ?LivePointDto {
$this->status
);
}

$now = Carbon::now()->timestamp;
$percentage = ($now - $newStopovers[0]->departure->timestamp)
/ ($newStopovers[1]->arrival->timestamp - $newStopovers[0]->departure->timestamp);
$this->origin = $newStopovers[0];
$this->destination = $newStopovers[1];
$polyline = $this->getPolylineBetween(false);

$meters = $this->getDistanceFromGeoJson($polyline) * $percentage;
$recentPoint = null;
$distance = 0;
foreach ($polyline->features as $key => $point) {
$point = Coordinate::fromGeoJson($point);
if ($recentPoint !== null && $point !== null) {
$lineSegment = new LineSegment($recentPoint, $point);

$distance += $lineSegment->calculateDistance();
if ($distance >= $meters) {
break;
try {
$now = Carbon::now()->timestamp;
$percentage = ($now - $newStopovers[0]->departure->timestamp)
/ ($newStopovers[1]->arrival->timestamp - $newStopovers[0]->departure->timestamp);
$this->origin = $newStopovers[0];
$this->destination = $newStopovers[1];
$polyline = $this->getPolylineBetween(false);

$meters = $this->getDistanceFromGeoJson($polyline) * $percentage;
$recentPoint = null;
$distance = 0;
foreach ($polyline->features as $key => $point) {
$point = Coordinate::fromGeoJson($point);
if ($recentPoint !== null && $point !== null) {
$lineSegment = new LineSegment($recentPoint, $point);

$distance += $lineSegment->calculateDistance();
if ($distance >= $meters) {
break;
}
}
$recentPoint = $point ?? $recentPoint;
}
$recentPoint = $point ?? $recentPoint;
}

$currentPosition = $lineSegment->interpolatePoint($meters / $distance);
$currentPosition = $lineSegment->interpolatePoint($meters / $distance);

$polyline->features = array_slice($polyline->features, $key);
array_unshift($polyline->features, Feature::fromCoordinate($currentPosition));
$polyline->features = array_slice($polyline->features, $key);
array_unshift($polyline->features, Feature::fromCoordinate($currentPosition));

return new LivePointDto(
null,
$polyline,
$newStopovers[1]->arrival->timestamp,
$newStopovers[1]->departure->timestamp,
$this->trip->linename,
$this->status,
);
return new LivePointDto(
null,
$polyline,
$newStopovers[1]->arrival->timestamp,
$newStopovers[1]->departure->timestamp,
$this->trip->linename,
$this->status,
);
} catch (Exception) {
return null;
}
}

private function getDistanceFromGeoJson(stdClass $geoJson): int {
Expand Down

0 comments on commit 211329c

Please sign in to comment.