Files
HickmeisterandClaude Sonnet 5.5 2fe4fbd91e
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 28s
Cache integration API reads and refresh them on a schedule
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 19:01:32 +01:00

86 lines
2.6 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class DryerClient
{
public function __construct(private IntegrationSettings $settings, private IntegrationCache $cache) {}
public function configured(): bool
{
$settings = $this->settings->for('dryer');
return filled($settings['url'] ?? null) && filled($settings['api_key'] ?? null);
}
public function dashboardUrl(): string
{
$url = rtrim((string) ($this->settings->for('dryer')['url'] ?? ''), '/');
return preg_replace('#/api/v1$#', '', $url) ?? $url;
}
private function apiKey(): string
{
return (string) ($this->settings->for('dryer')['api_key'] ?? '');
}
private function url(string $path): string
{
$base = $this->settings->for('dryer')['url'] ?? null;
if (! $this->configured()) {
throw new RuntimeException('Dryer URL and API key must be configured.');
}
$base = rtrim($base, '/');
if (! str_ends_with($base, '/api/v1')) {
$base .= '/api/v1';
}
return $base.'/'.$path;
}
public function status(): array
{
return $this->cache->remember('dryer.status', 20, fn (): array => Http::withHeaders(['X-API-Key' => $this->apiKey()])->acceptJson()
->connectTimeout(3)->timeout(7)->get($this->url('status'))->throw()->json() ?? []);
}
public function history(): array
{
return $this->cache->remember('dryer.history', 120, fn (): array => Http::withHeaders(['X-API-Key' => $this->apiKey()])->acceptJson()
->connectTimeout(3)->timeout(7)->get($this->url('history'), ['limit' => 60])->throw()->json() ?? []);
}
public function start(string $profile, int $targetTemperature, int $dryingTime): array
{
return $this->command('cycles/start', [
'profile' => $profile, 'targetC' => $targetTemperature, 'durationMinutes' => $dryingTime,
]);
}
public function stop(): array
{
return $this->command('cycles/stop');
}
public function acknowledge(): array
{
return $this->command('faults/acknowledge');
}
private function command(string $path, array $payload = []): array
{
$this->cache->forget('dryer.status');
$this->cache->forget('dryer.history');
return Http::withHeaders(['X-API-Key' => $this->apiKey()])->acceptJson()
->connectTimeout(3)->timeout(7)->post($this->url($path), [
'requestId' => 'dashboard-'.bin2hex(random_bytes(12)), ...$payload,
])->throw()->json() ?? [];
}
}