php-upnp/src/SSDP/Device.php

124 lines
3.3 KiB
PHP

<?php
namespace NoccyLabs\UPnP\SSDP;
/*
<deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>
<friendlyName>WANDevice</friendlyName>
<manufacturer>MiniUPnP</manufacturer>
<manufacturerURL>http://miniupnp.free.fr/</manufacturerURL>
<modelDescription>WAN Device</modelDescription>
<modelName>WAN Device</modelName>
<modelNumber>20161205</modelNumber>
<modelURL>http://miniupnp.free.fr/</modelURL>
<serialNumber>00000000</serialNumber>
<UDN>uuid:ea80ec5e-2ff9-4834-866a-257fccd9e572</UDN>
<UPC>000000000000</UPC>
*/
use SimpleXMLElement;
class Device
{
protected $deviceType;
protected $friendlyName;
protected $manufacturer;
protected $manufacturerUrl;
protected $modelName;
protected $modelDescription;
protected $modelNumber;
protected $modelUrl;
protected $serialNumber;
protected $specUrl;
protected $services = [];
protected $devices = [];
public static function createFromSchema($url, $ip)
{
$xml = simplexml_load_file($url);
$spec = $xml->children('urn:schemas-upnp-org:device-1-0');
if (count($spec)>0) {
$device = $spec->device;
return new Device($device, $url, $ip);
}
}
public function __construct(SimpleXMLElement $spec, $spec_url, $ip)
{
$this->ip = $ip;
$this->specUrl = (string)$spec_url;
$this->deviceType = (string)$spec->deviceType;
$this->friendlyName = (string)$spec->friendlyName;
$this->manufacturer = (string)$spec->manufacturer;
$this->manufacturerUrl = (string)$spec->manufacturerURL;
$this->modelName = (string)$spec->modelName;
$this->modelDescription = (string)$spec->modelDescription;
$this->modelNumber = (string)$spec->modelNumber;
$this->modelUrl = (string)$spec->modelURL;
$this->serialNumber = (string)$spec->serialNumber;
$devices = $spec->deviceList;
if (count($devices)>0) {
foreach ($devices->children() as $device) {
$this->devices[] = new Device($device, $spec_url, $ip);
}
}
$services = $spec->serviceList;
if (count($services)>0) {
foreach ($services->children() as $service) {
$this->services[] = new Service($service);
}
}
}
public function getIp()
{
return $this->ip;
}
public function __toString()
{
$info = sprintf("Device: %s [%s] at %s\nManufacturer: %s\nModel: %s (%s)\nURL: %s\n",
$this->friendlyName,
$this->deviceType,
$this->ip,
$this->manufacturer,
$this->modelName,
$this->modelDescription,
$this->specUrl
);
if (count($this->services)>0) {
$services = " = ".join("\n = ",array_map("strval", $this->services));
$services = join("\n ", explode("\n", rtrim($services)));
$info.= $services."\n";
}
if (count($this->devices)>0) {
$devices = " + ".join("\n + ",array_map("strval", $this->devices));
$devices = join("\n ", explode("\n", rtrim($devices)));
$info.= $devices."\n";
}
return $info;
}
}