fix: update_firmware flag is only returned once to avoid upgrade loop

chore: fix code style
This commit is contained in:
Benjamin Nussbaum 2025-03-30 10:19:15 +02:00
parent 5367fc8a64
commit 6e5e4cd633
4 changed files with 59 additions and 1 deletions

View file

@ -71,6 +71,14 @@ class Device extends Model
return null;
}
public function resetUpdateFirmwareFlag(): void
{
if ($this->proxy_cloud_response) {
$this->proxy_cloud_response = array_merge($this->proxy_cloud_response, ['update_firmware' => false]);
$this->save();
}
}
public function playlists(): HasMany
{
return $this->hasMany(Playlist::class);

View file

@ -42,5 +42,5 @@ return [
'override_orig_icon' => env('TRMNL_OVERRIDE_ORIG_ICON', false),
'image_url_timeout' => env('TRMNL_IMAGE_URL_TIMEOUT', null),
],
];

View file

@ -92,6 +92,11 @@ Route::get('/display', function (Request $request) {
$response['image_url_timeout'] = config('services.trmnl.image_url_timeout');
}
// If update_firmware is true, reset it after returning it, to avoid upgrade loop
if ($device->update_firmware) {
$device->resetUpdateFirmwareFlag();
}
return response()->json($response);
});

View file

@ -198,3 +198,48 @@ test('log endpoint requires valid device credentials', function () {
$response->assertNotFound()
->assertJson(['message' => 'Device not found or invalid access token']);
});
test('update_firmware flag is only returned once', function () {
$device = Device::factory()->create([
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
'proxy_cloud_response' => [
'update_firmware' => true,
'firmware_url' => 'https://example.com/firmware.bin',
],
]);
// First request should return update_firmware as true
$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()
->assertJson([
'update_firmware' => true,
'firmware_url' => 'https://example.com/firmware.bin',
]);
// Second request should return update_firmware as false
$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()
->assertJson([
'update_firmware' => false,
'firmware_url' => 'https://example.com/firmware.bin',
]);
// Verify the proxy_cloud_response was updated
$device->refresh();
expect($device->proxy_cloud_response['update_firmware'])->toBeFalse();
});