feat: adapt device models api
Some checks are pending
tests / ci (push) Waiting to run

This commit is contained in:
Benjamin Nussbaum 2025-08-16 09:41:00 +02:00
parent a88e72b75e
commit ba3bf31bb7
29 changed files with 2379 additions and 215 deletions

View file

@ -0,0 +1,125 @@
<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Models\DeviceModel;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
final class FetchDeviceModelsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const API_URL = 'https://usetrmnl.com/api/models';
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
try {
$response = Http::timeout(30)->get(self::API_URL);
if (! $response->successful()) {
Log::error('Failed to fetch device models from API', [
'status' => $response->status(),
'body' => $response->body(),
]);
return;
}
$data = $response->json('data', []);
if (! is_array($data)) {
Log::error('Invalid response format from device models API', [
'response' => $response->json(),
]);
return;
}
$this->processDeviceModels($data);
Log::info('Successfully fetched and updated device models', [
'count' => count($data),
]);
} catch (Exception $e) {
Log::error('Exception occurred while fetching device models', [
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
}
/**
* Process the device models data and update/create records.
*/
private function processDeviceModels(array $deviceModels): void
{
foreach ($deviceModels as $modelData) {
try {
$this->updateOrCreateDeviceModel($modelData);
} catch (Exception $e) {
Log::error('Failed to process device model', [
'model_data' => $modelData,
'error' => $e->getMessage(),
]);
}
}
}
/**
* Update or create a device model record.
*/
private function updateOrCreateDeviceModel(array $modelData): void
{
$name = $modelData['name'] ?? null;
if (! $name) {
Log::warning('Device model data missing name field', [
'model_data' => $modelData,
]);
return;
}
$attributes = [
'label' => $modelData['label'] ?? '',
'description' => $modelData['description'] ?? '',
'width' => $modelData['width'] ?? 0,
'height' => $modelData['height'] ?? 0,
'colors' => $modelData['colors'] ?? 0,
'bit_depth' => $modelData['bit_depth'] ?? 0,
'scale_factor' => $modelData['scale_factor'] ?? 1,
'rotation' => $modelData['rotation'] ?? 0,
'mime_type' => $modelData['mime_type'] ?? '',
'offset_x' => $modelData['offset_x'] ?? 0,
'offset_y' => $modelData['offset_y'] ?? 0,
'published_at' => $modelData['published_at'] ?? null,
'source' => 'api',
];
DeviceModel::updateOrCreate(
['name' => $name],
$attributes
);
}
}

View file

@ -78,22 +78,30 @@ class FetchProxyCloudResponses implements ShouldQueue
Log::info("Successfully updated proxy cloud response for device: {$device->mac_address}");
if ($device->last_log_request) {
Http::withHeaders([
'id' => $device->mac_address,
'access-token' => $device->api_key,
'width' => 800,
'height' => 480,
'rssi' => $device->last_rssi_level,
'battery_voltage' => $device->last_battery_voltage,
'refresh-rate' => $device->default_refresh_interval,
'fw-version' => $device->last_firmware_version,
'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0',
'user-agent' => 'ESP32HTTPClient',
])->post(config('services.trmnl.proxy_base_url').'/api/log', $device->last_log_request);
try {
Http::withHeaders([
'id' => $device->mac_address,
'access-token' => $device->api_key,
'width' => 800,
'height' => 480,
'rssi' => $device->last_rssi_level,
'battery_voltage' => $device->last_battery_voltage,
'refresh-rate' => $device->default_refresh_interval,
'fw-version' => $device->last_firmware_version,
'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0',
'user-agent' => 'ESP32HTTPClient',
])->post(config('services.trmnl.proxy_base_url').'/api/log', $device->last_log_request);
$device->update([
'last_log_request' => null,
]);
// Only clear the pending log request if the POST succeeded
$device->update([
'last_log_request' => null,
]);
} catch (Exception $e) {
// Do not fail the entire proxy fetch if the log upload fails
Log::error("Failed to upload device log for device: {$device->mac_address}", [
'error' => $e->getMessage(),
]);
}
}
} catch (Exception $e) {