sackey 6b878bb0a0
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Initial commit
2025-09-12 16:19:56 +00:00

135 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Event extends Model
{
use HasFactory;
protected $fillable = [
'device_id',
'position_id',
'geofence_id',
'traccar_event_id',
'type',
'event_time',
'attributes',
'acknowledged',
'acknowledged_by',
'acknowledged_at',
];
protected $casts = [
'event_time' => 'datetime',
'attributes' => 'array',
'acknowledged' => 'boolean',
'acknowledged_at' => 'datetime',
];
/**
* Accessor for event_type to map to type column
*/
public function getEventTypeAttribute()
{
return $this->type;
}
/**
* Get the device that triggered this event
*/
public function device(): BelongsTo
{
return $this->belongsTo(Device::class);
}
/**
* Get the position where this event occurred
*/
public function position(): BelongsTo
{
return $this->belongsTo(Position::class);
}
/**
* Get the geofence related to this event
*/
public function geofence(): BelongsTo
{
return $this->belongsTo(Geofence::class);
}
/**
* Get the user who acknowledged this event
*/
public function acknowledgedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'acknowledged_by');
}
/**
* Scope for unacknowledged events
*/
public function scopeUnacknowledged($query)
{
return $query->where('acknowledged', false);
}
/**
* Scope for events by type
*/
public function scopeByType($query, $type)
{
return $query->where('type', $type);
}
/**
* Get event severity level
*/
public function getSeverity(): string
{
return match ($this->type) {
'alarm' => 'high',
'geofenceEnter', 'geofenceExit' => 'medium',
'overspeed' => 'medium',
'deviceOnline', 'deviceOffline' => 'low',
default => 'low'
};
}
/**
* Get event color based on type
*/
public function getEventColor(): string
{
return match ($this->type) {
'alarm' => 'red',
'geofenceEnter' => 'green',
'geofenceExit' => 'orange',
'overspeed' => 'red',
'deviceOnline' => 'green',
'deviceOffline' => 'red',
default => 'gray'
};
}
/**
* Get human readable event type
*/
public function getReadableType(): string
{
return match ($this->type) {
'geofenceEnter' => 'Geofence Enter',
'geofenceExit' => 'Geofence Exit',
'overspeed' => 'Overspeed',
'deviceOnline' => 'Device Online',
'deviceOffline' => 'Device Offline',
'alarm' => 'Alarm',
default => ucfirst($this->type)
};
}
}