Skip to content

Commit ee0bfe4

Browse files
authored
UniFi OS updates (#267)
* Add list active devices endpoint * Add list clients history endpoint * Add OS console update endpoints
1 parent 73ae5a4 commit ee0bfe4

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

src/Client.php

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use UniFi_API\Exceptions\LoginFailedException;
1515
use UniFi_API\Exceptions\LoginRequiredException;
1616
use UniFi_API\Exceptions\MethodDeprecatedException;
17+
use UniFi_API\Exceptions\NotAnOsConsoleException;
1718

1819
/**
1920
* The UniFi API client class.
@@ -3261,6 +3262,40 @@ public function check_controller_update()
32613262
return $this->fetch_results('/api/s/' . $this->site . '/stat/fwupdate/latest-version');
32623263
}
32633264

3265+
/**
3266+
* Get the recent firmware update for an UniFi-OS console
3267+
*
3268+
* @return array|bool returns an array with a single object containing details of the current known latest
3269+
* UniFi OS version info on success, else returns false
3270+
* @throws Exception
3271+
*/
3272+
public function get_update_os_console()
3273+
{
3274+
if (!$this->is_unifi_os) {
3275+
throw new NotAnOsConsoleException();
3276+
}
3277+
3278+
return $this->fetch_results('/api/firmware/update', null, false, true, false);
3279+
}
3280+
3281+
/**
3282+
* Update the OS for an UniFi-OS console
3283+
*
3284+
* @note triggers an UniFi OS Update in Control Plane > Updates > UniFi OS
3285+
* @return bool true upon success
3286+
* @throws Exception
3287+
*/
3288+
public function update_os_console(): bool
3289+
{
3290+
if (!$this->is_unifi_os) {
3291+
throw new NotAnOsConsoleException();
3292+
}
3293+
3294+
$payload = ['persistFullData' => true];
3295+
3296+
return $this->fetch_results_boolean('/api/firmware/update', $payload, true, false);
3297+
}
3298+
32643299
/**
32653300
* Check firmware update.
32663301
*
@@ -4122,19 +4157,23 @@ protected function fetch_results(
41224157
string $path,
41234158
$payload = null,
41244159
bool $boolean = false,
4125-
bool $login_required = true
4160+
bool $login_required = true,
4161+
bool $prefix_path = true
41264162
)
41274163
{
41284164
/** Guard clause to check if logged in when needed. */
41294165
if ($login_required && !$this->is_logged_in) {
41304166
throw new LoginRequiredException();
41314167
}
41324168

4133-
$this->last_results_raw = $this->exec_curl($path, $payload);
4169+
$this->last_results_raw = $this->exec_curl($path, $payload, $prefix_path);
41344170

41354171
if (is_string($this->last_results_raw)) {
41364172
$response = json_decode($this->last_results_raw);
4137-
$this->get_json_last_error();
4173+
4174+
if (!empty($this->last_results_raw)) {
4175+
$this->get_json_last_error();
4176+
}
41384177

41394178
if (isset($response->meta->rc)) {
41404179
if ($response->meta->rc === 'ok') {
@@ -4174,6 +4213,24 @@ protected function fetch_results(
41744213

41754214
return $response;
41764215
}
4216+
4217+
/** Deal with a responses from an UniFi OS console */
4218+
if (strpos($path, '/api/') === 0) {
4219+
if (isset($response->code)) {
4220+
$this->last_error_message = 'An unknown error was returned by an UniFi OS endpoint.';
4221+
if (isset($response->message)) {
4222+
$this->last_error_message = $response->message;
4223+
}
4224+
4225+
throw new Exception('Error code: ' . $response->code . ', message: ' . $this->last_error_message);
4226+
}
4227+
4228+
if (is_object($response) && !$boolean) {
4229+
return [$response];
4230+
}
4231+
4232+
return true;
4233+
}
41774234
}
41784235

41794236
return false;
@@ -4189,9 +4246,14 @@ protected function fetch_results(
41894246
* @return bool [description]
41904247
* @throws Exception
41914248
*/
4192-
protected function fetch_results_boolean(string $path, $payload = null, bool $login_required = true): bool
4249+
protected function fetch_results_boolean(
4250+
string $path,
4251+
$payload = null,
4252+
bool $login_required = true,
4253+
bool $prefix_path = true
4254+
): bool
41934255
{
4194-
return $this->fetch_results($path, $payload, true, $login_required);
4256+
return $this->fetch_results($path, $payload, true, $login_required, $prefix_path);
41954257
}
41964258

41974259
/**
@@ -4381,15 +4443,15 @@ protected function response_header_callback($ch, string $header_line): int
43814443
* @return bool|string response returned by the controller API
43824444
* @throws CurlGeneralErrorException|CurlTimeoutException|InvalidCurlMethodException|LoginFailedException
43834445
*/
4384-
protected function exec_curl(string $path, $payload = null)
4446+
protected function exec_curl(string $path, $payload = null, $prefix_path = true)
43854447
{
43864448
if (!in_array($this->curl_method, self::CURL_METHODS_ALLOWED)) {
43874449
throw new InvalidCurlMethodException();
43884450
}
43894451

43904452
$url = $this->baseurl . $path;
43914453

4392-
if ($this->is_unifi_os) {
4454+
if ($this->is_unifi_os && $prefix_path) {
43934455
$url = $this->baseurl . '/proxy/network' . $path;
43944456
}
43954457

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace UniFi_API\Exceptions;
4+
5+
use Exception;
6+
7+
class NotAnOsConsoleException extends Exception
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct('This console is not an UniFi OS console.');
12+
}
13+
}

0 commit comments

Comments
 (0)