Fix monitoring: gunakan DoorAccessReading untuk riwayat reed switch

This commit is contained in:
Adamalfantino 2026-05-27 04:56:39 +07:00
parent e12b29dde8
commit 181c619cb1
3 changed files with 16 additions and 22 deletions

View File

@ -35,7 +35,7 @@ public function receiveData(Request $request): JsonResponse
'access_method' => 'nullable|string|in:keycard,manual,force,emergency,maintenance,unknown',
'user_id_card' => 'nullable|string|max:50',
'duration_seconds' => 'nullable|integer|min:0',
'door_location' => 'nullable|string|in:front_door,back_door,side_door,main_entrance',
'door_location' => 'nullable|string|in:front_door,back_door,side_door,main_entrance,rack',
'is_forced_entry' => 'nullable|boolean',
'door_opened_at' => 'nullable|date',
'door_closed_at' => 'nullable|date',

View File

@ -278,18 +278,19 @@
@endif
</div>
@if($reedLatest)
@php $reedOpen = $reedLatest->door_opened ?? $reedLatest->door_open ?? false; @endphp
<div class="mb-3 md:mb-4">
<p id="reed-status-text" class="text-xl md:text-3xl font-display font-bold {{ $reedLatest->door_open ? 'text-red-600' : 'text-green-600' }} mb-1">
{{ $reedLatest->door_open ? '⚠ Terbuka' : '✓ Tertutup' }}
<p id="reed-status-text" class="text-xl md:text-3xl font-display font-bold {{ $reedOpen ? 'text-red-600' : 'text-green-600' }} mb-1">
{{ $reedOpen ? '⚠ Terbuka' : '✓ Tertutup' }}
</p>
<p class="text-gray-500 text-xs md:text-sm">
Level: <span class="font-semibold text-gray-700">{{ ucfirst($reedLatest->access_level ?? '-') }}</span>
· Durasi: <span class="font-semibold text-gray-700">{{ $reedLatest->open_duration_seconds ?? 0 }}s</span>
Tipe: <span class="font-semibold text-gray-700">{{ ucfirst($reedLatest->access_type ?? $reedLatest->access_level ?? 'manual') }}</span>
· Durasi: <span class="font-semibold text-gray-700">{{ $reedLatest->duration_seconds ?? $reedLatest->open_duration_seconds ?? 0 }}s</span>
</p>
</div>
<div class="pt-3 border-t border-gray-100 flex justify-between text-xs md:text-sm">
<span class="text-gray-500">Update:</span>
<span id="reed-update" class="{{ $reedLatest->door_open ? 'text-red-600' : 'text-green-600' }} font-semibold">{{ $reedLatest->recorded_at->diffForHumans() }}</span>
<span id="reed-update" class="{{ $reedOpen ? 'text-red-600' : 'text-green-600' }} font-semibold">{{ $reedLatest->recorded_at->diffForHumans() }}</span>
</div>
@else
<p class="text-lg md:text-2xl font-display font-bold text-gray-400 mb-2">Belum Ada Data</p>
@ -364,14 +365,15 @@
</div>
<div class="space-y-3">
@forelse($reedHistory as $reed)
<div class="flex items-center justify-between p-3 rounded-xl {{ $reed->door_open ? 'bg-red-50 border border-red-100' : 'bg-gray-50 border border-gray-100' }}">
@php $reedOpen = $reed->door_opened ?? $reed->door_open ?? false; @endphp
<div class="flex items-center justify-between p-3 rounded-xl {{ $reedOpen ? 'bg-red-50 border border-red-100' : 'bg-gray-50 border border-gray-100' }}">
<div>
<p class="text-sm font-semibold {{ $reed->door_open ? 'text-red-700' : 'text-gray-700' }}">
{{ $reed->door_open ? 'Terbuka' : 'Tertutup' }} {{ ucfirst($reed->access_level ?? 'normal') }}
<p class="text-sm font-semibold {{ $reedOpen ? 'text-red-700' : 'text-gray-700' }}">
{{ $reedOpen ? 'Terbuka' : 'Tertutup' }} {{ ucfirst($reed->access_type ?? $reed->access_level ?? 'manual') }}
</p>
<p class="text-xs text-gray-400">{{ $reed->recorded_at->format('d M H:i:s') }}</p>
</div>
<div class="w-2 h-2 rounded-full {{ $reed->door_open ? 'bg-red-500' : 'bg-green-500' }}"></div>
<div class="w-2 h-2 rounded-full {{ $reedOpen ? 'bg-red-500' : 'bg-green-500' }}"></div>
</div>
@empty
<p class="text-gray-400 text-sm text-center py-4">Belum ada data</p>
@ -495,7 +497,7 @@ function toggleSidebar() {
if (reedRes?.success && reedRes.data?.length > 0) {
const d = reedRes.data[0];
const age = (now - new Date(d.recorded_at).getTime()) / 1000;
const active = d.door_open && age <= DETECTION_WINDOW;
const active = (d.door_opened ?? d.door_open ?? false) && age <= DETECTION_WINDOW;
document.getElementById('reed-status-text').textContent = active ? '⚠ Terbuka' : '✓ Tertutup';
document.getElementById('reed-status-text').className = 'text-xl md:text-3xl font-display font-bold mb-1 ' + (active ? 'text-red-600' : 'text-green-600');
document.getElementById('reed-update').textContent = 'Baru saja';

View File

@ -131,11 +131,8 @@
$vibAktif = \App\Models\VibrationReading::where('recorded_at', '>=', now()->subSeconds(30))
->where('is_abnormal', true)->exists();
// Reed Switch: cek DoorAccessReading (data dari ESP32 via /api/door-access/data)
// DAN ReedSwitchReading (data dari endpoint /api/door-access/data lama)
$reedAktif = \App\Models\DoorAccessReading::where('recorded_at', '>=', now()->subSeconds(30))
->where('door_opened', true)->exists()
|| \App\Models\ReedSwitchReading::where('recorded_at', '>=', now()->subSeconds(30))
->where('door_open', true)->exists();
->where('door_opened', true)->exists();
// Status device online — berdasarkan last_seen dalam 2 menit terakhir
$device = \App\Models\Device::orderBy('last_seen', 'desc')->first();
@ -154,11 +151,8 @@
->orderBy('recorded_at', 'desc')->first();
$vibrationLatest = \App\Models\VibrationReading::with('device')
->orderBy('recorded_at', 'desc')->first();
// Reed Switch: ambil dari DoorAccessReading (data ESP32 terbaru)
// fallback ke ReedSwitchReading jika tidak ada
// Reed Switch: data dari ESP32 disimpan di DoorAccessReading via /api/door-access/data
$reedLatest = \App\Models\DoorAccessReading::with('device')
->orderBy('recorded_at', 'desc')->first()
?? \App\Models\ReedSwitchReading::with('device')
->orderBy('recorded_at', 'desc')->first();
// Ambil data sensor dari tabel sensors untuk info nama & status
@ -172,9 +166,7 @@
$vibrationCount24h = \App\Models\VibrationReading::where('recorded_at', '>=', now()->subHours(24))
->where('is_abnormal', true)->count();
$reedCount24h = \App\Models\DoorAccessReading::where('recorded_at', '>=', now()->subHours(24))
->where('door_opened', true)->count()
+ \App\Models\ReedSwitchReading::where('recorded_at', '>=', now()->subHours(24))
->where('door_open', true)->count();
->where('door_opened', true)->count();
// Riwayat 5 data terakhir tiap sensor
$pirHistory = \App\Models\PirReading::orderBy('recorded_at', 'desc')->limit(5)->get();