Auto-create activity log saat data PIR, Vibration, DoorAccess masuk

This commit is contained in:
Adamalfantino 2026-06-08 21:03:40 +07:00
parent 4e3d5a6902
commit 74d576eb4d
3 changed files with 44 additions and 0 deletions

View File

@ -123,6 +123,20 @@ public function receiveData(Request $request): JsonResponse
// Tandai device sebagai online
Device::markOnline($data['device_id']);
// Tulis activity log
$sensor = \App\Models\Sensor::where('device_id', $data['device_id'])->where('type', 'reed_switch')->first();
\App\Models\ActivityLog::create([
'device_id' => $data['device_id'],
'sensor_id' => $sensor?->id,
'event_type' => $data['door_opened'] ? 'door_opened' : 'door_closed',
'severity' => $isSuspicious ? 'warning' : 'info',
'title' => $data['door_opened'] ? 'Rak Dibuka' : 'Rak Ditutup',
'description'=> "Reed switch mendeteksi rak " . ($data['door_opened'] ? 'dibuka' : 'ditutup') . " di lokasi " . ($data['door_location'] ?? 'rack') . ". Durasi: {$durationSeconds}s.",
'event_data' => ['door_reading_id' => $doorReading->id, 'access_type' => $accessType, 'duration' => $durationSeconds],
'location' => $sensor?->device?->location ?? 'Rak A - Lantai 1',
'event_time' => $recordedAt,
]);
// Jika akses mencurigakan atau tidak sah, buat alert dan kirim notifikasi
$alertSent = false;
if ($isSuspicious || !$isAuthorizedAccess || $accessType === 'forced_entry') {

View File

@ -112,6 +112,22 @@ public function receiveData(Request $request): JsonResponse
// Tandai device sebagai online
Device::markOnline($data['device_id']);
// Tulis activity log
if ($data['motion_detected']) {
$sensor = \App\Models\Sensor::where('device_id', $data['device_id'])->where('type', 'pir')->first();
\App\Models\ActivityLog::create([
'device_id' => $data['device_id'],
'sensor_id' => $sensor?->id,
'event_type' => $isSuspicious ? 'motion_detected' : 'motion_detected',
'severity' => $isSuspicious ? 'warning' : 'info',
'title' => $isSuspicious ? 'Gerakan Mencurigakan Terdeteksi' : 'Gerakan Terdeteksi',
'description'=> "Sensor PIR mendeteksi gerakan di zona {$pirReading->detection_zone}. Intensitas: {$motionIntensity}%.",
'event_data' => ['pir_reading_id' => $pirReading->id, 'motion_type' => $motionType, 'intensity' => $motionIntensity],
'location' => $sensor?->device?->location ?? 'Rak A - Lantai 1',
'event_time' => $recordedAt,
]);
}
// Jika gerakan mencurigakan, buat alert dan kirim notifikasi
$alertSent = false;
if ($isSuspicious || $motionType === 'unauthorized') {

View File

@ -88,6 +88,20 @@ public function receiveData(Request $request): JsonResponse
// Tandai device sebagai online
Device::markOnline($data['device_id']);
// Tulis activity log
$sensor = \App\Models\Sensor::where('device_id', $data['device_id'])->where('type', 'vibration')->first();
\App\Models\ActivityLog::create([
'device_id' => $data['device_id'],
'sensor_id' => $sensor?->id,
'event_type' => $isAbnormal ? 'vibration_detected' : 'system_normal',
'severity' => $status === 'critical' ? 'critical' : ($isAbnormal ? 'warning' : 'info'),
'title' => $isAbnormal ? 'Getaran Abnormal Terdeteksi' : 'Getaran Normal',
'description'=> "Sensor SW-420 mendeteksi getaran. Magnitude: " . round($magnitude, 2) . ", Status: {$status}.",
'event_data' => ['vibration_reading_id' => $vibrationReading->id, 'magnitude' => $magnitude, 'status' => $status],
'location' => $sensor?->device?->location ?? 'Rak A - Lantai 1',
'event_time' => now(),
]);
// Jika getaran abnormal, buat alert dan kirim notifikasi
if ($isAbnormal) {
$this->handleAbnormalVibration($vibrationReading);