From 24ee8db67687fceb4e64c21b4bcdbcd03499b1e1 Mon Sep 17 00:00:00 2001 From: Craig Duncan Date: Wed, 27 Nov 2024 18:16:29 +0000 Subject: [PATCH] Move the speaker check to a collection wrapper This will enable users to add new models if theirs isn't supported yet It will probably result in less PR/Issues for new models to be added, but is unfortunate, but it should also result in less forks and frustration --- src/Devices/Device.php | 44 ------ src/Devices/Speakers.php | 148 +++++++++++++++++++++ src/Interfaces/Devices/DeviceInterface.php | 8 -- src/Network.php | 10 +- src/Speaker.php | 5 - 5 files changed, 150 insertions(+), 65 deletions(-) create mode 100644 src/Devices/Speakers.php diff --git a/src/Devices/Device.php b/src/Devices/Device.php index 6229e26..f51c02e 100644 --- a/src/Devices/Device.php +++ b/src/Devices/Device.php @@ -184,48 +184,4 @@ public function getModel(): string return $this->model; } - - - /** - * Check if this sonos device is a speaker. - * - * @return bool - */ - public function isSpeaker(): bool - { - $model = $this->getModel(); - - $models = [ - "S1" => "PLAY:1", - "S12" => "PLAY:1", - "S3" => "PLAY:3", - "S5" => "PLAY:5", - "S6" => "PLAY:5", - "S24" => "PLAY:5", - "S9" => "PLAYBAR", - "S11" => "PLAYBASE", - "S13" => "ONE", - "S18" => "ONE", - "S14" => "BEAM", - "S31" => "BEAM", - "S15" => "CONNECT", - "S17" => "Move", - "S19" => "ARC", - "S20" => "SYMFONISK Table Lamp", - "S21" => "SYMFONISK Bookshelf", - "S29" => "SYMFONISK Picture Frame", - "S22" => "ONE SL", - "S38" => "ONE SL", - "S23" => "PORT", - "S27" => "ROAM", - "S35" => "ROAM SL", - "ZP80" => "ZONEPLAYER", - "ZP90" => "CONNECT", - "S16" => "CONNECT:AMP", - "ZP100" => "CONNECT:AMP", - "ZP120" => "CONNECT:AMP", - ]; - - return array_key_exists($model, $models); - } } diff --git a/src/Devices/Speakers.php b/src/Devices/Speakers.php new file mode 100644 index 0000000..9df7341 --- /dev/null +++ b/src/Devices/Speakers.php @@ -0,0 +1,148 @@ + "PLAY:1", + "S12" => "PLAY:1", + "S3" => "PLAY:3", + "S5" => "PLAY:5", + "S6" => "PLAY:5", + "S24" => "PLAY:5", + "S9" => "PLAYBAR", + "S11" => "PLAYBASE", + "S13" => "ONE", + "S18" => "ONE", + "S14" => "BEAM", + "S31" => "BEAM", + "S15" => "CONNECT", + "S17" => "Move", + "S19" => "ARC", + "S20" => "SYMFONISK Table Lamp", + "S21" => "SYMFONISK Bookshelf", + "S29" => "SYMFONISK Picture Frame", + "S22" => "ONE SL", + "S38" => "ONE SL", + "S23" => "PORT", + "S27" => "ROAM", + "S35" => "ROAM SL", + "ZP80" => "ZONEPLAYER", + "ZP90" => "CONNECT", + "S16" => "CONNECT:AMP", + "ZP100" => "CONNECT:AMP", + "ZP120" => "CONNECT:AMP", + ]; + + /** + * @var CollectionInterface $collection The device collection to actually use. + */ + private $collection; + + + /** + * @param CollectionInterface $collection The device collection to actually use + */ + public function __construct(CollectionInterface $collection) + { + $this->collection = $collection; + } + + + /** + * Set the logger object to use. + * + * @return $this + * @var LoggerInterface $logger The logging object + * + */ + public function setLogger(LoggerInterface $logger) + { + $this->collection->setLogger($logger); + + return $this; + } + + + /** + * Get the logger object to use. + * + * @return LoggerInterface $logger The logging object + */ + public function getLogger(): LoggerInterface + { + return $this->collection->getLogger(); + } + + + private function isSpeaker(DeviceInterface $device): bool + { + return array_key_exists($device->getModel(), self::MODELS); + } + + + /** + * Add a device to this collection. + * + * @param DeviceInterface $device The device to add + * + * @return $this + */ + public function addDevice(DeviceInterface $device): CollectionInterface + { + if (!$this->isSpeaker($device)) { + $error = "This device is not recognised as a speaker model: " . $device->getModel(); + throw new \InvalidArgumentException($error); + } + + $this->collection->addDevice($device); + return $this; + } + + + /** + * Add a device to this collection using its IP address + * + * @param string $address The IP address of the device to add + * + * @return $this + */ + public function addIp(string $address): CollectionInterface + { + $this->collection->addIp($address); + return $this; + } + + + /** + * Get all of the devices on the current network + * + * @return DeviceInterface[] + */ + public function getDevices(): array + { + return $this->collection->getDevices(); + } + + + /** + * Remove all devices from this collection. + * + * @return $this + */ + public function clear(): CollectionInterface + { + $this->collection->clear(); + return $this; + } +} diff --git a/src/Interfaces/Devices/DeviceInterface.php b/src/Interfaces/Devices/DeviceInterface.php index d98eb1d..3801de6 100644 --- a/src/Interfaces/Devices/DeviceInterface.php +++ b/src/Interfaces/Devices/DeviceInterface.php @@ -42,12 +42,4 @@ public function soap(string $service, string $action, array $params = []); * @return string */ public function getModel(): string; - - - /** - * Check if this sonos device is a speaker. - * - * @return bool - */ - public function isSpeaker(): bool; } diff --git a/src/Network.php b/src/Network.php index fd07b27..e97e634 100644 --- a/src/Network.php +++ b/src/Network.php @@ -3,9 +3,8 @@ namespace duncan3dc\Sonos; use duncan3dc\DomParser\XmlParser; -use duncan3dc\Sonos\Devices\Collection; use duncan3dc\Sonos\Devices\Discovery; -use duncan3dc\Sonos\Devices\Factory; +use duncan3dc\Sonos\Devices\Speakers; use duncan3dc\Sonos\Exceptions\NotFoundException; use duncan3dc\Sonos\Exceptions\UnknownGroupException; use duncan3dc\Sonos\Interfaces\AlarmInterface; @@ -16,7 +15,6 @@ use duncan3dc\Sonos\Interfaces\Services\RadioInterface; use duncan3dc\Sonos\Interfaces\SpeakerInterface; use duncan3dc\Sonos\Services\Radio; -use GuzzleHttp\Client; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; @@ -54,7 +52,7 @@ final class Network implements NetworkInterface, LoggerAwareInterface public function __construct(CollectionInterface $collection = null) { if ($collection === null) { - $collection = new Discovery(); + $collection = new Speakers(new Discovery()); } $this->collection = $collection; } @@ -106,10 +104,6 @@ public function getSpeakers(): array $this->speakers = []; foreach ($devices as $device) { - if (!$device->isSpeaker()) { - continue; - } - $speaker = new Speaker($device); try { diff --git a/src/Speaker.php b/src/Speaker.php index 97caab5..ab2c7d0 100644 --- a/src/Speaker.php +++ b/src/Speaker.php @@ -77,11 +77,6 @@ public function __construct($param) if (preg_match("/^uuid:(.*)$/", $udn, $matches)) { $this->uuid = $matches[1]; } - - if (!$this->device->isSpeaker()) { - $error = "You cannot create a Speaker instance for this model: " . $this->device->getModel(); - throw new \InvalidArgumentException($error); - } }