diff --git a/app/Models/Device.php b/app/Models/Device.php index 4fe3508..4182743 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -21,6 +21,7 @@ class Device extends Model 'width' => 'integer', 'height' => 'integer', 'rotate' => 'integer', + 'last_refreshed_at' => 'datetime', ]; public function getBatteryPercentAttribute() diff --git a/database/migrations/2025_06_03_141055_add_last_refresh_at_to_devices_table.php b/database/migrations/2025_06_03_141055_add_last_refresh_at_to_devices_table.php new file mode 100644 index 0000000..51b1882 --- /dev/null +++ b/database/migrations/2025_06_03_141055_add_last_refresh_at_to_devices_table.php @@ -0,0 +1,28 @@ +timestamp('last_refreshed_at')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('devices', function (Blueprint $table) { + $table->dropColumn('last_refreshed_at'); + }); + } +}; diff --git a/resources/views/livewire/devices/configure.blade.php b/resources/views/livewire/devices/configure.blade.php index 8e19503..5e0d468 100644 --- a/resources/views/livewire/devices/configure.blade.php +++ b/resources/views/livewire/devices/configure.blade.php @@ -268,8 +268,8 @@ new class extends Component {

{{ $device->name }}

- - {{$device->updated_at->diffForHumans()}} + + {{$device->last_refreshed_at?->diffForHumans()}} diff --git a/routes/api.php b/routes/api.php index 76610d7..2fb5e3c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -43,6 +43,7 @@ Route::get('/display', function (Request $request) { 'last_rssi_level' => $request->header('rssi'), 'last_battery_voltage' => $request->header('battery_voltage'), 'last_firmware_version' => $request->header('fw-version'), + 'last_refreshed_at' => now(), ]); // Get current screen image from mirror device or continue if not available diff --git a/tests/Feature/Api/DeviceEndpointsTest.php b/tests/Feature/Api/DeviceEndpointsTest.php index 7ec86f6..e033d5a 100644 --- a/tests/Feature/Api/DeviceEndpointsTest.php +++ b/tests/Feature/Api/DeviceEndpointsTest.php @@ -647,3 +647,53 @@ test('plugins in playlist are rendered in order', function () { expect($thirdResponse['filename']) ->not->toBe($secondResponse['filename']); })->skipOnGitHubActions(); + +test('display endpoint updates last_refreshed_at timestamp', function () { + $device = Device::factory()->create([ + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', + ]); + + $response = $this->withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'rssi' => -70, + 'battery_voltage' => 3.8, + 'fw-version' => '1.0.0', + ])->get('/api/display'); + + $response->assertOk(); + + $device->refresh(); + expect($device->last_refreshed_at)->not->toBeNull() + ->and($device->last_refreshed_at->diffInSeconds(now()))->toBeLessThan(2); +}); + +test('display endpoint updates last_refreshed_at timestamp for mirrored devices', function () { + // Create source device + $sourceDevice = Device::factory()->create([ + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'source-api-key', + ]); + + // Create mirroring device + $mirrorDevice = Device::factory()->create([ + 'mac_address' => 'AA:BB:CC:DD:EE:FF', + 'api_key' => 'mirror-api-key', + 'mirror_device_id' => $sourceDevice->id, + ]); + + $response = $this->withHeaders([ + 'id' => $mirrorDevice->mac_address, + 'access-token' => $mirrorDevice->api_key, + 'rssi' => -70, + 'battery_voltage' => 3.8, + 'fw-version' => '1.0.0', + ])->get('/api/display'); + + $response->assertOk(); + + $mirrorDevice->refresh(); + expect($mirrorDevice->last_refreshed_at)->not->toBeNull() + ->and($mirrorDevice->last_refreshed_at->diffInSeconds(now()))->toBeLessThan(2); +}); diff --git a/tests/Feature/DeviceConfigureTest.php b/tests/Feature/DeviceConfigureTest.php new file mode 100644 index 0000000..95e3d2b --- /dev/null +++ b/tests/Feature/DeviceConfigureTest.php @@ -0,0 +1,24 @@ +create(); + $device = Device::factory()->create([ + 'user_id' => $user->id, + 'last_refreshed_at' => now()->subMinutes(5), + ]); + + $response = actingAs($user) + ->get(route('devices.configure', $device)); + + $response->assertOk() + ->assertSee('5 minutes ago'); +});