mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat: update plugin data if stale
This commit is contained in:
parent
cda1223103
commit
64eb1bc047
3 changed files with 56 additions and 4 deletions
|
|
@ -4,6 +4,7 @@ namespace App\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Plugin extends Model
|
||||
|
|
@ -14,6 +15,8 @@ class Plugin extends Model
|
|||
|
||||
protected $casts = [
|
||||
'data_payload' => 'json',
|
||||
'data_payload_updated_at' => 'datetime',
|
||||
'is_native' => 'boolean',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
|
|
@ -26,4 +29,24 @@ class Plugin extends Model
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function isDataStale(): bool
|
||||
{
|
||||
if (! $this->data_payload_updated_at || ! $this->data_stale_minutes) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->data_payload_updated_at->addMinutes($this->data_stale_minutes)->isPast();
|
||||
}
|
||||
|
||||
public function updateDataPayload(): void
|
||||
{
|
||||
if ($this->data_strategy === 'polling' && $this->polling_url) {
|
||||
$response = Http::get($this->polling_url)->json();
|
||||
$this->update([
|
||||
'data_payload' => $response,
|
||||
'data_payload_updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('plugins', function (Blueprint $table) {
|
||||
$table->timestamp('data_payload_updated_at')->nullable()->after('data_payload');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('plugins', function (Blueprint $table) {
|
||||
$table->dropColumn('data_payload_updated_at');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -46,11 +46,18 @@ Route::get('/display', function (Request $request) {
|
|||
$playlistItem = $device->getNextPlaylistItem();
|
||||
|
||||
if ($playlistItem) {
|
||||
$plugin = $playlistItem->plugin;
|
||||
|
||||
// Check and update stale data if needed
|
||||
if ($plugin->isDataStale()) {
|
||||
$plugin->updateDataPayload();
|
||||
}
|
||||
|
||||
$playlistItem->update(['last_displayed_at' => now()]);
|
||||
if ($playlistItem->plugin->render_markup) {
|
||||
$markup = Blade::render($playlistItem->plugin->render_markup, ['data' => $playlistItem->plugin->data_payload]);
|
||||
} elseif ($playlistItem->plugin->render_markup_view) {
|
||||
$markup = view($playlistItem->plugin->render_markup_view, ['data' => $playlistItem->plugin->data_payload])->render();
|
||||
if ($plugin->render_markup) {
|
||||
$markup = Blade::render($plugin->render_markup, ['data' => $plugin->data_payload]);
|
||||
} elseif ($plugin->render_markup_view) {
|
||||
$markup = view($plugin->render_markup_view, ['data' => $plugin->data_payload])->render();
|
||||
}
|
||||
|
||||
GenerateScreenJob::dispatchSync($device->id, $markup);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue