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', 'access_method' => 'nullable|string|in:keycard,manual,force,emergency,maintenance,unknown',
'user_id_card' => 'nullable|string|max:50', 'user_id_card' => 'nullable|string|max:50',
'duration_seconds' => 'nullable|integer|min:0', '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', 'is_forced_entry' => 'nullable|boolean',
'door_opened_at' => 'nullable|date', 'door_opened_at' => 'nullable|date',
'door_closed_at' => 'nullable|date', 'door_closed_at' => 'nullable|date',

View File

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

View File

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