2022-11-01 00:50:43 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
|
use App\Mail\WeeklyDigest;
|
|
|
|
|
use GuzzleHttp\Client;
|
2023-01-14 21:02:31 +00:00
|
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2022-11-01 00:50:43 +00:00
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
|
|
class TrackerController extends Controller
|
|
|
|
|
{
|
2022-11-01 02:29:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* Show the list of past trips.
|
|
|
|
|
* TODO: Get these from the Wayward API rather than hard-coding them.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
|
*/
|
2022-11-30 18:47:09 +00:00
|
|
|
|
public function show_past_trips_list()
|
|
|
|
|
{
|
2022-11-01 00:50:43 +00:00
|
|
|
|
return view('past-trips');
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* Show a trip in tracker app.
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param string? $tripId
|
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
|
*/
|
2022-11-30 18:47:09 +00:00
|
|
|
|
public function show_trip(Request $request, string $tripId = null)
|
|
|
|
|
{
|
2022-11-01 02:29:33 +00:00
|
|
|
|
$tripId = $tripId ?? config('app.current_trip_id');
|
2022-11-30 18:47:09 +00:00
|
|
|
|
if (!$tripId) {
|
|
|
|
|
return view('no-trip');
|
|
|
|
|
}
|
2022-11-01 00:50:43 +00:00
|
|
|
|
|
|
|
|
|
$viewMode = $request->input('show', null);
|
2022-11-10 02:16:12 +00:00
|
|
|
|
$fromCheckin = $request->input('from', null);
|
|
|
|
|
$toCheckin = $request->input('to', null);
|
2022-11-29 23:58:11 +00:00
|
|
|
|
$forceDownload = $request->input('force', false);
|
2024-06-17 20:21:22 +00:00
|
|
|
|
$cherryPickedCheckins = explode(',', $request->input('cherrypick', null));
|
2022-11-29 23:58:11 +00:00
|
|
|
|
|
2023-01-14 21:02:31 +00:00
|
|
|
|
try {
|
|
|
|
|
$tripData = $this->get_trip_data($tripId, $forceDownload);
|
|
|
|
|
} catch (ConnectException) {
|
|
|
|
|
return view(
|
|
|
|
|
'error',
|
|
|
|
|
[
|
2024-03-14 18:31:28 +00:00
|
|
|
|
'message' => "App timed out whilst downloading trip data" .
|
|
|
|
|
" from Wayward's servers. They may be down currently."
|
2023-01-14 21:02:31 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-11-01 00:50:43 +00:00
|
|
|
|
|
2022-11-10 02:16:12 +00:00
|
|
|
|
if ($fromCheckin) {
|
2022-11-29 23:58:11 +00:00
|
|
|
|
$tripData->checkins = array_filter(
|
2022-11-30 18:47:09 +00:00
|
|
|
|
$tripData->checkins,
|
2022-12-13 00:25:11 +00:00
|
|
|
|
function ($checkin) use ($fromCheckin) {
|
|
|
|
|
return $checkin->id >= $fromCheckin;
|
|
|
|
|
}
|
2022-11-29 23:58:11 +00:00
|
|
|
|
);
|
2022-11-10 02:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($toCheckin) {
|
2022-11-29 23:58:11 +00:00
|
|
|
|
$tripData->checkins = array_filter(
|
2022-11-30 18:47:09 +00:00
|
|
|
|
$tripData->checkins,
|
2022-12-13 00:25:11 +00:00
|
|
|
|
function ($checkin) use ($toCheckin) {
|
|
|
|
|
return $checkin->id <= $toCheckin;
|
|
|
|
|
}
|
2022-11-29 23:58:11 +00:00
|
|
|
|
);
|
2022-11-10 02:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 20:21:22 +00:00
|
|
|
|
if ($cherryPickedCheckins) {
|
|
|
|
|
$tripData->checkins = array_filter(
|
|
|
|
|
$tripData->checkins,
|
|
|
|
|
function ($checkin) use ($cherryPickedCheckins) {
|
|
|
|
|
return in_array($checkin->id, $cherryPickedCheckins);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
return view(
|
2022-11-30 18:47:09 +00:00
|
|
|
|
'tracker',
|
2022-11-01 02:29:33 +00:00
|
|
|
|
[
|
2022-11-30 18:47:09 +00:00
|
|
|
|
'trip' => $tripData,
|
2022-11-10 02:16:12 +00:00
|
|
|
|
'showAllCheckins' => ($viewMode === 'all'),
|
|
|
|
|
'fromCheckin' => $fromCheckin,
|
|
|
|
|
'toCheckin' => $toCheckin
|
2022-11-01 02:29:33 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a trip from the local cache, or from the Wayward API.
|
|
|
|
|
*
|
|
|
|
|
* The retrieval algorithm is as follows:
|
|
|
|
|
* 1. If no local cache exists, download the trip data
|
|
|
|
|
* 2. If a local cache exists:
|
|
|
|
|
* 1. If the trip is inactive (i.e., finished), use the cache
|
|
|
|
|
* 2. If the trip is active:
|
|
|
|
|
* 1. If the trip was actively tracking at last check, use the
|
2022-11-30 18:47:09 +00:00
|
|
|
|
* cache if it's less than an hour old. Otherwise, download
|
|
|
|
|
* the new trip data.
|
2022-11-01 02:29:33 +00:00
|
|
|
|
* 2. If the trip was not actively tracking at lest check, use
|
|
|
|
|
* the cache if it's less than three hours old. Otherwise,
|
|
|
|
|
* download the new trip data.
|
|
|
|
|
* 3. If new trip data is downloaded, overwrite any cached data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $tripId
|
2022-11-29 23:58:11 +00:00
|
|
|
|
* @param bool $forceDownload
|
2022-11-01 02:29:33 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-11-30 18:47:09 +00:00
|
|
|
|
public function get_trip_data(string $tripId, bool $forceDownload = false)
|
|
|
|
|
{
|
2022-11-01 00:50:43 +00:00
|
|
|
|
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
// Returns the cached trip data if the trip is inactive (i.e., finished)
|
|
|
|
|
// or less than 1–3 hours old, depending on whether it was tracking
|
|
|
|
|
// at last check or not.
|
2022-11-29 23:58:11 +00:00
|
|
|
|
if (!$forceDownload && Storage::disk('local')->exists($tripFileName)) {
|
2022-11-01 00:50:43 +00:00
|
|
|
|
Log::debug("Cached trip file '{$tripFileName}' found...");
|
|
|
|
|
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
|
|
|
|
|
|
|
|
|
|
if ($cachedData->is_active) {
|
|
|
|
|
Log::debug("Cached trip file '{$tripFileName}' is for an active trip.");
|
|
|
|
|
|
|
|
|
|
$cachedDataUpdatedAt = new DateTime($cachedData->updated_at);
|
|
|
|
|
$now = new DateTime();
|
|
|
|
|
$cachingTimeout = ($cachedData->is_tracking) ? 1 : 3;
|
|
|
|
|
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
|
|
|
|
|
|
|
|
|
|
if ($cachedDataAge <= $cachingTimeout) {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
Log::debug(
|
|
|
|
|
"Cached trip file '" .
|
|
|
|
|
$tripFileName .
|
|
|
|
|
"' is younger than " .
|
|
|
|
|
$cachingTimeout .
|
|
|
|
|
" hours, showing from cache..."
|
|
|
|
|
);
|
2022-11-01 02:29:33 +00:00
|
|
|
|
return $cachedData;
|
2022-11-01 00:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
Log::debug(
|
|
|
|
|
"Cached trip file '" .
|
|
|
|
|
$tripFileName .
|
|
|
|
|
"' is for an old trip, showing from cache..."
|
|
|
|
|
);
|
2022-11-01 02:29:33 +00:00
|
|
|
|
return $cachedData;
|
2022-11-01 00:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
// Otherwise, download the trip data from the Wayward API.
|
2022-11-29 23:58:11 +00:00
|
|
|
|
if ($forceDownload) {
|
|
|
|
|
Log::debug("Forcing download for '{$tripFileName}'.");
|
2024-03-14 18:31:28 +00:00
|
|
|
|
Storage::disk('local')->delete($tripFileName);
|
2022-11-29 23:58:11 +00:00
|
|
|
|
} else {
|
|
|
|
|
Log::debug("No cached trip file found for '{$tripFileName}'.");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 00:50:43 +00:00
|
|
|
|
$client = new Client([
|
|
|
|
|
'base_uri' => 'https://app.wayward.travel/',
|
2023-01-10 20:00:48 +00:00
|
|
|
|
'timeout' => 20.0
|
2022-11-01 00:50:43 +00:00
|
|
|
|
]);
|
2023-01-14 21:02:31 +00:00
|
|
|
|
|
2022-11-01 00:50:43 +00:00
|
|
|
|
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
switch ($response->getStatusCode()) {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
case 200:
|
|
|
|
|
$data = json_decode($response->getBody());
|
2022-11-01 00:50:43 +00:00
|
|
|
|
|
2023-01-14 20:08:56 +00:00
|
|
|
|
$data->trip->locations = $this->decode_polyline($data->trip->route);
|
|
|
|
|
|
2022-11-01 02:29:33 +00:00
|
|
|
|
// Cache the downloaded file if it does not exist locally.
|
|
|
|
|
if (Storage::disk('local')->missing($tripFileName)) {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
Log::debug("Caching new trip file '{$tripFileName}'.");
|
2022-11-01 00:50:43 +00:00
|
|
|
|
Storage::disk('local')->put($tripFileName, json_encode($data));
|
|
|
|
|
} else {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
2023-05-19 14:39:48 +00:00
|
|
|
|
if (end($data->trip->checkins)->date !== end($cachedData->trip->checkins)->date) {
|
2022-11-30 18:47:09 +00:00
|
|
|
|
Log::debug(
|
|
|
|
|
"Cached trip file '" .
|
|
|
|
|
$tripFileName .
|
|
|
|
|
"' has different 'updated_at' time, updating cache..."
|
|
|
|
|
);
|
|
|
|
|
Storage::disk('local')->put($tripFileName, json_encode($data));
|
|
|
|
|
// TODO: Cache photos locally
|
|
|
|
|
} else {
|
|
|
|
|
Log::debug(
|
|
|
|
|
"Cached trip file '" .
|
|
|
|
|
$tripFileName .
|
|
|
|
|
"' has same 'updated_at' time, showing from cache..."
|
|
|
|
|
);
|
|
|
|
|
$data = $cachedData;
|
|
|
|
|
}
|
2022-11-01 00:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 18:47:09 +00:00
|
|
|
|
return $data->trip;
|
|
|
|
|
default:
|
2022-11-01 00:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-10 20:00:48 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Decode route polyline.
|
|
|
|
|
*
|
|
|
|
|
* Source: https://github.com/dyaaj/polyline-encoder
|
|
|
|
|
*
|
|
|
|
|
* @param object $value
|
|
|
|
|
* @return object
|
|
|
|
|
*/
|
|
|
|
|
public function decode_polyline($value)
|
|
|
|
|
{
|
|
|
|
|
$index = 0;
|
|
|
|
|
$points = array();
|
|
|
|
|
$lat = 0;
|
|
|
|
|
$lng = 0;
|
|
|
|
|
$id = 0;
|
|
|
|
|
|
|
|
|
|
while ($index < strlen($value)) {
|
|
|
|
|
$b;
|
|
|
|
|
$shift = 0;
|
|
|
|
|
$result = 0;
|
|
|
|
|
do {
|
|
|
|
|
$b = ord(substr($value, $index++, 1)) - 63;
|
|
|
|
|
$result |= ($b & 0x1f) << $shift;
|
|
|
|
|
$shift += 5;
|
|
|
|
|
} while ($b > 31);
|
|
|
|
|
$dlat = (($result & 1) ? ~($result >> 1) : ($result >> 1));
|
|
|
|
|
$lat += $dlat;
|
|
|
|
|
|
|
|
|
|
$shift = 0;
|
|
|
|
|
$result = 0;
|
|
|
|
|
do {
|
|
|
|
|
$b = ord(substr($value, $index++, 1)) - 63;
|
|
|
|
|
$result |= ($b & 0x1f) << $shift;
|
|
|
|
|
$shift += 5;
|
|
|
|
|
} while ($b > 31);
|
|
|
|
|
$dlng = (($result & 1) ? ~($result >> 1) : ($result >> 1));
|
|
|
|
|
$lng += $dlng;
|
|
|
|
|
|
2024-03-14 18:31:28 +00:00
|
|
|
|
array_push($points, (object)[
|
2023-01-10 20:00:48 +00:00
|
|
|
|
'id' => $id++,
|
2024-03-14 18:31:28 +00:00
|
|
|
|
'latitude' => $lat/100000,
|
2023-01-21 20:01:11 +00:00
|
|
|
|
'longitude' => $lng/100000
|
2023-01-10 20:00:48 +00:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $points;
|
|
|
|
|
}
|
2022-11-01 00:50:43 +00:00
|
|
|
|
}
|