Skip to content

Commit 3df8ef9

Browse files
authored
🐛 catch error while calculating live position (#3149)
1 parent 0c17764 commit 3df8ef9

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

app/Http/Controllers/Backend/Support/LocationController.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ private function filterStopoversFromStatus(): ?array {
6464
return $newStopovers;
6565
}
6666

67-
/**
68-
* @throws JsonException
69-
*/
7067
public function calculateLivePosition(): ?LivePointDto {
7168
$newStopovers = $this->filterStopoversFromStatus();
7269

@@ -86,43 +83,46 @@ public function calculateLivePosition(): ?LivePointDto {
8683
$this->status
8784
);
8885
}
89-
90-
$now = Carbon::now()->timestamp;
91-
$percentage = ($now - $newStopovers[0]->departure->timestamp)
92-
/ ($newStopovers[1]->arrival->timestamp - $newStopovers[0]->departure->timestamp);
93-
$this->origin = $newStopovers[0];
94-
$this->destination = $newStopovers[1];
95-
$polyline = $this->getPolylineBetween(false);
96-
97-
$meters = $this->getDistanceFromGeoJson($polyline) * $percentage;
98-
$recentPoint = null;
99-
$distance = 0;
100-
foreach ($polyline->features as $key => $point) {
101-
$point = Coordinate::fromGeoJson($point);
102-
if ($recentPoint !== null && $point !== null) {
103-
$lineSegment = new LineSegment($recentPoint, $point);
104-
105-
$distance += $lineSegment->calculateDistance();
106-
if ($distance >= $meters) {
107-
break;
86+
try {
87+
$now = Carbon::now()->timestamp;
88+
$percentage = ($now - $newStopovers[0]->departure->timestamp)
89+
/ ($newStopovers[1]->arrival->timestamp - $newStopovers[0]->departure->timestamp);
90+
$this->origin = $newStopovers[0];
91+
$this->destination = $newStopovers[1];
92+
$polyline = $this->getPolylineBetween(false);
93+
94+
$meters = $this->getDistanceFromGeoJson($polyline) * $percentage;
95+
$recentPoint = null;
96+
$distance = 0;
97+
foreach ($polyline->features as $key => $point) {
98+
$point = Coordinate::fromGeoJson($point);
99+
if ($recentPoint !== null && $point !== null) {
100+
$lineSegment = new LineSegment($recentPoint, $point);
101+
102+
$distance += $lineSegment->calculateDistance();
103+
if ($distance >= $meters) {
104+
break;
105+
}
108106
}
107+
$recentPoint = $point ?? $recentPoint;
109108
}
110-
$recentPoint = $point ?? $recentPoint;
111-
}
112109

113-
$currentPosition = $lineSegment->interpolatePoint($meters / $distance);
110+
$currentPosition = $lineSegment->interpolatePoint($meters / $distance);
114111

115-
$polyline->features = array_slice($polyline->features, $key);
116-
array_unshift($polyline->features, Feature::fromCoordinate($currentPosition));
112+
$polyline->features = array_slice($polyline->features, $key);
113+
array_unshift($polyline->features, Feature::fromCoordinate($currentPosition));
117114

118-
return new LivePointDto(
119-
null,
120-
$polyline,
121-
$newStopovers[1]->arrival->timestamp,
122-
$newStopovers[1]->departure->timestamp,
123-
$this->trip->linename,
124-
$this->status,
125-
);
115+
return new LivePointDto(
116+
null,
117+
$polyline,
118+
$newStopovers[1]->arrival->timestamp,
119+
$newStopovers[1]->departure->timestamp,
120+
$this->trip->linename,
121+
$this->status,
122+
);
123+
} catch (Exception) {
124+
return null;
125+
}
126126
}
127127

128128
private function getDistanceFromGeoJson(stdClass $geoJson): int {

0 commit comments

Comments
 (0)