@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Device extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $fillable = ['tenant_id', 'name', 'hostname', 'device_vendor_id', 'device_type_id', 'device_model_id', 'serial_number', 'management_address', 'connection_type', 'management_port', 'connection_ports', 'location', 'latitude', 'longitude', 'firmware_version', 'software_version', 'status', 'last_seen_at', 'notes', 'is_active', 'created_by', 'updated_by', 'activation_status', 'activated_at', 'activation_message', 'remove_legacy_users_on_activation', 'device_facts', 'base_sync_status', 'base_sync_error_code', 'base_sync_message', 'base_synced_at', 'base_sync_attempted_at'];
|
||||
|
||||
protected $hidden = ['credentials'];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(fn (Device $device) => $device->uuid ??= (string) Str::uuid());
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['is_active' => 'boolean', 'remove_legacy_users_on_activation' => 'boolean', 'connection_ports' => 'array', 'device_facts' => 'array', 'last_seen_at' => 'datetime', 'activated_at' => 'datetime', 'base_synced_at' => 'datetime', 'base_sync_attempted_at' => 'datetime', 'latitude' => 'decimal:7', 'longitude' => 'decimal:7'];
|
||||
}
|
||||
|
||||
public function portFor(string $protocol): ?int
|
||||
{
|
||||
$port = $this->connection_ports[$protocol] ?? ($this->connection_type === $protocol ? $this->management_port : null);
|
||||
|
||||
return $port ? (int) $port : null;
|
||||
}
|
||||
|
||||
public function hasConnection(string $protocol): bool
|
||||
{
|
||||
return $this->portFor($protocol) !== null;
|
||||
}
|
||||
|
||||
public function vendor()
|
||||
{
|
||||
return $this->belongsTo(DeviceVendor::class, 'device_vendor_id');
|
||||
}
|
||||
|
||||
public function type()
|
||||
{
|
||||
return $this->belongsTo(DeviceType::class, 'device_type_id');
|
||||
}
|
||||
|
||||
public function model()
|
||||
{
|
||||
return $this->belongsTo(DeviceModel::class, 'device_model_id');
|
||||
}
|
||||
|
||||
public function credentials()
|
||||
{
|
||||
return $this->hasMany(DeviceCredential::class);
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function userAssignments()
|
||||
{
|
||||
return $this->hasMany(DeviceUserAssignment::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user