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

View File

@ -10,10 +10,11 @@
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\UPnP\SSDP\Device;
use NoccyLabs\UPnP\SSDP\Discovery;
use NoccyLabs\UPnP\SSDP\SearchTarget;
$opt_short = "hARd:s:u:D:l";
$opt_short = "hARd:s:u:D:lj";
$opt_long = [
"help", // -h
"all", // -A
@ -22,7 +23,8 @@ $opt_long = [
"service:", // -s:
"uuid:", // -u:
"domain:", // -D:
"long",
"long", // -l
"json", // -j
];
$help = <<<EOH
upnp-discover - Discover UPnP devices on the local network
@ -39,6 +41,7 @@ Options:
-D, --domain Combine with -d or -s to search for specific vendor domains.
-u, --uuid Find by UUID
-l, --long Include all info in output
-j, --json Output JSON
Examples:
@ -69,7 +72,8 @@ $opts = (object)[
'service'=>null,
'domain'=>null,
'uuid'=>null,
'long'=>null
'long'=>null,
'json'=>null
];
foreach (getopt($opt_short, $opt_long) as $opt=>$value) switch ($opt) {
@ -97,6 +101,9 @@ foreach (getopt($opt_short, $opt_long) as $opt=>$value) switch ($opt) {
case 'long':
case 'l':
$opts->long = true; break;
case 'json':
case 'j':
$opts->json = true; break;
}
if ($opts->device && $opts->service) {
@ -190,17 +197,54 @@ function discover_root() {
}
function show_results(Discovery $discovery) {
foreach ($discovery as $device) {
printf(" %s: %s (%s) %s [%s]\n",
$device->getFriendlyName(),
$device->getModelName(),
$device->getManufacturer(),
$device->getDeviceType(),
$device->getIp()
);
foreach ($device->getServices() as $service) {
printf(" + %s\n", $service->getServiceType());
}
global $opts;
if ($opts->long) {
show_results_long($discovery);
} elseif ($opts->json) {
show_results_json($discovery);
} else {
show_results_short($discovery);
}
}
}
function show_results_short(Discovery $discovery) {
foreach ($discovery as $device) {
printf(" * \e[94m%s: %s \e[1m%s\e[21m (%s) [%s]\e[0m\n",
$device->getFriendlyName(),
$device->getManufacturer(),
$device->getModelName(),
$device->getDeviceType(),
$device->getIp()
);
}
}
function show_results_long(Discovery $discovery) {
foreach ($discovery as $device) {
show_device_long($device);
}
}
function show_device_long(Device $device, $level=0) {
$indent = str_repeat(" ",$level);
printf("{$indent} * \e[92m%s\e[94m: %s \e[1m%s\e[21m (\e[34m%s\e[94m) [%s]\e[0m\n",
$device->getFriendlyName(),
$device->getManufacturer(),
$device->getModelName(),
$device->getDeviceType(),
$device->getIp()
);
printf("{$indent} \e[32m%s\e[0m\n", $device->getUrl());
foreach ($device->getServices() as $service) {
printf("{$indent} + \e[36m%s\e[0m\n{$indent} \e[32m%s\e[0m\n", $service->getServiceType(), $service->getServiceUrl());
}
foreach ($device->getDevices() as $subdevice) {
show_device_long($subdevice, $level+1);
}
}
function show_results_json(Discovery $discovery) {
echo json_encode($discovery, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)."\n";
}