84 lines
1.7 KiB
PHP
84 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Command extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'device_id',
|
|
'user_id',
|
|
'traccar_command_id',
|
|
'type',
|
|
'description',
|
|
'parameters',
|
|
'attributes',
|
|
'sent_at',
|
|
'delivered_at',
|
|
'executed_at',
|
|
'expires_at',
|
|
'status',
|
|
'response',
|
|
'error_message',
|
|
];
|
|
|
|
protected $casts = [
|
|
'attributes' => 'array',
|
|
'parameters' => 'array',
|
|
'sent_at' => 'datetime',
|
|
'delivered_at' => 'datetime',
|
|
'executed_at' => 'datetime',
|
|
'expires_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Get the device this command was sent to
|
|
*/
|
|
public function device(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Device::class);
|
|
}
|
|
|
|
/**
|
|
* Get the user who sent this command
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Scope for pending commands
|
|
*/
|
|
public function scopePending($query)
|
|
{
|
|
return $query->where('status', 'pending');
|
|
}
|
|
|
|
/**
|
|
* Scope for successful commands
|
|
*/
|
|
public function scopeSuccessful($query)
|
|
{
|
|
return $query->where('status', 'success');
|
|
}
|
|
|
|
/**
|
|
* Get status color
|
|
*/
|
|
public function getStatusColor(): string
|
|
{
|
|
return match ($this->status) {
|
|
'success' => 'green',
|
|
'failed' => 'red',
|
|
'pending' => 'yellow',
|
|
default => 'gray'
|
|
};
|
|
}
|
|
}
|