addition of several new functions/methods and minor code cleanup

-  added list_known_rogueaps() function/method
-  added stat_status() function/method
-  added power_cycle_switch_port() function/method
This commit is contained in:
malle-pietje 2017-10-18 08:25:48 +02:00
parent 746a29fbcf
commit e6ffb2e5de
2 changed files with 95 additions and 47 deletions

View File

@ -1,6 +1,6 @@
## UniFi controller API client class
A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
This class can now also be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
@ -49,6 +49,7 @@ The class currently supports the following functions/methods to get/post/put/del
- list_guests()
- list_health()
- list_hotspotop()
- list_known_rogueaps()
- list_networkconf()
- list_portconf()
- list_portforward_stats()
@ -65,11 +66,13 @@ The class currently supports the following functions/methods to get/post/put/del
- list_wlan_groups()
- list_wlanconf()
- locate_ap()
- power_cycle_switch_port()
- reconnect_sta()
- rename_ap()
- restart_ap()
- revoke_voucher()
- set_ap_radiosettings()
- set_device_settings_base()
- set_guestlogin_settings()
- set_locate_ap() (deprecated but still available as alias)
- set_networksettings_base()
@ -98,6 +101,7 @@ The class currently supports the following functions/methods to get/post/put/del
- stat_sessions()
- stat_sites()
- stat_sta_sessions_latest()
- stat_status()
- stat_sysinfo()
- stat_voucher()
- unauthorize_guest()

View File

@ -782,9 +782,9 @@ class Client
}
/**
* List rogue access points
* ------------------------
* returns an array of known rogue access point objects
* List rogue/neighboring access points
* ------------------------------------
* returns an array of rogue/neighboring access point objects
* optional parameter <within> = hours to go back to list discovered "rogue" access points (default = 24 hours)
*/
public function list_rogueaps($within = 24)
@ -795,6 +795,18 @@ class Client
return $this->process_response($response);
}
/**
* List known rogue access points
* ------------------------------
* returns an array of known rogue access point objects
*/
public function list_known_rogueaps()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/rogueknown');
return $this->process_response($response);
}
/**
* List sites
* ----------
@ -888,6 +900,17 @@ class Client
return $this->process_response($response);
}
/**
* List controller status
* ----------------------
* returns an array containing general controller status info
*/
public function stat_status()
{
$response = $this->exec_curl('/status');
return $this->process_response($response);
}
/**
* List self
* ---------
@ -1177,16 +1200,12 @@ class Client
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$override_mode_options = ['off', 'on', 'default'];
if (in_array($override_mode, $override_mode_options)) {
if (!in_array($override_mode, ['off', 'on', 'default'])) return false;
$json = json_encode(['led_override' => $override_mode]);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json);
return $this->process_response_boolean($response);
}
return false;
}
/**
* Toggle flashing LED of an access point for locating purposes
* ------------------------------------------------------------
@ -1253,16 +1272,12 @@ class Client
*/
public function set_ap_wlangroup($wlantype_id, $device_id, $wlangroup_id) {
if (!$this->is_loggedin) return false;
$wlantype_id_options = ['ng', 'na'];
if (in_array($wlantype_id, $wlantype_id_options)) {
if (in_array($wlantype_id, ['ng', 'na'])) return false;
$json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json);
return $this->process_response_boolean($response);
}
return false;
}
/**
* Update guest login settings
* ---------------------------
@ -1532,8 +1547,7 @@ class Client
*/
public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs)
{
$mac_filter_policy_options = ['allow', 'deny'];
if (in_array($mac_filter_policy, $mac_filter_policy_options)) {
if (in_array($mac_filter_policy, ['allow', 'deny'])) return false;
$payload = new \stdClass();
$payload->mac_filter_enabled = (bool)$mac_filter_enabled;
$payload->mac_filter_policy = $mac_filter_policy;
@ -1541,9 +1555,6 @@ class Client
return $this->set_wlansettings_base($wlan_id, $payload);
}
return false;
}
/**
* List events
* -----------
@ -1625,6 +1636,25 @@ class Client
return $this->process_response_boolean($response);
}
/**
* Power-cycle the PoE output of a switch port
* -------------------------------------------
* return true on success
* required parameter <switch_mac> = string; main MAC address of the switch
* required parameter <port_idx> = integer; port number/index of the port to be affected
*
* NOTES:
* - only applies to switches and their PoE ports...
*/
public function power_cycle_switch_port($switch_mac, $port_idx)
{
if (!$this->is_loggedin) return false;
$json = ['mac' => $switch_mac, 'port_idx' => intval($port_idx), 'cmd' => 'power-cycle'];
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/devmgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Trigger an RF scan by an AP
* ---------------------------
@ -1653,6 +1683,23 @@ class Client
return $this->process_response($response);
}
/**
* Update device settings, base (using REST)
* -----------------------------------------
* return true on success
* required parameter <device_id> = 24 char string; _id of the device which can be found with the list_devices() function
* required parameter <device_settings> = stdClass object or associative array containing the configuration to apply to the device, must be a
* (partial) object/array structured in the same manner as is returned by list_devices() for the device.
*/
public function set_device_settings_base($device_id, $device_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($device_settings);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* List Radius profiles (using REST)
* --------------------------------------
@ -1727,9 +1774,9 @@ class Client
public function create_radius_account($name, $x_password, $tunnel_type, $tunnel_medium_type, $vlan = null)
{
if (!$this->is_loggedin) return false;
$tunnel_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$tunnel_medium_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
if (in_array($tunnel_type, $tunnel_type_options) && in_array($tunnel_medium_type, $tunnel_medium_type_options)) {
$tunnel_types = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$tunnel_medium_types = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
if (!in_array($tunnel_type, $tunnel_types) || !in_array($tunnel_medium_type, $tunnel_medium_types)) return false;
$this->request_type = 'POST';
$account_details = [
'name' => $name,
@ -1743,9 +1790,6 @@ class Client
return $this->process_response($response);
}
return false;
}
/**
* Update Radius account, base (using REST)
* ----------------------------------------