-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-device-management.php
More file actions
executable file
·134 lines (113 loc) · 5.73 KB
/
02-device-management.php
File metadata and controls
executable file
·134 lines (113 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Example 2: Device Management
*
* This example demonstrates how to work with UniFi devices including
* listing devices, getting device details, statistics, and executing device actions.
*/
require_once __DIR__.'/../vendor/autoload.php';
use ArtOfWiFi\UnifiNetworkApplicationApi\UnifiClient;
$config = require_once __DIR__.'/config.php';
// Configuration - Update the values in the config.php file
$controllerUrl = $config['base_url'];
$apiKey = $config['api_key'];
$siteId = $config['site_id'];
$verifySsl = $config['verify_ssl'];
try {
// Initialize the API client
$apiClient = new UnifiClient($controllerUrl, $apiKey, $verifySsl);
$apiClient->setSiteId($siteId);
echo "UniFi API Client - Device Management Example\n";
echo str_repeat('=', 50)."\n\n"; // 1. List all adopted devices
echo "1. Listing all adopted devices...\n";
$devicesResponse = $apiClient->devices()->listAdopted();
$devices = $devicesResponse->json();
if (isset($devices['data']) && count($devices['data']) > 0) {
foreach ($devices['data'] as $device) {
$name = $device['name'] ?? 'Unnamed';
$model = $device['model'] ?? 'Unknown';
$mac = $device['macAddress'] ?? 'Unknown';
$ip = $device['ipAddress'] ?? 'No IP';
$state = $device['state'] ?? 'Unknown';
echo " Device: {$name}\n";
echo " - Model: {$model}\n";
echo " - MAC: {$mac}\n";
echo " - IP: {$ip}\n";
echo " - State: {$state}\n";
echo "\n";
}
// Use the first device for detailed examples
$firstDevice = $devices['data'][0];
$deviceId = $firstDevice['id'];
$deviceName = $firstDevice['name'] ?? 'Unnamed Device';
// 2. Get detailed device information
echo "2. Getting detailed information for: {$deviceName}\n";
$detailResponse = $apiClient->devices()->get($deviceId);
$details = $detailResponse->json();
echo ' IP Address: '.($details['ipAddress'] ?? 'No IP')."\n";
echo ' Firmware: '.($details['firmwareVersion'] ?? 'Unknown')."\n";
echo ' Adopted at: '.($details['adoptedAt'] ?? 'Unknown')."\n";
echo "\n";
// 3. Get device statistics
echo "3. Getting device statistics...\n";
$statsResponse = $apiClient->devices()->getStatistics($deviceId);
$stats = $statsResponse->json();
echo ' CPU Usage: '.($stats['cpuUtilizationPct'] ?? 'N/A')."%\n";
echo ' Memory Usage: '.($stats['memoryUtilizationPct'] ?? 'N/A')."%\n";
echo ' Uptime: '.($stats['uptimeSec'] ?? 'N/A')."s\n";
echo "\n";
} else {
echo " No adopted devices found.\n\n";
}// 4. List pending devices (devices waiting to be adopted)
echo "4. Listing pending devices...\n";
$pendingResponse = $apiClient->devices()->listPending();
$pending = $pendingResponse->json();
$pendingCount = isset($pending['data']) ? count($pending['data']) : 0;
echo " Pending devices: {$pendingCount}\n";
if ($pendingCount > 0) {
foreach ($pending['data'] as $device) {
$mac = $device['macAddress'] ?? 'Unknown';
$model = $device['model'] ?? 'Unknown';
echo " - {$model} (MAC: {$mac})\n";
}
}
echo "\n"; // 5. Filtering examples
echo "5. Filtering Examples (based on official API filterable properties):\n";
echo " Available filterable properties:\n";
echo " - id (UUID), macAddress (STRING), ipAddress (STRING), name (STRING)\n";
echo " - model (STRING), state (STRING), supported (BOOLEAN)\n";
echo " - firmwareVersion (STRING), firmwareUpdatable (BOOLEAN)\n";
echo " - features (SET), interfaces (SET)\n\n";
echo " Filter devices by name pattern:\n";
echo " \$apiClient->devices()->listAdopted(filter: \"name.like('AP-%')\");\n\n";
echo " Filter by device model:\n";
echo " \$apiClient->devices()->listAdopted(filter: 'model.eq(\"U6-Pro\")');\n\n";
echo " Filter by state:\n";
echo " \$apiClient->devices()->listAdopted(filter: 'state.eq(\"ONLINE\")');\n\n";
echo " Filter devices needing firmware update:\n";
echo " \$apiClient->devices()->listAdopted(filter: 'firmwareUpdatable.eq(true)');\n\n"; // 6. Example of executing device actions (commented out for safety)
echo "6. Device Actions (examples - commented out for safety)\n";
echo " According to the official API specification, the only documented action is:\n";
echo " - Restart device:\n";
echo " \$apiClient->devices()->executeAction(\$deviceId, ['action' => 'RESTART']);\n";
echo "\n"; // Uncomment to actually execute an action:
// $apiClient->devices()->executeAction($deviceId, ['action' => 'RESTART']);
// 7. Adopting a pending device (commented out for safety)
echo "7. Adopting a Device (example - commented out for safety)\n";
echo " Adopt a pending device by MAC address:\n";
echo " \$apiClient->devices()->adopt('00:11:22:33:44:55');\n\n";
echo " Adopt and ignore the device limit:\n";
echo " \$apiClient->devices()->adopt('00:11:22:33:44:55', ignoreDeviceLimit: true);\n\n";
// Uncomment to actually adopt a device:
// $apiClient->devices()->adopt('00:11:22:33:44:55');
// 8. Removing (unadopting) a device (commented out for safety)
echo "8. Removing a Device (example - commented out for safety)\n";
echo " Remove (unadopt) an adopted device:\n";
echo " \$apiClient->devices()->remove(\$deviceId);\n\n";
// Uncomment to actually remove a device:
// $apiClient->devices()->remove($deviceId);
echo str_repeat('=', 50)."\n";
echo "Example completed successfully!\n";
} catch (Exception $e) {
echo 'ERROR: '.$e->getMessage()."\n";
}