Various improvements

This commit is contained in:
2017-02-18 13:12:36 +01:00
parent ccc70650f2
commit a9c1a3ccbe
7 changed files with 185 additions and 22 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace NoccyLabs\UPnP\DCP;
use NoccyLabs\UPnP\DCP\AbstractDevice;
use NoccyLabs\UPnP\SSDP\Device;
abstract class AbstractDevice
{
/** @var Device The device this DCP operates on */
protected $device;
/**
*
*
* @param Device $device The device for the DCP
*/
public function __construct(Device $device)
{
$this->device = $device;
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace NoccyLabs\UPnP\DCP;
use NoccyLabs\UPnP\DCP\AbstractService;
use NoccyLabs\UPnP\SSDP\Device;
use NoccyLabs\UPnP\SSDP\Service;
abstract class AbstractService
{
/** @var Service The service this DCP operates on */
protected $service;
/** @var Device The device this DCP operates on */
protected $device;
/**
*
*
* @param Device $device The device for the DCP
* @param Service $service The service for the DCP
*/
public function __construct(Device $device, Service $service)
{
$this->device = $device;
$this->service = $service;
}
private function readScpdServiceDefinition($url)
{}
/**
* Get the schema to pass as the XML namespace when sending the SOAP request
* to the device.
*
* @return string The schema
*/
abstract protected function getSchemaUrn();
}
+14 -2
View File
@@ -17,8 +17,9 @@ namespace NoccyLabs\UPnP\SSDP;
*/
use SimpleXMLElement;
use JsonSerializable;
class Device
class Device implements JsonSerializable
{
protected $deviceType;
@@ -86,7 +87,7 @@ class Device
$services = $spec->serviceList;
if (count($services)>0) {
foreach ($services->children() as $service) {
$this->services[] = new Service($service);
$this->services[] = new Service($this, $service);
}
}
@@ -163,5 +164,16 @@ class Device
return $info;
}
public function createServiceWrapper($type)
{
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}
+12 -2
View File
@@ -4,6 +4,7 @@ namespace NoccyLabs\UPnP\SSDP;
use IteratorAggregate;
use ArrayIterator;
use JsonSerializable;
use NoccyLabs\UPnP\HTTPU\Endpoint;
use NoccyLabs\UPnP\HTTPU\EndpointException;
use NoccyLabs\UPnP\HTTPU\MSearchRequest;
@@ -14,7 +15,7 @@ use NoccyLabs\UPnP\HTTPU\MSearchResponse;
*
*
*/
class Discovery implements IteratorAggregate
class Discovery implements IteratorAggregate, JsonSerializable
{
protected $devices = [];
@@ -52,10 +53,14 @@ class Discovery implements IteratorAggregate
continue;
}
$device = Device::createFromSchema($response->getLocation(), $response->getIp());
$this->devices[$loc] = $device;
if ($device) {
$this->devices[$loc] = $device;
}
}
}
$this->devices = array_values($this->devices);
return count($this->devices);
}
@@ -65,5 +70,10 @@ class Discovery implements IteratorAggregate
return new ArrayIterator($this->devices);
}
public function jsonSerialize()
{
return $this->devices;
}
}
+31 -2
View File
@@ -10,10 +10,13 @@ namespace NoccyLabs\UPnP\SSDP;
<eventSubURL>/evt/L3F</eventSubURL>
*/
use JsonSerializable;
use SimpleXMLElement;
class Service
class Service implements JsonSerializable
{
protected $device;
protected $serviceType;
protected $serviceId;
@@ -24,9 +27,10 @@ class Service
protected $eventSubUrl;
public function __construct(SimpleXMLElement $spec)
public function __construct(Device $device, SimpleXMLElement $spec)
{
$this->device = $device;
$this->serviceType = (string)$spec->serviceType;
$this->serviceId = (string)$spec->serviceId;
$this->scpdUrl = (string)$spec->SCPDURL;
@@ -50,6 +54,26 @@ class Service
return $this->scpdUrl;
}
public function getServiceUrl()
{
if (strpos($this->scpdUrl,"://")!==false) {
return $this->scpdUrl;
}
$dev = ['user'=>null, 'pass'=>null, 'port'=>80, 'path'=>null];
$dev = array_merge($dev, parse_url($this->device->getUrl()));
$url = $this->scpdUrl;
$auth = $dev['user']?($dev['user'].":".$dev['path']):"";
$base = $dev['scheme']."://".$dev['host'].":".($dev['port']?:80);
if ($url[0]=="/") {
return $base.$url;
} else {
return $base.rtrim($dev['path'],"/")."/".$url;
}
}
public function getControlUrl()
{
return $this->controlUrl;
@@ -71,5 +95,10 @@ class Service
);
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}