-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- changed several references from UniFi SDN controller to UniFi Network controller - added optional payload parameter to the list_alarms() method/function, contributed by @MikeSiekkinen through PR #68 - added example showing how to disable/enable a UniFi switch port - updated restart_device() function/method, thanks go to @leonardogyn for reporting this - added example to modify outlet settings on a UniFi SmartPower PDU Pro, thanks go to @panthergm for providing access
- Loading branch information
1 parent
69b43df
commit d26cba1
Showing
4 changed files
with
210 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* PHP API usage example | ||
* | ||
* contributed by: Art of WiFi | ||
* description: example PHP script to disable/enable the port of a UniFi switch | ||
* note: Requires controller version 5.5.X or higher. This example assumes an override alreay exists for the desired port. | ||
* To create a new port override simply append one (similar in structure to $updated_override) as needed to the | ||
* $existing_overrides array | ||
*/ | ||
|
||
/** | ||
* using the composer autoloader | ||
*/ | ||
require_once('vendor/autoload.php'); | ||
|
||
/** | ||
* include the config file (place your credentials etc. there if not already present) | ||
* see the config.template.php file for an example | ||
*/ | ||
require_once('config.php'); | ||
|
||
/** | ||
* the site to use to log in to the controller | ||
*/ | ||
$site_id = '<enter your site id here>'; | ||
|
||
/** | ||
* the MAC address of the UniFi switch to re-configure | ||
*/ | ||
$device_mac = '<enter MAC address>'; | ||
|
||
/** | ||
* index of port to modify/add | ||
*/ | ||
$port_idx = 24; | ||
|
||
/** | ||
* port configuration id to apply when enabling/disabling the port | ||
* | ||
* NOTE: | ||
* port configurations are available through list_portconf() | ||
*/ | ||
$port_conf_id = '<enter _id value of desired port configuration>'; | ||
|
||
/** | ||
* initialize the UniFi API connection class and log in to the controller and do our thing | ||
*/ | ||
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, false); | ||
$set_debug_mode = $unifi_connection->set_debug($debug); | ||
$loginresults = $unifi_connection->login(); | ||
$data = $unifi_connection->list_devices($device_mac); | ||
$device_id = $data[0]->device_id; | ||
$existing_overrides = $data[0]->port_overrides; | ||
|
||
foreach ($existing_overrides as $key => $value) { | ||
if (!empty($value->port_idx) && $value->port_idx === $port_idx) { | ||
$updated_override = [ | ||
'portconf_id' => $port_conf_id, | ||
'port_idx' => $port_idx, | ||
'poe_mode' => $value->poe_mode, | ||
'name' => 'Your-port-name', | ||
]; | ||
|
||
$existing_overrides[$key] = $updated_override; | ||
} | ||
} | ||
|
||
$payload = [ | ||
'port_overrides' => $existing_overrides | ||
]; | ||
|
||
$update_device = $unifi_connection->set_device_settings_base($device_id, $payload); | ||
|
||
/** | ||
* provide feedback in json format | ||
*/ | ||
echo json_encode($update_device, JSON_PRETTY_PRINT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* PHP API usage example | ||
* | ||
* contributed by: Art of WiFi | ||
* description: example basic PHP script to toggle power of an outlet on the UniFi SmartPower PDU Pro, | ||
* last tested with UniFi controller version 6.1.19 | ||
*/ | ||
require 'vendor/autoload.php'; | ||
|
||
/** | ||
* include the config file (place your credentials etc. there if not already present) | ||
* see the config.template.php file for an example | ||
*/ | ||
require_once('config.php'); | ||
|
||
/** | ||
* the site to use | ||
*/ | ||
$site_id = 'default'; | ||
|
||
/** | ||
* MAC of UniFi SmartPower PDU Pro to work with | ||
*/ | ||
$pdu_mac = '<MAC ADDRESS of PDU>'; | ||
|
||
/** | ||
* index value of the outlet to modify | ||
*/ | ||
$outlet_idx = 20; | ||
|
||
/** | ||
* new values for relay_state (enable/disable Power) and cycle_enabled (disable/enable Modem Power Cycle) for the above outlet, | ||
* values must be boolean (true/false) | ||
* | ||
* NOTES: | ||
* - here you can choose to also change the name of the outlet | ||
* - outlet overrides are structured like this: | ||
* { | ||
* "index": 1, | ||
* "name": "USB Outlet 1", | ||
* "cycle_enabled": false, | ||
* "relay_state": true | ||
* } | ||
*/ | ||
$new_relay_state = true; | ||
$new_cycle_enabled = false; | ||
|
||
/** | ||
* initialize the UniFi API connection class and log in to the controller and do our thing | ||
*/ | ||
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); | ||
$set_debug_mode = $unifi_connection->set_debug($debug); | ||
$loginresults = $unifi_connection->login(); | ||
if ($loginresults) { | ||
$pdu_details = $unifi_connection->list_devices($pdu_mac); | ||
|
||
if (!empty($pdu_details) && property_exists($pdu_details[0], 'model') && $pdu_details[0]->model === 'USPPDUP' && property_exists($pdu_details[0], 'outlet_overrides')) { | ||
$device_id = $pdu_details[0]->_id; | ||
$outlet_overrides = $pdu_details[0]->outlet_overrides; | ||
|
||
foreach ($outlet_overrides as $key => $value) { | ||
if ($value->index === $outlet_idx) { | ||
$outlet_overrides[$key]->relay_state = $new_relay_state; | ||
$outlet_overrides[$key]->cycle_enabled = $new_cycle_enabled; | ||
} | ||
} | ||
|
||
$pdu_update = $unifi_connection->set_device_settings_base($device_id, ['outlet_overrides' => $outlet_overrides]); | ||
|
||
/** | ||
* provide feedback in json format | ||
*/ | ||
echo 'results:' . PHP_EOL . PHP_EOL; | ||
echo json_encode($pdu_update, JSON_PRETTY_PRINT); | ||
echo PHP_EOL; | ||
} else { | ||
echo 'not a PDU device?'; | ||
echo PHP_EOL; | ||
} | ||
} else { | ||
echo 'we encountered a login error!'; | ||
echo PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters