Handle any errors if unable to get presence or weather data

This commit is contained in:
floppydiskette 2024-08-31 00:47:01 +01:00
parent 97d8f4447d
commit a715ae58af
Signed by: fwoppydwisk
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
4 changed files with 31 additions and 15 deletions

View file

@ -31,13 +31,15 @@ public function getDiscordPresence(): mixed {
$response = Http::get('https://api.lanyard.rest/v1/users/' . Config::get('services.lanyard.user_id')); $response = Http::get('https://api.lanyard.rest/v1/users/' . Config::get('services.lanyard.user_id'));
$data = $response->json(); $data = $response->json();
if (!isset($data["data"])) return null;
$presence = $data["data"]; $presence = $data["data"];
Cache::put('discord_presence', $presence, now()->addSeconds(60)); Cache::put('discord_presence', $presence, now()->addSeconds(60));
return $presence; return $presence;
} }
public function getOnlineStatus(): array { public function getOnlineStatus(): ?array {
$presence = $this->getDiscordPresence(); $presence = $this->getDiscordPresence();
if ($presence == null) return null;
return match ($presence["discord_status"]) { return match ($presence["discord_status"]) {
"online", "dnd" => [ "online", "dnd" => [
"text" => "online", "text" => "online",

View file

@ -3,6 +3,7 @@
namespace App\View\Components; namespace App\View\Components;
use Closure; use Closure;
use Exception;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
@ -25,11 +26,16 @@ public function getWeatherData(): mixed {
return Cache::get('weather_data'); return Cache::get('weather_data');
} }
$response = Http::get('http://'. Config::get('services.weatherlink') . '/v1/current_conditions'); try {
$data = $response->json(); $response = Http::get('http://' . Config::get('services.weatherlink') . '/v1/current_conditions');
$conditions = $data["data"]["conditions"]; $data = $response->json();
Cache::put('weather_data', $conditions, now()->addSeconds(60)); $conditions = $data["data"]["conditions"];
return $conditions; Cache::put('weather_data', $conditions, now()->addSeconds(60));
return $conditions;
} catch (Exception $ex) {
return null;
}
} }
/** /**

View file

@ -1,3 +1,7 @@
<span>I'm</span> @if($status == null)
<h2 class="online-status" style="color: {{ $status["color"] }};text-shadow: var(--firefox-shadow) {{ $status["color"] }}4f !important">{{ $status["text"] }}!</h2> <p>Status Unavailable</p>
@else
<span>I'm</span>
<h2 class="online-status" style="color: {{ $status["color"] }};text-shadow: var(--firefox-shadow) {{ $status["color"] }}4f !important">{{ $status["text"] }}!</h2>
@endif
<p><strong>Time in Britain:</strong> <span id="clock"></span></p> <p><strong>Time in Britain:</strong> <span id="clock"></span></p>

View file

@ -1,11 +1,15 @@
<p><strong>Weather Conditions:</strong></p> <p><strong>Weather Conditions:</strong></p>
<hr style="margin: 4px 0"> <hr style="margin: 4px 0">
<p><strong>Temperature:</strong> {{ round(($conditions[0]["temp"] - 32) * (5/9), 1) }} degC</p> @if($conditions == null)
<p><strong>Rain:</strong> {{ ($conditions[0]["rain_rate_last"] * 0.2) }}mm/hr ({{ $conditions[0]["rainfall_daily"] }}mm today)</p> <p>Data Unavailable</p>
@if ($conditions[0]["wind_speed_last"] != 0)
<p><strong>Wind:</strong> {{ round($conditions[0]["wind_speed_last"], 1) }}mph ({{ $conditions[0]["wind_dir_last"] }} deg)</p>
@else @else
<p><strong>Wind:</strong> 0mph</p> <p><strong>Temperature:</strong> {{ round(($conditions[0]["temp"] - 32) * (5/9), 1) }} degC</p>
<p><strong>Rain:</strong> {{ ($conditions[0]["rain_rate_last"] * 0.2) }}mm/hr ({{ $conditions[0]["rainfall_daily"] }}mm today)</p>
@if ($conditions[0]["wind_speed_last"] != 0)
<p><strong>Wind:</strong> {{ round($conditions[0]["wind_speed_last"], 1) }}mph ({{ $conditions[0]["wind_dir_last"] }} deg)</p>
@else
<p><strong>Wind:</strong> 0mph</p>
@endif
<p><strong>Humidity:</strong> {{ round($conditions[0]["hum"], 1) }}%</p>
<p><strong>Pressure:</strong> {{ round($conditions[2]["bar_sea_level"], 1) }} inHg</p>
@endif @endif
<p><strong>Humidity:</strong> {{ round($conditions[0]["hum"], 1) }}%</p>
<p><strong>Pressure:</strong> {{ round($conditions[2]["bar_sea_level"], 1) }} inHg</p>