42 lines
936 B
PHP
42 lines
936 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasUuid;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Device extends Model
|
|
{
|
|
use HasUuid;
|
|
|
|
protected $fillable = [
|
|
'uuid', 'name', 'vendor', 'model', 'protocol', 'config',
|
|
'room_id', 'status', 'last_seen_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'config' => 'array',
|
|
'last_seen_at' => 'datetime',
|
|
];
|
|
|
|
public function room(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Room::class);
|
|
}
|
|
|
|
public function entities(): HasMany
|
|
{
|
|
return $this->hasMany(Entity::class);
|
|
}
|
|
|
|
/** Active and seen recently. */
|
|
public function isOnline(): bool
|
|
{
|
|
return $this->status === 'active'
|
|
&& $this->last_seen_at !== null
|
|
&& $this->last_seen_at->gt(now()->subMinutes(10));
|
|
}
|
|
}
|