Дашборд, зоны, устройства, правила автоматизации и профиль теперь 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 проп на любой странице.
161 lines
5.2 KiB
PHP
161 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\DeviceRequest;
|
|
use App\Http\Resources\DeviceResource;
|
|
use App\Http\Resources\DeviceTypeResource;
|
|
use App\Http\Resources\ZoneResource;
|
|
use App\Models\Device;
|
|
use App\Models\DeviceType;
|
|
use App\Models\Zone;
|
|
use App\Services\ClickHouseClient;
|
|
use App\Services\DeviceControlClient;
|
|
use App\Services\DeviceShadow;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class DeviceController extends Controller
|
|
{
|
|
public function index(DeviceShadow $shadow)
|
|
{
|
|
$this->authorize('viewAny', Device::class);
|
|
|
|
$devices = Device::with(['zone', 'deviceType'])->orderBy('name')->get();
|
|
$statuses = $shadow->statuses($devices->pluck('external_id')->all());
|
|
$devices->each(fn (Device $d) => $d->live_status = $statuses[$d->external_id] ?? 'unknown');
|
|
|
|
return Inertia::render('Devices/Index', [
|
|
'devices' => DeviceResource::collection($devices)->resolve(),
|
|
]);
|
|
}
|
|
|
|
public function show(Device $device, DeviceShadow $shadow, ClickHouseClient $clickHouse)
|
|
{
|
|
$this->authorize('view', $device);
|
|
|
|
$device->load(['zone', 'deviceType']);
|
|
$snapshot = $shadow->snapshot($device->external_id);
|
|
$device->live_status = $snapshot['status'];
|
|
|
|
$telemetry = [];
|
|
if ($device->deviceType->category->value === 'sensor') {
|
|
$telemetry = $clickHouse->query(
|
|
'SELECT sensor_type, value, recorded_at FROM telemetry
|
|
WHERE device_id = {device_id:String}
|
|
ORDER BY recorded_at DESC LIMIT 50',
|
|
['device_id' => $device->external_id],
|
|
);
|
|
}
|
|
|
|
return Inertia::render('Devices/Show', [
|
|
'device' => (new DeviceResource($device))->resolve(),
|
|
'shadow' => [
|
|
'last_seen' => $snapshot['last_seen'],
|
|
'desired_state' => $snapshot['desired_state'],
|
|
'reported_state' => $snapshot['reported_state'],
|
|
],
|
|
'telemetry' => $telemetry,
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorize('create', Device::class);
|
|
|
|
return Inertia::render('Devices/Create', $this->formOptions());
|
|
}
|
|
|
|
public function store(DeviceRequest $request)
|
|
{
|
|
$device = new Device($request->validated());
|
|
$device->user()->associate($request->user());
|
|
$device->save();
|
|
|
|
return redirect()->route('devices.index')->with('status', 'Устройство добавлено.');
|
|
}
|
|
|
|
public function edit(Device $device)
|
|
{
|
|
$this->authorize('update', $device);
|
|
|
|
return Inertia::render('Devices/Edit', [
|
|
'device' => $device->only(['id', 'name', 'zone_id', 'device_type_id', 'external_id', 'protocol']),
|
|
...$this->formOptions(),
|
|
]);
|
|
}
|
|
|
|
public function update(DeviceRequest $request, Device $device)
|
|
{
|
|
$device->update($request->validated());
|
|
|
|
return redirect()->route('devices.index')->with('status', 'Устройство обновлено.');
|
|
}
|
|
|
|
public function destroy(Device $device)
|
|
{
|
|
$this->authorize('delete', $device);
|
|
|
|
$device->delete();
|
|
|
|
return redirect()->route('devices.index')->with('status', 'Устройство удалено.');
|
|
}
|
|
|
|
public function turnOn(Device $device, DeviceControlClient $client)
|
|
{
|
|
$this->authorize('update', $device);
|
|
$this->ensureCapability($device, 'turn_on');
|
|
|
|
return $this->respondToCommand($client->turnOn($device->external_id));
|
|
}
|
|
|
|
public function turnOff(Device $device, DeviceControlClient $client)
|
|
{
|
|
$this->authorize('update', $device);
|
|
$this->ensureCapability($device, 'turn_off');
|
|
|
|
return $this->respondToCommand($client->turnOff($device->external_id));
|
|
}
|
|
|
|
public function setLevel(Request $request, Device $device, DeviceControlClient $client)
|
|
{
|
|
$this->authorize('update', $device);
|
|
$this->ensureCapability($device, 'set_level');
|
|
|
|
$validated = $request->validate(['level' => ['required', 'numeric']]);
|
|
|
|
return $this->respondToCommand($client->setLevel($device->external_id, (float) $validated['level']));
|
|
}
|
|
|
|
private function ensureCapability(Device $device, string $capability): void
|
|
{
|
|
abort_unless(
|
|
in_array($capability, $device->deviceType->capabilities ?? [], true),
|
|
422,
|
|
"Устройство не поддерживает действие «{$capability}».",
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array{success: bool, error: ?string} $result
|
|
*/
|
|
private function respondToCommand(array $result)
|
|
{
|
|
return back()->with(
|
|
$result['success'] ? 'status' : 'error',
|
|
$result['success'] ? 'Команда отправлена.' : ($result['error'] ?? 'Команда не выполнена.'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function formOptions(): array
|
|
{
|
|
return [
|
|
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
|
|
'deviceTypes' => DeviceTypeResource::collection(DeviceType::orderBy('code')->get())->resolve(),
|
|
];
|
|
}
|
|
}
|