Дашборд, зоны, устройства, правила автоматизации и профиль теперь Vue 3 + Inertia вместо Blade+Alpine — гостевые страницы Breeze (логин/регистрация) сознательно оставлены на Blade, чтобы не трогать уже протестированную аутентификацию. Контроллеры возвращают Inertia::render() вместо view(), переиспользуют существующие API Resources и FormRequest. Ключевой нюанс: Resource/ResourceCollection, переданные напрямую в проп Inertia, заворачиваются в ключ "data" (Inertia вызывает toResponse() у Responsable-объектов) — везде используется ->resolve() и явное резолвление вложенных ресурсов (zone/device_type внутри Device и т.п.), иначе вложенные поля тоже задваивались бы обёрткой. Alpine.js и все Blade-вьюхи защищённой части удалены как мёртвый код. Тесты (ZoneControllerTest/DeviceControllerTest) переведены на assertInertia(). UserFactory теперь явно задаёт role=owner — без этого Eloquent не подтягивает DB-default обратно в модель после create(), что уронило бы общий auth.user проп на любой странице.
65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* HTTP client for device-control-service's manual-command API (see that
|
|
* service's README for why HTTP instead of gRPC here). Mirrors the
|
|
* gRPC CommandResult contract: {success, error} in the body — always
|
|
* returned as an array here too, never an exception, so callers don't need
|
|
* to distinguish "device rejected it" from "service unreachable" to render
|
|
* a flash message.
|
|
*/
|
|
class DeviceControlClient
|
|
{
|
|
public function __construct(private readonly string $baseUrl) {}
|
|
|
|
/** @return array{success: bool, error: ?string} */
|
|
public function turnOn(string $externalId): array
|
|
{
|
|
return $this->post("/devices/{$externalId}/turn-on");
|
|
}
|
|
|
|
/** @return array{success: bool, error: ?string} */
|
|
public function turnOff(string $externalId): array
|
|
{
|
|
return $this->post("/devices/{$externalId}/turn-off");
|
|
}
|
|
|
|
/** @return array{success: bool, error: ?string} */
|
|
public function setLevel(string $externalId, float $level): array
|
|
{
|
|
return $this->post("/devices/{$externalId}/set-level", ['level' => $level]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $body
|
|
* @return array{success: bool, error: ?string}
|
|
*/
|
|
private function post(string $path, array $body = []): array
|
|
{
|
|
try {
|
|
$response = Http::timeout(5)->post("{$this->baseUrl}{$path}", $body);
|
|
} catch (ConnectionException $e) {
|
|
Log::error('device-control-service unreachable', ['path' => $path, 'error' => $e->getMessage()]);
|
|
|
|
return ['success' => false, 'error' => 'device-control-service недоступен'];
|
|
}
|
|
|
|
if ($response->failed()) {
|
|
Log::error('device-control-service returned an error status', ['path' => $path, 'status' => $response->status()]);
|
|
|
|
return ['success' => false, 'error' => 'device-control-service вернул ошибку'];
|
|
}
|
|
|
|
return [
|
|
'success' => (bool) $response->json('success'),
|
|
'error' => $response->json('error'),
|
|
];
|
|
}
|
|
}
|