Compare commits
7
Commits
be1f238f89
...
a7d61b9fcc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7d61b9fcc | ||
|
|
2c542fe097 | ||
|
|
e452217cc7 | ||
|
|
f7aa0ba88e | ||
|
|
6f8a6b49aa | ||
|
|
11d210839d | ||
|
|
17af76c159 |
+5
-2
@@ -34,8 +34,11 @@ MQTT_PORT=1883
|
|||||||
DEVICE_CONTROL_GRPC_PORT=50051
|
DEVICE_CONTROL_GRPC_PORT=50051
|
||||||
DEVICE_CONTROL_HTTP_PORT=8090
|
DEVICE_CONTROL_HTTP_PORT=8090
|
||||||
DEVICE_CONTROL_MQTT_CLIENT_ID=device-control-service
|
DEVICE_CONTROL_MQTT_CLIENT_ID=device-control-service
|
||||||
DEVICE_CONTROL_HEALTHCHECK_TIMEOUT=60s
|
|
||||||
DEVICE_CONTROL_HEALTHCHECK_INTERVAL=15s
|
# --- health-check-service ---
|
||||||
|
HEALTH_CHECK_TIMEOUT=60s
|
||||||
|
HEALTH_CHECK_INTERVAL=15s
|
||||||
|
HEALTH_CHECK_METRICS_PORT=9103
|
||||||
|
|
||||||
# --- ingest-service ---
|
# --- ingest-service ---
|
||||||
INGEST_MQTT_CLIENT_ID=ingest-service
|
INGEST_MQTT_CLIENT_ID=ingest-service
|
||||||
|
|||||||
+28
-4
@@ -140,8 +140,6 @@ services:
|
|||||||
RABBITMQ_PORT: 5672
|
RABBITMQ_PORT: 5672
|
||||||
RABBITMQ_USER: ${RABBITMQ_USER}
|
RABBITMQ_USER: ${RABBITMQ_USER}
|
||||||
RABBITMQ_PASSWORD: ${RABBITMQ_PASSWORD}
|
RABBITMQ_PASSWORD: ${RABBITMQ_PASSWORD}
|
||||||
DEVICE_CONTROL_HEALTHCHECK_TIMEOUT: ${DEVICE_CONTROL_HEALTHCHECK_TIMEOUT}
|
|
||||||
DEVICE_CONTROL_HEALTHCHECK_INTERVAL: ${DEVICE_CONTROL_HEALTHCHECK_INTERVAL}
|
|
||||||
depends_on:
|
depends_on:
|
||||||
mosquitto:
|
mosquitto:
|
||||||
condition: service_started
|
condition: service_started
|
||||||
@@ -153,6 +151,33 @@ services:
|
|||||||
- home-automation
|
- home-automation
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
|
health-check-service:
|
||||||
|
build: ./services/health-check-service
|
||||||
|
ports:
|
||||||
|
- "${HEALTH_CHECK_METRICS_PORT}:9103"
|
||||||
|
environment:
|
||||||
|
REDIS_HOST: redis
|
||||||
|
REDIS_PORT: 6379
|
||||||
|
REDIS_PASSWORD: ${REDIS_PASSWORD}
|
||||||
|
REDIS_DB: ${REDIS_DB}
|
||||||
|
RABBITMQ_HOST: rabbitmq
|
||||||
|
RABBITMQ_PORT: 5672
|
||||||
|
RABBITMQ_USER: ${RABBITMQ_USER}
|
||||||
|
RABBITMQ_PASSWORD: ${RABBITMQ_PASSWORD}
|
||||||
|
HEALTH_CHECK_TIMEOUT: ${HEALTH_CHECK_TIMEOUT}
|
||||||
|
HEALTH_CHECK_INTERVAL: ${HEALTH_CHECK_INTERVAL}
|
||||||
|
HEALTH_CHECK_METRICS_PORT: 9103
|
||||||
|
depends_on:
|
||||||
|
redis:
|
||||||
|
condition: service_started
|
||||||
|
rabbitmq:
|
||||||
|
condition: service_healthy
|
||||||
|
device-control-service:
|
||||||
|
condition: service_started
|
||||||
|
networks:
|
||||||
|
- home-automation
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
rule-engine-service:
|
rule-engine-service:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
@@ -258,5 +283,4 @@ services:
|
|||||||
- home-automation
|
- home-automation
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
# Remaining app services (health-check-service, esp32-emulator) are added
|
# esp32-emulator is added here once implemented — see its README for the plan.
|
||||||
# here as they get implemented — see services/*/README.md for the plan.
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ WORKDIR /app
|
|||||||
COPY package.json package-lock.json ./
|
COPY package.json package-lock.json ./
|
||||||
RUN npm ci
|
RUN npm ci
|
||||||
COPY resources/ resources/
|
COPY resources/ resources/
|
||||||
COPY vite.config.js ./
|
COPY vite.config.js postcss.config.js tailwind.config.js ./
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# --- final: php-fpm + nginx in one container, supervised ---
|
# --- final: php-fpm + nginx in one container, supervised ---
|
||||||
|
|||||||
@@ -59,11 +59,11 @@ class DeviceController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(DeviceShadow $shadow)
|
||||||
{
|
{
|
||||||
$this->authorize('create', Device::class);
|
$this->authorize('create', Device::class);
|
||||||
|
|
||||||
return Inertia::render('Devices/Create', $this->formOptions());
|
return Inertia::render('Devices/Create', $this->formOptions($shadow));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(DeviceRequest $request)
|
public function store(DeviceRequest $request)
|
||||||
@@ -75,13 +75,13 @@ class DeviceController extends Controller
|
|||||||
return redirect()->route('devices.index')->with('status', 'Устройство добавлено.');
|
return redirect()->route('devices.index')->with('status', 'Устройство добавлено.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(Device $device)
|
public function edit(Device $device, DeviceShadow $shadow)
|
||||||
{
|
{
|
||||||
$this->authorize('update', $device);
|
$this->authorize('update', $device);
|
||||||
|
|
||||||
return Inertia::render('Devices/Edit', [
|
return Inertia::render('Devices/Edit', [
|
||||||
'device' => $device->only(['id', 'name', 'zone_id', 'device_type_id', 'external_id', 'protocol']),
|
'device' => $device->only(['id', 'name', 'zone_id', 'device_type_id', 'external_id', 'protocol']),
|
||||||
...$this->formOptions(),
|
...$this->formOptions($shadow),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,11 +150,21 @@ class DeviceController extends Controller
|
|||||||
/**
|
/**
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
private function formOptions(): array
|
private function formOptions(DeviceShadow $shadow): array
|
||||||
{
|
{
|
||||||
|
$registeredIds = Device::pluck('external_id')->all();
|
||||||
|
$unregisteredExternalIds = collect($shadow->knownExternalIds())
|
||||||
|
->diff($registeredIds)
|
||||||
|
->sort()
|
||||||
|
->values();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
|
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
|
||||||
'deviceTypes' => DeviceTypeResource::collection(DeviceType::orderBy('code')->get())->resolve(),
|
'deviceTypes' => DeviceTypeResource::collection(DeviceType::orderBy('code')->get())->resolve(),
|
||||||
|
// external_id of devices device-control-service has already seen
|
||||||
|
// over MQTT but nobody has registered yet — offered as
|
||||||
|
// suggestions (not a closed list) when adding/editing a device.
|
||||||
|
'unregisteredExternalIds' => $unregisteredExternalIds,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\DeviceTypeRequest;
|
||||||
|
use App\Http\Resources\DeviceTypeResource;
|
||||||
|
use App\Models\DeviceType;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class DeviceTypeController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$this->authorize('viewAny', DeviceType::class);
|
||||||
|
|
||||||
|
$deviceTypes = DeviceType::withCount('devices')->orderBy('code')->get();
|
||||||
|
|
||||||
|
return Inertia::render('DeviceTypes/Index', [
|
||||||
|
'deviceTypes' => DeviceTypeResource::collection($deviceTypes)->resolve(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
$this->authorize('create', DeviceType::class);
|
||||||
|
|
||||||
|
return Inertia::render('DeviceTypes/Create');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(DeviceTypeRequest $request)
|
||||||
|
{
|
||||||
|
DeviceType::create($request->validated());
|
||||||
|
|
||||||
|
return redirect()->route('device-types.index')->with('status', 'Тип устройства создан.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,10 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\ZoneRequest;
|
use App\Http\Requests\ZoneRequest;
|
||||||
|
use App\Http\Resources\DeviceResource;
|
||||||
use App\Http\Resources\ZoneResource;
|
use App\Http\Resources\ZoneResource;
|
||||||
use App\Models\Zone;
|
use App\Models\Zone;
|
||||||
|
use App\Services\DeviceShadow;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
|
|
||||||
class ZoneController extends Controller
|
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()
|
public function create()
|
||||||
{
|
{
|
||||||
$this->authorize('create', Zone::class);
|
$this->authorize('create', Zone::class);
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Enums\DeviceCategory;
|
||||||
|
use App\Models\DeviceType;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class DeviceTypeRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return $this->user()->can('create', DeviceType::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'code' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('device_types', 'code')],
|
||||||
|
'category' => ['required', Rule::enum(DeviceCategory::class)],
|
||||||
|
'capabilities' => ['array'],
|
||||||
|
'capabilities.*' => ['string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ class DeviceTypeResource extends JsonResource
|
|||||||
'code' => $this->code,
|
'code' => $this->code,
|
||||||
'category' => $this->category->value,
|
'category' => $this->category->value,
|
||||||
'capabilities' => $this->capabilities,
|
'capabilities' => $this->capabilities,
|
||||||
|
'devices_count' => $this->whenCounted('devices'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\DeviceType;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
/** See ZonePolicy for the household-wide RBAC rationale. */
|
||||||
|
class DeviceTypePolicy
|
||||||
|
{
|
||||||
|
public function viewAny(User $user): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function view(User $user, DeviceType $deviceType): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(User $user): bool
|
||||||
|
{
|
||||||
|
return $user->isOwner();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,4 +54,18 @@ class DeviceShadow
|
|||||||
|
|
||||||
return array_combine($externalIds, array_map(fn ($v) => $v ?: 'unknown', $values));
|
return array_combine($externalIds, array_map(fn ($v) => $v ?: 'unknown', $values));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* external_id of every device device-control-service has ever heard
|
||||||
|
* from over MQTT (`devices:known`, a Redis Set it maintains) — includes
|
||||||
|
* devices that sent telemetry/ack before anyone registered them in
|
||||||
|
* Postgres. Used to suggest an external_id when adding a device instead
|
||||||
|
* of requiring it to be typed exactly from memory.
|
||||||
|
*
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
public function knownExternalIds(): array
|
||||||
|
{
|
||||||
|
return Redis::smembers('devices:known');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
<script setup>
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import { useForm } from '@inertiajs/vue3';
|
||||||
|
import AppLayout from '@/Layouts/AppLayout.vue';
|
||||||
|
import { routes } from '@/routes';
|
||||||
|
|
||||||
|
const form = useForm({
|
||||||
|
code: '',
|
||||||
|
category: '',
|
||||||
|
capabilities: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const capabilityInput = ref('');
|
||||||
|
|
||||||
|
function addCapability() {
|
||||||
|
const value = capabilityInput.value.trim();
|
||||||
|
if (value && !form.capabilities.includes(value)) {
|
||||||
|
form.capabilities.push(value);
|
||||||
|
}
|
||||||
|
capabilityInput.value = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeCapability(index) {
|
||||||
|
form.capabilities.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function submit() {
|
||||||
|
form.post(routes.deviceTypes.store);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<AppLayout>
|
||||||
|
<template #header>
|
||||||
|
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Новый тип устройства</h2>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<div class="py-12">
|
||||||
|
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||||
|
<form @submit.prevent="submit">
|
||||||
|
<div>
|
||||||
|
<label for="code" class="block font-medium text-sm text-gray-700">Код</label>
|
||||||
|
<input
|
||||||
|
id="code"
|
||||||
|
v-model="form.code"
|
||||||
|
type="text"
|
||||||
|
required
|
||||||
|
autofocus
|
||||||
|
placeholder="например, smart_socket"
|
||||||
|
class="mt-1 block w-full font-mono border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||||
|
>
|
||||||
|
<p class="mt-1 text-xs text-gray-400">Только латиница, цифры, дефис и подчёркивание — используется как идентификатор.</p>
|
||||||
|
<div v-if="form.errors.code" class="mt-2 text-sm text-red-600">{{ form.errors.code }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<label for="category" class="block font-medium text-sm text-gray-700">Категория</label>
|
||||||
|
<select
|
||||||
|
id="category"
|
||||||
|
v-model="form.category"
|
||||||
|
required
|
||||||
|
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||||
|
>
|
||||||
|
<option value="">— выбрать —</option>
|
||||||
|
<option value="sensor">Датчик</option>
|
||||||
|
<option value="actuator">Исполнитель</option>
|
||||||
|
</select>
|
||||||
|
<div v-if="form.errors.category" class="mt-2 text-sm text-red-600">{{ form.errors.category }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<label for="capability" class="block font-medium text-sm text-gray-700">Возможности</label>
|
||||||
|
<p class="mt-1 text-xs text-gray-400">
|
||||||
|
Для датчика — типы показаний (например, temperature). Для исполнителя — команды
|
||||||
|
(например, turn_on, turn_off, set_level).
|
||||||
|
</p>
|
||||||
|
<div class="mt-2 flex gap-2">
|
||||||
|
<input
|
||||||
|
id="capability"
|
||||||
|
v-model="capabilityInput"
|
||||||
|
type="text"
|
||||||
|
placeholder="turn_on"
|
||||||
|
class="block w-full font-mono border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||||
|
@keydown.enter.prevent="addCapability"
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="shrink-0 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"
|
||||||
|
@click="addCapability"
|
||||||
|
>
|
||||||
|
Добавить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="form.capabilities.length" class="mt-3 flex flex-wrap gap-1.5">
|
||||||
|
<span
|
||||||
|
v-for="(capability, index) in form.capabilities"
|
||||||
|
:key="capability"
|
||||||
|
class="inline-flex items-center gap-1 rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-600"
|
||||||
|
>
|
||||||
|
{{ capability }}
|
||||||
|
<button type="button" class="text-gray-400 hover:text-red-600" @click="removeCapability(index)">×</button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="form.errors.capabilities" class="mt-2 text-sm text-red-600">{{ form.errors.capabilities }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-6 flex items-center gap-4">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
:disabled="form.processing"
|
||||||
|
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 disabled:opacity-50"
|
||||||
|
>
|
||||||
|
Сохранить
|
||||||
|
</button>
|
||||||
|
<a :href="routes.deviceTypes.index" class="text-sm text-gray-600 hover:underline">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AppLayout>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
<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 { routes } from '@/routes';
|
||||||
|
|
||||||
|
defineProps({
|
||||||
|
deviceTypes: { type: Array, required: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
const isOwner = computed(() => usePage().props.auth.user?.role === 'owner');
|
||||||
|
|
||||||
|
const categoryLabel = { sensor: 'Датчик', actuator: 'Исполнитель' };
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<AppLayout>
|
||||||
|
<template #header>
|
||||||
|
<div class="flex justify-between items-center">
|
||||||
|
<div>
|
||||||
|
<Link :href="routes.devices.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">Типы устройств</h2>
|
||||||
|
</div>
|
||||||
|
<Link
|
||||||
|
v-if="isOwner"
|
||||||
|
:href="routes.deviceTypes.create"
|
||||||
|
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700"
|
||||||
|
>
|
||||||
|
Добавить тип
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<div class="py-12">
|
||||||
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
<div v-if="deviceTypes.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="type in deviceTypes"
|
||||||
|
:key="type.id"
|
||||||
|
class="flex flex-col gap-4 rounded-xl border border-gray-200 bg-white p-5"
|
||||||
|
>
|
||||||
|
<div class="flex items-center gap-3">
|
||||||
|
<DeviceTypeIcon :category="type.category" />
|
||||||
|
<div>
|
||||||
|
<div class="font-medium text-gray-900">{{ type.code }}</div>
|
||||||
|
<div class="text-xs text-gray-500">{{ categoryLabel[type.category] ?? type.category }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex flex-wrap gap-1.5">
|
||||||
|
<span
|
||||||
|
v-for="capability in type.capabilities"
|
||||||
|
:key="capability"
|
||||||
|
class="rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-600"
|
||||||
|
>
|
||||||
|
{{ capability }}
|
||||||
|
</span>
|
||||||
|
<span v-if="type.capabilities.length === 0" class="text-xs text-gray-400">Без возможностей</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="border-t border-gray-100 pt-3 text-xs text-gray-400">
|
||||||
|
{{ type.devices_count }} {{ type.devices_count === 1 ? 'устройство' : 'устройств' }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AppLayout>
|
||||||
|
</template>
|
||||||
@@ -5,6 +5,7 @@ import Form from '@/Pages/Devices/Form.vue';
|
|||||||
defineProps({
|
defineProps({
|
||||||
zones: { type: Array, required: true },
|
zones: { type: Array, required: true },
|
||||||
deviceTypes: { type: Array, required: true },
|
deviceTypes: { type: Array, required: true },
|
||||||
|
unregisteredExternalIds: { type: Array, default: () => [] },
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ defineProps({
|
|||||||
<div class="py-12">
|
<div class="py-12">
|
||||||
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
||||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||||
<Form :zones="zones" :device-types="deviceTypes" />
|
<Form :zones="zones" :device-types="deviceTypes" :unregistered-external-ids="unregisteredExternalIds" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ defineProps({
|
|||||||
device: { type: Object, required: true },
|
device: { type: Object, required: true },
|
||||||
zones: { type: Array, required: true },
|
zones: { type: Array, required: true },
|
||||||
deviceTypes: { type: Array, required: true },
|
deviceTypes: { type: Array, required: true },
|
||||||
|
unregisteredExternalIds: { type: Array, default: () => [] },
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ defineProps({
|
|||||||
<div class="py-12">
|
<div class="py-12">
|
||||||
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
||||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||||
<Form :device="device" :zones="zones" :device-types="deviceTypes" />
|
<Form :device="device" :zones="zones" :device-types="deviceTypes" :unregistered-external-ids="unregisteredExternalIds" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const props = defineProps({
|
|||||||
device: { type: Object, default: null },
|
device: { type: Object, default: null },
|
||||||
zones: { type: Array, required: true },
|
zones: { type: Array, required: true },
|
||||||
deviceTypes: { type: Array, required: true },
|
deviceTypes: { type: Array, required: true },
|
||||||
|
unregisteredExternalIds: { type: Array, default: () => [] },
|
||||||
});
|
});
|
||||||
|
|
||||||
const isEdit = !!props.device;
|
const isEdit = !!props.device;
|
||||||
@@ -76,9 +77,17 @@ function submit() {
|
|||||||
id="external_id"
|
id="external_id"
|
||||||
v-model="form.external_id"
|
v-model="form.external_id"
|
||||||
type="text"
|
type="text"
|
||||||
|
list="unregistered-external-ids"
|
||||||
required
|
required
|
||||||
|
placeholder="можно ввести вручную или выбрать увиденное устройство"
|
||||||
class="mt-1 block w-full font-mono border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
class="mt-1 block w-full font-mono border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||||
>
|
>
|
||||||
|
<datalist id="unregistered-external-ids">
|
||||||
|
<option v-for="id in unregisteredExternalIds" :key="id" :value="id" />
|
||||||
|
</datalist>
|
||||||
|
<p v-if="unregisteredExternalIds.length" class="mt-1 text-xs text-gray-400">
|
||||||
|
От {{ unregisteredExternalIds.length }} {{ unregisteredExternalIds.length === 1 ? 'устройства' : 'устройств' }} уже приходил сигнал, но они ещё не зарегистрированы — начните вводить, чтобы увидеть подсказки.
|
||||||
|
</p>
|
||||||
<div v-if="form.errors.external_id" class="mt-2 text-sm text-red-600">{{ form.errors.external_id }}</div>
|
<div v-if="form.errors.external_id" class="mt-2 text-sm text-red-600">{{ form.errors.external_id }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -23,13 +23,16 @@ function destroy(device) {
|
|||||||
<template #header>
|
<template #header>
|
||||||
<div class="flex justify-between items-center">
|
<div class="flex justify-between items-center">
|
||||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Устройства</h2>
|
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Устройства</h2>
|
||||||
<Link
|
<div class="flex items-center gap-3">
|
||||||
v-if="isOwner"
|
<Link :href="routes.deviceTypes.index" class="text-sm text-gray-500 hover:text-gray-700">Типы устройств</Link>
|
||||||
:href="routes.devices.create"
|
<Link
|
||||||
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700"
|
v-if="isOwner"
|
||||||
>
|
:href="routes.devices.create"
|
||||||
Добавить устройство
|
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700"
|
||||||
</Link>
|
>
|
||||||
|
Добавить устройство
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
@@ -34,31 +34,32 @@ function destroy(zone) {
|
|||||||
|
|
||||||
<div class="py-12">
|
<div class="py-12">
|
||||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
<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">
|
<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">
|
||||||
<table class="w-full text-left text-sm">
|
Зон пока нет.
|
||||||
<thead>
|
</div>
|
||||||
<tr class="border-b">
|
|
||||||
<th class="py-2">Название</th>
|
<div v-else class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||||
<th class="py-2">Описание</th>
|
<div
|
||||||
<th class="py-2">Устройств</th>
|
v-for="zone in zones"
|
||||||
<th class="py-2" />
|
:key="zone.id"
|
||||||
</tr>
|
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"
|
||||||
</thead>
|
>
|
||||||
<tbody>
|
<Link :href="routes.zones.show(zone.id)" class="absolute inset-0" :aria-label="zone.name" />
|
||||||
<tr v-if="zones.length === 0">
|
|
||||||
<td colspan="4" class="py-4 text-gray-500">Зон пока нет.</td>
|
<div class="flex items-start justify-between">
|
||||||
</tr>
|
<h3 class="font-medium text-gray-900 group-hover:text-indigo-600">{{ zone.name }}</h3>
|
||||||
<tr v-for="zone in zones" :key="zone.id" class="border-b">
|
<span class="rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-600">
|
||||||
<td class="py-2">{{ zone.name }}</td>
|
{{ zone.devices_count }} {{ zone.devices_count === 1 ? 'устройство' : 'устройств' }}
|
||||||
<td class="py-2 text-gray-500">{{ zone.description }}</td>
|
</span>
|
||||||
<td class="py-2">{{ zone.devices_count }}</td>
|
</div>
|
||||||
<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>
|
<p class="text-sm text-gray-500 min-h-5">{{ zone.description }}</p>
|
||||||
<button v-if="isOwner" type="button" class="text-red-600 hover:underline" @click="destroy(zone)">Удалить</button>
|
|
||||||
</td>
|
<div v-if="isOwner" class="relative z-10 mt-2 flex items-center gap-3 border-t border-gray-100 pt-3 text-sm">
|
||||||
</tr>
|
<Link :href="routes.zones.edit(zone.id)" class="text-indigo-600 hover:underline">Изменить</Link>
|
||||||
</tbody>
|
<button type="button" class="text-red-600 hover:underline" @click="destroy(zone)">Удалить</button>
|
||||||
</table>
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</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',
|
dashboard: '/dashboard',
|
||||||
zones: {
|
zones: {
|
||||||
index: '/zones',
|
index: '/zones',
|
||||||
|
show: (id) => `/zones/${id}`,
|
||||||
create: '/zones/create',
|
create: '/zones/create',
|
||||||
store: '/zones',
|
store: '/zones',
|
||||||
edit: (id) => `/zones/${id}/edit`,
|
edit: (id) => `/zones/${id}/edit`,
|
||||||
@@ -24,6 +25,11 @@ export const routes = {
|
|||||||
turnOff: (id) => `/devices/${id}/turn-off`,
|
turnOff: (id) => `/devices/${id}/turn-off`,
|
||||||
setLevel: (id) => `/devices/${id}/set-level`,
|
setLevel: (id) => `/devices/${id}/set-level`,
|
||||||
},
|
},
|
||||||
|
deviceTypes: {
|
||||||
|
index: '/device-types',
|
||||||
|
create: '/device-types/create',
|
||||||
|
store: '/device-types',
|
||||||
|
},
|
||||||
automationRules: {
|
automationRules: {
|
||||||
index: '/automation-rules',
|
index: '/automation-rules',
|
||||||
create: '/automation-rules/create',
|
create: '/automation-rules/create',
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use App\Http\Controllers\AutomationRuleController;
|
use App\Http\Controllers\AutomationRuleController;
|
||||||
use App\Http\Controllers\DeviceController;
|
use App\Http\Controllers\DeviceController;
|
||||||
|
use App\Http\Controllers\DeviceTypeController;
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
use App\Http\Controllers\ZoneController;
|
use App\Http\Controllers\ZoneController;
|
||||||
use App\Models\AutomationRule;
|
use App\Models\AutomationRule;
|
||||||
@@ -30,8 +31,9 @@ Route::get('/dashboard', function (DeviceShadow $shadow) {
|
|||||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||||
|
|
||||||
Route::middleware(['auth', 'verified'])->group(function () {
|
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('devices', DeviceController::class);
|
||||||
|
Route::resource('device-types', DeviceTypeController::class)->only(['index', 'create', 'store']);
|
||||||
Route::resource('automation-rules', AutomationRuleController::class)
|
Route::resource('automation-rules', AutomationRuleController::class)
|
||||||
->except('show')
|
->except('show')
|
||||||
->parameters(['automation-rules' => 'automation_rule']);
|
->parameters(['automation-rules' => 'automation_rule']);
|
||||||
|
|||||||
@@ -130,4 +130,23 @@ class DeviceControllerTest extends TestCase
|
|||||||
->where('telemetry.0.value', 24.5)
|
->where('telemetry.0.value', 24.5)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_create_form_suggests_unregistered_external_ids(): void
|
||||||
|
{
|
||||||
|
$owner = User::factory()->create(['role' => UserRole::Owner]);
|
||||||
|
// Already registered — must be excluded even though device-control-service still knows it.
|
||||||
|
$this->makeDevice($owner, ['turn_on']);
|
||||||
|
|
||||||
|
$this->mock(DeviceShadow::class, function ($mock) {
|
||||||
|
$mock->shouldReceive('knownExternalIds')->once()->andReturn(['fan-1', 'sensor-2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
$response = $this->actingAs($owner)->get(route('devices.create'));
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
$response->assertInertia(fn (Assert $page) => $page
|
||||||
|
->component('Devices/Create')
|
||||||
|
->where('unregisteredExternalIds', ['sensor-2'])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Enums\UserRole;
|
||||||
|
use App\Models\DeviceType;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Inertia\Testing\AssertableInertia as Assert;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class DeviceTypeControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_owner_can_create_device_type(): void
|
||||||
|
{
|
||||||
|
$owner = User::factory()->create(['role' => UserRole::Owner]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($owner)->post(route('device-types.store'), [
|
||||||
|
'code' => 'smart_socket',
|
||||||
|
'category' => 'actuator',
|
||||||
|
'capabilities' => ['turn_on', 'turn_off'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertRedirect(route('device-types.index'));
|
||||||
|
$this->assertDatabaseHas('device_types', [
|
||||||
|
'code' => 'smart_socket',
|
||||||
|
'category' => 'actuator',
|
||||||
|
]);
|
||||||
|
$this->assertSame(
|
||||||
|
['turn_on', 'turn_off'],
|
||||||
|
DeviceType::where('code', 'smart_socket')->first()->capabilities,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_viewer_cannot_create_device_type(): void
|
||||||
|
{
|
||||||
|
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($viewer)->post(route('device-types.store'), [
|
||||||
|
'code' => 'smart_socket',
|
||||||
|
'category' => 'actuator',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertForbidden();
|
||||||
|
$this->assertDatabaseMissing('device_types', ['code' => 'smart_socket']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_code_must_be_unique(): void
|
||||||
|
{
|
||||||
|
$owner = User::factory()->create(['role' => UserRole::Owner]);
|
||||||
|
DeviceType::create(['code' => 'fan', 'category' => 'actuator', 'capabilities' => []]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($owner)->post(route('device-types.store'), [
|
||||||
|
'code' => 'fan',
|
||||||
|
'category' => 'actuator',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertSessionHasErrors('code');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_viewer_can_view_device_types_index(): void
|
||||||
|
{
|
||||||
|
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
||||||
|
DeviceType::create(['code' => 'fan', 'category' => 'actuator', 'capabilities' => ['turn_on']]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($viewer)->get(route('device-types.index'));
|
||||||
|
|
||||||
|
$response->assertOk();
|
||||||
|
$response->assertInertia(fn (Assert $page) => $page
|
||||||
|
->component('DeviceTypes/Index')
|
||||||
|
->where('deviceTypes.0.code', 'fan')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Enums\DeviceCategory;
|
||||||
use App\Enums\UserRole;
|
use App\Enums\UserRole;
|
||||||
|
use App\Models\Device;
|
||||||
|
use App\Models\DeviceType;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\Zone;
|
use App\Models\Zone;
|
||||||
|
use App\Services\DeviceShadow;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Inertia\Testing\AssertableInertia as Assert;
|
use Inertia\Testing\AssertableInertia as Assert;
|
||||||
use Tests\TestCase;
|
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
|
public function test_viewer_cannot_delete_zone(): void
|
||||||
{
|
{
|
||||||
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
$viewer = User::factory()->create(['role' => UserRole::Viewer]);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@
|
|||||||
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 },
|
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 },
|
||||||
"targets": [
|
"targets": [
|
||||||
{
|
{
|
||||||
"expr": "sum by (direction) (rate(device_control_health_transitions_total[5m]))",
|
"expr": "sum by (direction) (rate(device_health_transitions_total[5m]))",
|
||||||
"legendFormat": "{{direction}}",
|
"legendFormat": "{{direction}}",
|
||||||
"refId": "A"
|
"refId": "A"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ scrape_configs:
|
|||||||
- job_name: rule-engine-service
|
- job_name: rule-engine-service
|
||||||
static_configs:
|
static_configs:
|
||||||
- targets: ["rule-engine-service:9102"]
|
- targets: ["rule-engine-service:9102"]
|
||||||
|
|
||||||
|
- job_name: health-check-service
|
||||||
|
static_configs:
|
||||||
|
- targets: ["health-check-service:9103"]
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
- Поддерживает паттерн Device Shadow в Redis: `desired_state` (чего хочет
|
- Поддерживает паттерн Device Shadow в Redis: `desired_state` (чего хочет
|
||||||
пользователь) против `reported_state` (что подтвердило устройство), плюс
|
пользователь) против `reported_state` (что подтвердило устройство), плюс
|
||||||
`status` и `last_seen`.
|
`status` и `last_seen`.
|
||||||
- Ведёт health-check горутиной-тикером: переводит устройство в `offline`,
|
- Переводит устройство в `online` и публикует `device.status_changed` в
|
||||||
когда `last_seen` превышает таймаут, и публикует событие в RabbitMQ
|
RabbitMQ при получении телеметрии/ack (см. «Архитектурные решения»).
|
||||||
(`device.status_changed`) для будущего notification-service — как при
|
Обратный переход, `offline` по таймауту `last_seen`, — зона
|
||||||
уходе в офлайн, так и при возврате online.
|
ответственности отдельного health-check-service (см. его README).
|
||||||
|
|
||||||
Не входит в зону ответственности: решение о том, *когда* отправлять команду
|
Не входит в зону ответственности: решение о том, *когда* отправлять команду
|
||||||
на основе показаний датчиков — это логика rule-engine-service. Этот сервис
|
на основе показаний датчиков — это логика rule-engine-service. Этот сервис
|
||||||
@@ -54,7 +54,9 @@
|
|||||||
`GET /metrics` на том же порту, что и команды (`DEVICE_CONTROL_HTTP_PORT`)
|
`GET /metrics` на том же порту, что и команды (`DEVICE_CONTROL_HTTP_PORT`)
|
||||||
— не открывали отдельный порт ради этого. Счётчики/latency команд
|
— не открывали отдельный порт ради этого. Счётчики/latency команд
|
||||||
(TurnOn/TurnOff/SetLevel по action+outcome), MQTT-событий (telemetry/ack),
|
(TurnOn/TurnOff/SetLevel по action+outcome), MQTT-событий (telemetry/ack),
|
||||||
переходов online/offline health-check.
|
переходов в online (`device_health_transitions_total{direction="online"}`
|
||||||
|
— та же метрика по имени, что публикует health-check-service для
|
||||||
|
`direction="offline"`, см. его README).
|
||||||
|
|
||||||
## Запуск
|
## Запуск
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Command device-control-service exposes a gRPC API for issuing device
|
// Command device-control-service exposes a gRPC API for issuing device
|
||||||
// commands, dispatches them over MQTT, maintains the Device Shadow in
|
// commands, dispatches them over MQTT, and maintains the Device Shadow in
|
||||||
// Redis, and runs a health-check loop that flips stale devices offline.
|
// Redis. Offline detection lives in the separate health-check-service —
|
||||||
|
// this service only ever flips a device back to online, as a side effect
|
||||||
|
// of handling its telemetry/ack MQTT messages.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -21,7 +23,6 @@ import (
|
|||||||
|
|
||||||
devicecontrol "git.cactoz.su/cacto/home_automatization/proto/device_control"
|
devicecontrol "git.cactoz.su/cacto/home_automatization/proto/device_control"
|
||||||
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/config"
|
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/config"
|
||||||
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/healthcheck"
|
|
||||||
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/httpapi"
|
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/httpapi"
|
||||||
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/metrics"
|
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/metrics"
|
||||||
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/mqttclient"
|
"git.cactoz.su/cacto/home_automatization/services/device-control-service/internal/mqttclient"
|
||||||
@@ -75,18 +76,6 @@ func run(logger *slog.Logger) error {
|
|||||||
return fmt.Errorf("subscribe ack: %w", err)
|
return fmt.Errorf("subscribe ack: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
checker := healthcheck.New(shadowStore, cfg.HealthCheckTimeout, cfg.HealthCheckInterval, func(deviceID string) {
|
|
||||||
metrics.HealthTransitionsTotal.WithLabelValues("offline").Inc()
|
|
||||||
|
|
||||||
pubCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if err := publisher.PublishStatusChanged(pubCtx, deviceID, shadow.StatusOffline); err != nil {
|
|
||||||
logger.Error("publish offline event failed", "device_id", deviceID, "error", err)
|
|
||||||
}
|
|
||||||
}, logger)
|
|
||||||
checker.Start()
|
|
||||||
defer checker.Stop()
|
|
||||||
|
|
||||||
srv := server.New(shadowStore, mqttClient, logger)
|
srv := server.New(shadowStore, mqttClient, logger)
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCPort))
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCPort))
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -21,9 +20,6 @@ type Config struct {
|
|||||||
RedisDB int
|
RedisDB int
|
||||||
|
|
||||||
RabbitMQURL string
|
RabbitMQURL string
|
||||||
|
|
||||||
HealthCheckTimeout time.Duration
|
|
||||||
HealthCheckInterval time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() (Config, error) {
|
func Load() (Config, error) {
|
||||||
@@ -52,12 +48,6 @@ func Load() (Config, error) {
|
|||||||
if cfg.RedisDB, err = getEnvInt("REDIS_DB", 0); err != nil {
|
if cfg.RedisDB, err = getEnvInt("REDIS_DB", 0); err != nil {
|
||||||
return Config{}, err
|
return Config{}, err
|
||||||
}
|
}
|
||||||
if cfg.HealthCheckTimeout, err = getEnvDuration("DEVICE_CONTROL_HEALTHCHECK_TIMEOUT", 60*time.Second); err != nil {
|
|
||||||
return Config{}, err
|
|
||||||
}
|
|
||||||
if cfg.HealthCheckInterval, err = getEnvDuration("DEVICE_CONTROL_HEALTHCHECK_INTERVAL", 15*time.Second); err != nil {
|
|
||||||
return Config{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
@@ -80,15 +70,3 @@ func getEnvInt(key string, fallback int) (int, error) {
|
|||||||
}
|
}
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEnvDuration(key string, fallback time.Duration) (time.Duration, error) {
|
|
||||||
v := os.Getenv(key)
|
|
||||||
if v == "" {
|
|
||||||
return fallback, nil
|
|
||||||
}
|
|
||||||
d, err := time.ParseDuration(v)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("%s: %w", key, err)
|
|
||||||
}
|
|
||||||
return d, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -25,8 +25,14 @@ var (
|
|||||||
Help: "Total number of telemetry/ack messages received, by event type and outcome.",
|
Help: "Total number of telemetry/ack messages received, by event type and outcome.",
|
||||||
}, []string{"event_type", "outcome"})
|
}, []string{"event_type", "outcome"})
|
||||||
|
|
||||||
|
// Name matches health-check-service's own metric of the same purpose —
|
||||||
|
// this service only ever emits direction="online" (detected via MQTT
|
||||||
|
// telemetry/ack), health-check-service only ever emits "offline".
|
||||||
|
// Prometheus aggregates same-named metrics across scrape targets, so
|
||||||
|
// Grafana's `sum by (direction) (...)` panel works across both without
|
||||||
|
// a query change.
|
||||||
HealthTransitionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
HealthTransitionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||||
Name: "device_control_health_transitions_total",
|
Name: "device_health_transitions_total",
|
||||||
Help: "Total number of device online/offline transitions detected.",
|
Help: "Total number of device online/offline transitions detected.",
|
||||||
}, []string{"direction"})
|
}, []string{"direction"})
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
FROM golang:1.25-alpine AS build
|
||||||
|
WORKDIR /src
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
COPY . .
|
||||||
|
RUN CGO_ENABLED=0 go build -o /out/health-check-service ./cmd/health-check-service
|
||||||
|
|
||||||
|
FROM alpine:3.20
|
||||||
|
RUN adduser -D -u 10001 app
|
||||||
|
COPY --from=build /out/health-check-service /usr/local/bin/health-check-service
|
||||||
|
USER app
|
||||||
|
ENTRYPOINT ["/usr/local/bin/health-check-service"]
|
||||||
@@ -1,12 +1,51 @@
|
|||||||
# health-check-service (Go)
|
# health-check-service (Go)
|
||||||
|
|
||||||
Статус: пока не реализован. Может начаться как горутина внутри
|
Статус: реализован.
|
||||||
device-control-service и позже выделиться в отдельный контейнер — поэтому
|
|
||||||
с первого дня держим отдельную директорию, чтобы это разделение прошло
|
Изначально жил как горутина-тикер внутри device-control-service (см. его
|
||||||
безболезненно.
|
README/git-историю) — выделен в отдельный сервис по мере роста проекта,
|
||||||
|
как и планировалось с самого начала.
|
||||||
|
|
||||||
Зона ответственности:
|
Зона ответственности:
|
||||||
- Периодически (по тикеру) проверяет `last_seen` каждого устройства в Redis.
|
- Периодически (по тикеру, `HEALTH_CHECK_INTERVAL`) обходит все известные
|
||||||
- Когда устройство превышает таймаут офлайна, переводит его `status` в
|
устройства из Redis-множества `devices:known` и проверяет `last_seen`
|
||||||
`offline` и публикует событие для notification-service.
|
каждого.
|
||||||
- Хороший повод продемонстрировать горутины + graceful shutdown в Go.
|
- Когда устройство превышает таймаут офлайна (`HEALTH_CHECK_TIMEOUT`),
|
||||||
|
переводит его `status` в `offline` в том же Redis (Device Shadow
|
||||||
|
keyspace, который device-control-service читает/пишет) и публикует
|
||||||
|
`device.status_changed` в RabbitMQ.
|
||||||
|
|
||||||
|
Не входит в зону ответственности:
|
||||||
|
- **Детекция перехода в online.** Это происходит как побочный эффект
|
||||||
|
MQTT-подписки device-control-service на `devices/+/telemetry` и
|
||||||
|
`devices/+/ack` (там уже есть открытое соединение и подписка ради
|
||||||
|
reported_state) — заводить для этого отдельный сервис с собственной
|
||||||
|
MQTT-подпиской избыточно. health-check-service работает только "в одну
|
||||||
|
сторону": online → offline.
|
||||||
|
- Хранение/интерпретация телеметрии — это ingest-service.
|
||||||
|
- Знание о существовании устройств из PostgreSQL — этот сервис, как и
|
||||||
|
device-control-service, не подключается к Postgres: список устройств для
|
||||||
|
сканирования берётся из того же Redis-множества `devices:known`
|
||||||
|
(пополняется device-control-service).
|
||||||
|
|
||||||
|
## Метрики
|
||||||
|
|
||||||
|
`GET /metrics` на `HEALTH_CHECK_METRICS_PORT`. Счётчик
|
||||||
|
`device_health_transitions_total{direction="offline"}` — намеренно та же
|
||||||
|
метрика (по имени), что device-control-service публикует для
|
||||||
|
`direction="online"`: Prometheus агрегирует одноимённые метрики с разных
|
||||||
|
таргетов прозрачно, так что дашборд Grafana не пришлось переделывать под
|
||||||
|
разделение на два сервиса.
|
||||||
|
|
||||||
|
## Запуск
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd services/health-check-service
|
||||||
|
go test ./...
|
||||||
|
go build ./cmd/health-check-service
|
||||||
|
```
|
||||||
|
|
||||||
|
Конфигурация — через переменные окружения (секция health-check-service в
|
||||||
|
корневом `.env.example`). Не зависит от `proto/` — работает только с Redis
|
||||||
|
и RabbitMQ, поэтому в отличие от device-control-service/rule-engine-service
|
||||||
|
сборка Docker-образа идёт из собственного контекста (не корня репозитория).
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
// Command health-check-service periodically scans every known device in
|
||||||
|
// Redis and flips it to offline once its last_seen exceeds the configured
|
||||||
|
// timeout, publishing a device.status_changed event to RabbitMQ for each
|
||||||
|
// transition. It never marks a device online — that happens as a side
|
||||||
|
// effect of device-control-service's own MQTT telemetry/ack handling — this
|
||||||
|
// service only ever detects silence.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
|
"git.cactoz.su/cacto/home_automatization/services/health-check-service/internal/config"
|
||||||
|
"git.cactoz.su/cacto/home_automatization/services/health-check-service/internal/healthcheck"
|
||||||
|
"git.cactoz.su/cacto/home_automatization/services/health-check-service/internal/metrics"
|
||||||
|
"git.cactoz.su/cacto/home_automatization/services/health-check-service/internal/rabbitmq"
|
||||||
|
"git.cactoz.su/cacto/home_automatization/services/health-check-service/internal/shadow"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
|
|
||||||
|
if err := run(logger); err != nil {
|
||||||
|
logger.Error("health-check-service exited with error", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(logger *slog.Logger) error {
|
||||||
|
cfg, err := config.Load()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
store, err := shadow.Connect(ctx, cfg.RedisAddr, cfg.RedisPassword, cfg.RedisDB)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer store.Close()
|
||||||
|
|
||||||
|
publisher, err := rabbitmq.Connect(cfg.RabbitMQURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer publisher.Close()
|
||||||
|
|
||||||
|
checker := healthcheck.New(store, cfg.HealthCheckTimeout, cfg.HealthCheckInterval, func(deviceID string) {
|
||||||
|
metrics.HealthTransitionsTotal.WithLabelValues("offline").Inc()
|
||||||
|
|
||||||
|
pubCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := publisher.PublishStatusChanged(pubCtx, deviceID, shadow.StatusOffline); err != nil {
|
||||||
|
logger.Error("publish offline event failed", "device_id", deviceID, "error", err)
|
||||||
|
}
|
||||||
|
}, logger)
|
||||||
|
checker.Start()
|
||||||
|
defer checker.Stop()
|
||||||
|
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", cfg.MetricsPort),
|
||||||
|
Handler: promhttp.Handler(),
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
logger.Info("metrics server started", "metrics_port", cfg.MetricsPort)
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
logger.Error("metrics server stopped", "error", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
logger.Info("health-check-service started",
|
||||||
|
"timeout", cfg.HealthCheckTimeout, "interval", cfg.HealthCheckInterval)
|
||||||
|
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
<-stop
|
||||||
|
|
||||||
|
logger.Info("shutting down")
|
||||||
|
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||||
|
logger.Error("metrics server shutdown failed", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
module git.cactoz.su/cacto/home_automatization/services/health-check-service
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alicebob/miniredis/v2 v2.38.0
|
||||||
|
github.com/prometheus/client_golang v1.24.1
|
||||||
|
github.com/rabbitmq/amqp091-go v1.13.0
|
||||||
|
github.com/redis/go-redis/v9 v9.22.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
|
github.com/prometheus/client_model v0.6.2 // indirect
|
||||||
|
github.com/prometheus/common v0.70.1 // indirect
|
||||||
|
github.com/prometheus/procfs v0.21.1 // indirect
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
|
golang.org/x/sys v0.47.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.36.11 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
github.com/alicebob/miniredis/v2 v2.38.0 h1:nZAzCR+Lj+Vxk4ZXzm2NuKq2O33RXj1XxJ2e2uP9jiw=
|
||||||
|
github.com/alicebob/miniredis/v2 v2.38.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
|
||||||
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
|
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
|
||||||
|
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/prometheus/client_golang v1.24.1 h1:JnJkREXzWxUdCuPFpIWZiPispT9xVV59uiuyR2bPlnU=
|
||||||
|
github.com/prometheus/client_golang v1.24.1/go.mod h1:F+oSRECHg4sse5ucfYpYDeIv/hu68Zo0uoHKetWnzcE=
|
||||||
|
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||||
|
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||||
|
github.com/prometheus/common v0.70.1 h1:1HvjP4D5oL3t8RsPlwxA9onvvStjtIHYE5XuuwOi/PY=
|
||||||
|
github.com/prometheus/common v0.70.1/go.mod h1:VdFUQDMZK3VLkurFUVhia6uys/0suUp86TJz5qbJRhc=
|
||||||
|
github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI=
|
||||||
|
github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.13.0 h1:L8NA1WtF76C6KA3LAoufjfLgbist/If1UQYcsOjtxXA=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.13.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||||
|
github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
|
||||||
|
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
|
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||||
|
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
|
||||||
|
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||||
|
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
|
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||||
|
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
// Package config loads health-check-service settings from environment
|
||||||
|
// variables, matching the names used in the repo-root .env.example.
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
RedisAddr string
|
||||||
|
RedisPassword string
|
||||||
|
RedisDB int
|
||||||
|
|
||||||
|
RabbitMQURL string
|
||||||
|
|
||||||
|
HealthCheckTimeout time.Duration
|
||||||
|
HealthCheckInterval time.Duration
|
||||||
|
|
||||||
|
MetricsPort int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() (Config, error) {
|
||||||
|
cfg := Config{
|
||||||
|
RedisAddr: fmt.Sprintf("%s:%s", getEnv("REDIS_HOST", "localhost"), getEnv("REDIS_PORT", "6379")),
|
||||||
|
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
||||||
|
|
||||||
|
RabbitMQURL: fmt.Sprintf("amqp://%s:%s@%s:%s/",
|
||||||
|
getEnv("RABBITMQ_USER", "guest"),
|
||||||
|
getEnv("RABBITMQ_PASSWORD", "guest"),
|
||||||
|
getEnv("RABBITMQ_HOST", "localhost"),
|
||||||
|
getEnv("RABBITMQ_PORT", "5672"),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if cfg.RedisDB, err = getEnvInt("REDIS_DB", 0); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
if cfg.HealthCheckTimeout, err = getEnvDuration("HEALTH_CHECK_TIMEOUT", 60*time.Second); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
if cfg.HealthCheckInterval, err = getEnvDuration("HEALTH_CHECK_INTERVAL", 15*time.Second); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
if cfg.MetricsPort, err = getEnvInt("HEALTH_CHECK_METRICS_PORT", 9103); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key, fallback string) string {
|
||||||
|
if v := os.Getenv(key); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnvInt(key string, fallback int) (int, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return fallback, nil
|
||||||
|
}
|
||||||
|
n, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("%s: %w", key, err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnvDuration(key string, fallback time.Duration) (time.Duration, error) {
|
||||||
|
v := os.Getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
return fallback, nil
|
||||||
|
}
|
||||||
|
d, err := time.ParseDuration(v)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("%s: %w", key, err)
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// Package metrics defines health-check-service's Prometheus metrics.
|
||||||
|
// Registered automatically (via promauto) into the default registry on
|
||||||
|
// import; served on /metrics.
|
||||||
|
//
|
||||||
|
// device_health_transitions_total is intentionally the same metric name
|
||||||
|
// device-control-service exposes for its "online" transitions (detected via
|
||||||
|
// MQTT touch) — this service only ever emits direction="offline". Prometheus
|
||||||
|
// aggregates same-named metrics across scrape targets/jobs transparently, so
|
||||||
|
// Grafana's `sum by (direction) (...)` panel keeps working across the split
|
||||||
|
// without a query change.
|
||||||
|
package metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
|
)
|
||||||
|
|
||||||
|
var HealthTransitionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||||
|
Name: "device_health_transitions_total",
|
||||||
|
Help: "Total number of device online/offline transitions detected.",
|
||||||
|
}, []string{"direction"})
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
// Package rabbitmq publishes device online/offline transitions for
|
||||||
|
// notification-service to consume. Delivery reliability matters more than
|
||||||
|
// latency here, so messages are persistent and the queue is durable.
|
||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
const StatusChangedQueue = "device.status_changed"
|
||||||
|
|
||||||
|
type Publisher struct {
|
||||||
|
conn *amqp.Connection
|
||||||
|
ch *amqp.Channel
|
||||||
|
}
|
||||||
|
|
||||||
|
func Connect(url string) (*Publisher, error) {
|
||||||
|
conn, err := amqp.Dial(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("dial rabbitmq: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ch, err := conn.Channel()
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, fmt.Errorf("open channel: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := ch.QueueDeclare(StatusChangedQueue, true, false, false, false, nil); err != nil {
|
||||||
|
ch.Close()
|
||||||
|
conn.Close()
|
||||||
|
return nil, fmt.Errorf("declare queue %s: %w", StatusChangedQueue, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Publisher{conn: conn, ch: ch}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Publisher) Close() error {
|
||||||
|
if err := p.ch.Close(); err != nil {
|
||||||
|
p.conn.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return p.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
type statusChangedEvent struct {
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
At time.Time `json:"at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublishStatusChanged emits one event when a device transitions online or offline.
|
||||||
|
func (p *Publisher) PublishStatusChanged(ctx context.Context, deviceID, status string) error {
|
||||||
|
body, err := json.Marshal(statusChangedEvent{
|
||||||
|
DeviceID: deviceID,
|
||||||
|
Status: status,
|
||||||
|
At: time.Now(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("marshal event: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.ch.PublishWithContext(ctx, "", StatusChangedQueue, false, false, amqp.Publishing{
|
||||||
|
ContentType: "application/json",
|
||||||
|
DeliveryMode: amqp.Persistent,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Body: body,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("publish status change for device %s: %w", deviceID, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
// Package shadow gives health-check-service read/offline-detection access
|
||||||
|
// to the Device Shadow keyspace in Redis. device-control-service owns the
|
||||||
|
// full Shadow API (desired/reported state patches, online-touch on
|
||||||
|
// telemetry/ack) — this is deliberately the narrow subset a periodic
|
||||||
|
// offline-scanner needs, kept as its own small copy rather than a shared
|
||||||
|
// module: the two services only overlap on reading/writing `status` and
|
||||||
|
// `last_seen`, and duplicating ~2 key-naming functions is cheaper than a
|
||||||
|
// fourth shared Go module for that.
|
||||||
|
package shadow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
const knownDevicesKey = "devices:known"
|
||||||
|
|
||||||
|
func statusKey(deviceID string) string { return "device:" + deviceID + ":status" }
|
||||||
|
func lastSeenKey(deviceID string) string { return "device:" + deviceID + ":last_seen" }
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusOnline = "online"
|
||||||
|
StatusOffline = "offline"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Store struct {
|
||||||
|
rdb *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(rdb *redis.Client) *Store {
|
||||||
|
return &Store{rdb: rdb}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Connect(ctx context.Context, addr, password string, db int) (*Store, error) {
|
||||||
|
rdb := redis.NewClient(&redis.Options{Addr: addr, Password: password, DB: db})
|
||||||
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("ping redis: %w", err)
|
||||||
|
}
|
||||||
|
return New(rdb), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) Close() error {
|
||||||
|
return s.rdb.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// KnownDevices lists every device ID device-control-service has heard from.
|
||||||
|
func (s *Store) KnownDevices(ctx context.Context) ([]string, error) {
|
||||||
|
ids, err := s.rdb.SMembers(ctx, knownDevicesKey).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list known devices: %w", err)
|
||||||
|
}
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkOfflineIfStale flips deviceID to offline if it is currently online and
|
||||||
|
// its last_seen is older than timeout. It reports whether a change was made.
|
||||||
|
func (s *Store) MarkOfflineIfStale(ctx context.Context, deviceID string, timeout time.Duration) (changed bool, err error) {
|
||||||
|
status, err := s.rdb.Get(ctx, statusKey(deviceID)).Result()
|
||||||
|
if err == redis.Nil || status != StatusOnline {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("get status for %s: %w", deviceID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSeenRaw, err := s.rdb.Get(ctx, lastSeenKey(deviceID)).Result()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return false, fmt.Errorf("get last_seen for %s: %w", deviceID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastSeen time.Time
|
||||||
|
if lastSeenRaw != "" {
|
||||||
|
sec, err := strconv.ParseInt(lastSeenRaw, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("parse last_seen for %s: %w", deviceID, err)
|
||||||
|
}
|
||||||
|
lastSeen = time.Unix(sec, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Since(lastSeen) <= timeout {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err := s.rdb.Set(ctx, statusKey(deviceID), StatusOffline, 0).Err(); err != nil {
|
||||||
|
return false, fmt.Errorf("set status offline for %s: %w", deviceID, err)
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package shadow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/alicebob/miniredis/v2"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestStore(t *testing.T) *Store {
|
||||||
|
t.Helper()
|
||||||
|
mr := miniredis.RunT(t)
|
||||||
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||||
|
t.Cleanup(func() { rdb.Close() })
|
||||||
|
return New(rdb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// markOnline seeds Redis the way device-control-service's Touch() would,
|
||||||
|
// without depending on that service's code.
|
||||||
|
func markOnline(t *testing.T, s *Store, deviceID string, lastSeen time.Time) {
|
||||||
|
t.Helper()
|
||||||
|
ctx := context.Background()
|
||||||
|
if err := s.rdb.SAdd(ctx, knownDevicesKey, deviceID).Err(); err != nil {
|
||||||
|
t.Fatalf("seed known device: %v", err)
|
||||||
|
}
|
||||||
|
if err := s.rdb.Set(ctx, statusKey(deviceID), StatusOnline, 0).Err(); err != nil {
|
||||||
|
t.Fatalf("seed status: %v", err)
|
||||||
|
}
|
||||||
|
if err := s.rdb.Set(ctx, lastSeenKey(deviceID), lastSeen.Unix(), 0).Err(); err != nil {
|
||||||
|
t.Fatalf("seed last_seen: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKnownDevices_ListsSeededDevices(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
s := newTestStore(t)
|
||||||
|
markOnline(t, s, "d1", time.Now())
|
||||||
|
markOnline(t, s, "d2", time.Now())
|
||||||
|
|
||||||
|
got, err := s.KnownDevices(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("known devices: %v", err)
|
||||||
|
}
|
||||||
|
if len(got) != 2 {
|
||||||
|
t.Fatalf("got %d known devices, want 2 (%v)", len(got), got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkOfflineIfStale(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
s := newTestStore(t)
|
||||||
|
markOnline(t, s, "d1", time.Now())
|
||||||
|
|
||||||
|
changed, err := s.MarkOfflineIfStale(ctx, "d1", time.Hour)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mark offline (not stale): %v", err)
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
t.Fatal("device just touched should not be considered stale")
|
||||||
|
}
|
||||||
|
|
||||||
|
changed, err = s.MarkOfflineIfStale(ctx, "d1", -time.Second) // any age counts as stale
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mark offline (stale): %v", err)
|
||||||
|
}
|
||||||
|
if !changed {
|
||||||
|
t.Fatal("expected status to flip to offline")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Already offline: a second call should report no further change.
|
||||||
|
changed, err = s.MarkOfflineIfStale(ctx, "d1", -time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mark offline (already offline): %v", err)
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
t.Fatal("expected no change once already offline")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkOfflineIfStale_UnknownDeviceIsNoop(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
s := newTestStore(t)
|
||||||
|
|
||||||
|
changed, err := s.MarkOfflineIfStale(ctx, "ghost", time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
t.Fatal("unknown device should never be reported as changed")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user