. * * @link https://www.librenms.org * * @copyright 2024 Tony Murray * @author Tony Murray */ namespace App\Discovery; use App\Models\Device; use Illuminate\Support\Collection; use LibreNMS\DB\SyncsModels; class Sensor { use SyncsModels; private Collection $models; /** @var bool[] */ private array $discovered = []; private string $relationship = 'sensors'; private Device $device; public function __construct(Device $device) { $this->device = $device; $this->models = new Collection; } public function discover(\App\Models\Sensor $sensor): static { $sensor->device_id ??= \DeviceCache::getPrimary()->device_id; $this->models->push($sensor); $this->discovered[$sensor->syncGroup()] = false; return $this; } public function isDiscovered(string $type): bool { return $this->discovered[$type] ?? false; } public function sync(...$params): Collection { $type = implode('-', $params); if (! $this->isDiscovered($type)) { $synced = $this->syncModelsByGroup($this->device, 'sensors', $this->getModels(), $params); $this->discovered[$type] = true; return $synced; } return new Collection; } public function getModels(): Collection { return $this->models; } }