homeos/app/Models/Device.php

59 lines
1.5 KiB
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', 'demo', 'last_seen_at',
];
protected $casts = [
'config' => 'array',
'demo' => 'boolean',
'last_seen_at' => 'datetime',
];
public function room(): BelongsTo
{
return $this->belongsTo(Room::class);
}
public function entities(): HasMany
{
return $this->hasMany(Entity::class);
}
/**
* Reachable now. Real devices must have been seen recently (kept fresh by MQTT); a
* never-seen real device is offline. Only demo devices are "assumed reachable" when
* they have no last_seen, so the seeded demo doesn't rot.
*/
public function isOnline(): bool
{
if ($this->status !== 'active') {
return false;
}
if ($this->last_seen_at !== null) {
return $this->last_seen_at->gt(now()->subMinutes(10));
}
return (bool) $this->demo;
}
/** Cloud-bridged (e.g. Ring) — surfaced with a "Cloud" badge so the dependency is visible. */
public function isCloud(): bool
{
return (bool) data_get($this->config, 'cloud', false);
}
}