Merge branch 'feature/zone-devices-view' into feature/health-check-service
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ZoneRequest;
|
||||
use App\Http\Resources\DeviceResource;
|
||||
use App\Http\Resources\ZoneResource;
|
||||
use App\Models\Zone;
|
||||
use App\Services\DeviceShadow;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class ZoneController extends Controller
|
||||
@@ -20,6 +22,20 @@ class ZoneController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Zone $zone, DeviceShadow $shadow)
|
||||
{
|
||||
$this->authorize('view', $zone);
|
||||
|
||||
$devices = $zone->devices()->with('deviceType')->orderBy('name')->get();
|
||||
$statuses = $shadow->statuses($devices->pluck('external_id')->all());
|
||||
$devices->each(fn ($d) => $d->live_status = $statuses[$d->external_id] ?? 'unknown');
|
||||
|
||||
return Inertia::render('Zones/Show', [
|
||||
'zone' => (new ZoneResource($zone))->resolve(),
|
||||
'devices' => DeviceResource::collection($devices)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create', Zone::class);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<script setup>
|
||||
defineProps({
|
||||
category: { type: String, required: true },
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="flex h-10 w-10 shrink-0 items-center justify-center rounded-full"
|
||||
:class="category === 'sensor' ? 'bg-sky-50 text-sky-600' : 'bg-amber-50 text-amber-600'"
|
||||
>
|
||||
<svg v-if="category === 'sensor'" class="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 3v10.5a3.5 3.5 0 1 0 3 0" />
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M9.5 6h5" />
|
||||
</svg>
|
||||
<svg v-else class="h-5 w-5" viewBox="0 0 24 24" fill="currentColor">
|
||||
<path d="M13 2 3 13.5h6.5L10.5 22 21 9.5h-6.5L13 2Z" />
|
||||
</svg>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup>
|
||||
defineProps({
|
||||
status: { type: String, required: true },
|
||||
});
|
||||
|
||||
const dotColor = {
|
||||
online: 'bg-green-500',
|
||||
offline: 'bg-gray-300',
|
||||
unknown: 'bg-yellow-400',
|
||||
};
|
||||
|
||||
const label = {
|
||||
online: 'Онлайн',
|
||||
offline: 'Офлайн',
|
||||
unknown: 'Неизвестно',
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
<span class="relative flex h-2.5 w-2.5">
|
||||
<span
|
||||
v-if="status === 'online'"
|
||||
class="absolute inline-flex h-full w-full animate-ping rounded-full opacity-75"
|
||||
:class="dotColor[status]"
|
||||
/>
|
||||
<span class="relative inline-flex h-2.5 w-2.5 rounded-full" :class="dotColor[status] ?? dotColor.unknown" />
|
||||
</span>
|
||||
<span class="text-xs font-medium text-gray-500">{{ label[status] ?? status }}</span>
|
||||
</span>
|
||||
</template>
|
||||
@@ -34,31 +34,32 @@ function destroy(zone) {
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<table class="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr class="border-b">
|
||||
<th class="py-2">Название</th>
|
||||
<th class="py-2">Описание</th>
|
||||
<th class="py-2">Устройств</th>
|
||||
<th class="py-2" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="zones.length === 0">
|
||||
<td colspan="4" class="py-4 text-gray-500">Зон пока нет.</td>
|
||||
</tr>
|
||||
<tr v-for="zone in zones" :key="zone.id" class="border-b">
|
||||
<td class="py-2">{{ zone.name }}</td>
|
||||
<td class="py-2 text-gray-500">{{ zone.description }}</td>
|
||||
<td class="py-2">{{ zone.devices_count }}</td>
|
||||
<td class="py-2 text-right space-x-2">
|
||||
<Link v-if="isOwner" :href="routes.zones.edit(zone.id)" class="text-indigo-600 hover:underline">Изменить</Link>
|
||||
<button v-if="isOwner" type="button" class="text-red-600 hover:underline" @click="destroy(zone)">Удалить</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div v-if="zones.length === 0" class="bg-white rounded-xl border border-dashed border-gray-300 p-10 text-center text-sm text-gray-500">
|
||||
Зон пока нет.
|
||||
</div>
|
||||
|
||||
<div v-else class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
<div
|
||||
v-for="zone in zones"
|
||||
:key="zone.id"
|
||||
class="group relative flex flex-col gap-3 rounded-xl border border-gray-200 bg-white p-5 transition hover:border-indigo-200 hover:shadow-md"
|
||||
>
|
||||
<Link :href="routes.zones.show(zone.id)" class="absolute inset-0" :aria-label="zone.name" />
|
||||
|
||||
<div class="flex items-start justify-between">
|
||||
<h3 class="font-medium text-gray-900 group-hover:text-indigo-600">{{ zone.name }}</h3>
|
||||
<span class="rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-600">
|
||||
{{ zone.devices_count }} {{ zone.devices_count === 1 ? 'устройство' : 'устройств' }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<p class="text-sm text-gray-500 min-h-5">{{ zone.description }}</p>
|
||||
|
||||
<div v-if="isOwner" class="relative z-10 mt-2 flex items-center gap-3 border-t border-gray-100 pt-3 text-sm">
|
||||
<Link :href="routes.zones.edit(zone.id)" class="text-indigo-600 hover:underline">Изменить</Link>
|
||||
<button type="button" class="text-red-600 hover:underline" @click="destroy(zone)">Удалить</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
<script setup>
|
||||
import { computed } from 'vue';
|
||||
import { Link, usePage } from '@inertiajs/vue3';
|
||||
import AppLayout from '@/Layouts/AppLayout.vue';
|
||||
import DeviceTypeIcon from '@/Components/DeviceTypeIcon.vue';
|
||||
import StatusDot from '@/Components/StatusDot.vue';
|
||||
import { routes } from '@/routes';
|
||||
|
||||
const props = defineProps({
|
||||
zone: { type: Object, required: true },
|
||||
devices: { type: Array, required: true },
|
||||
});
|
||||
|
||||
const isOwner = computed(() => usePage().props.auth.user?.role === 'owner');
|
||||
|
||||
const onlineCount = computed(() => props.devices.filter((d) => d.status === 'online').length);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppLayout>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<Link :href="routes.zones.index" class="text-sm text-gray-500 hover:text-gray-700">← Зоны</Link>
|
||||
<h2 class="mt-1 font-semibold text-xl text-gray-800 leading-tight">{{ zone.name }}</h2>
|
||||
</div>
|
||||
<Link
|
||||
v-if="isOwner"
|
||||
:href="routes.zones.edit(zone.id)"
|
||||
class="inline-flex items-center px-4 py-2 bg-white border border-gray-300 rounded-md font-semibold text-xs text-gray-700 uppercase tracking-widest hover:bg-gray-50"
|
||||
>
|
||||
Изменить зону
|
||||
</Link>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8 space-y-6">
|
||||
<div v-if="zone.description" class="text-sm text-gray-600">{{ zone.description }}</div>
|
||||
|
||||
<div class="flex items-center gap-2 text-sm text-gray-500">
|
||||
<span class="font-medium text-gray-700">{{ devices.length }}</span> устройств,
|
||||
<span class="font-medium text-green-600">{{ onlineCount }}</span> онлайн
|
||||
</div>
|
||||
|
||||
<div v-if="devices.length === 0" class="bg-white rounded-xl border border-dashed border-gray-300 p-10 text-center text-sm text-gray-500">
|
||||
В этой зоне пока нет устройств.
|
||||
</div>
|
||||
|
||||
<div v-else class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
<Link
|
||||
v-for="device in devices"
|
||||
:key="device.id"
|
||||
:href="routes.devices.show(device.id)"
|
||||
class="group flex flex-col gap-4 rounded-xl border border-gray-200 bg-white p-5 transition hover:border-indigo-200 hover:shadow-md"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-3">
|
||||
<div class="flex items-center gap-3">
|
||||
<DeviceTypeIcon :category="device.device_type.category" />
|
||||
<div>
|
||||
<div class="font-medium text-gray-900 group-hover:text-indigo-600">{{ device.name }}</div>
|
||||
<div class="text-xs text-gray-500">{{ device.device_type.code }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center justify-between border-t border-gray-100 pt-3">
|
||||
<StatusDot :status="device.status" />
|
||||
<span class="font-mono text-xs text-gray-400">{{ device.external_id }}</span>
|
||||
</div>
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AppLayout>
|
||||
</template>
|
||||
@@ -6,6 +6,7 @@ export const routes = {
|
||||
dashboard: '/dashboard',
|
||||
zones: {
|
||||
index: '/zones',
|
||||
show: (id) => `/zones/${id}`,
|
||||
create: '/zones/create',
|
||||
store: '/zones',
|
||||
edit: (id) => `/zones/${id}/edit`,
|
||||
|
||||
@@ -30,7 +30,7 @@ Route::get('/dashboard', function (DeviceShadow $shadow) {
|
||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||
|
||||
Route::middleware(['auth', 'verified'])->group(function () {
|
||||
Route::resource('zones', ZoneController::class)->except('show');
|
||||
Route::resource('zones', ZoneController::class);
|
||||
Route::resource('devices', DeviceController::class);
|
||||
Route::resource('automation-rules', AutomationRuleController::class)
|
||||
->except('show')
|
||||
|
||||
@@ -2,9 +2,13 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\DeviceCategory;
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceType;
|
||||
use App\Models\User;
|
||||
use App\Models\Zone;
|
||||
use App\Services\DeviceShadow;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Tests\TestCase;
|
||||
@@ -52,6 +56,35 @@ class ZoneControllerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function test_zone_show_lists_its_devices_with_live_status(): void
|
||||
{
|
||||
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
||||
$zone = Zone::forceCreate(['user_id' => $viewer->id, 'name' => 'Гроубокс']);
|
||||
$type = DeviceType::create([
|
||||
'code' => 'fan',
|
||||
'category' => DeviceCategory::Actuator,
|
||||
'capabilities' => ['turn_on'],
|
||||
]);
|
||||
Device::forceCreate([
|
||||
'user_id' => $viewer->id, 'zone_id' => $zone->id, 'device_type_id' => $type->id,
|
||||
'name' => 'Вентилятор', 'external_id' => 'fan-1', 'protocol' => 'mqtt',
|
||||
]);
|
||||
|
||||
$this->mock(DeviceShadow::class, function ($mock) {
|
||||
$mock->shouldReceive('statuses')->once()->with(['fan-1'])->andReturn(['fan-1' => 'online']);
|
||||
});
|
||||
|
||||
$response = $this->actingAs($viewer)->get(route('zones.show', $zone));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertInertia(fn (Assert $page) => $page
|
||||
->component('Zones/Show')
|
||||
->where('zone.name', 'Гроубокс')
|
||||
->where('devices.0.name', 'Вентилятор')
|
||||
->where('devices.0.status', 'online')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_viewer_cannot_delete_zone(): void
|
||||
{
|
||||
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
||||
|
||||
Reference in New Issue
Block a user