14
14
use UniFi_API \Exceptions \LoginFailedException ;
15
15
use UniFi_API \Exceptions \LoginRequiredException ;
16
16
use UniFi_API \Exceptions \MethodDeprecatedException ;
17
+ use UniFi_API \Exceptions \NotAnOsConsoleException ;
17
18
18
19
/**
19
20
* The UniFi API client class.
@@ -3257,6 +3258,40 @@ public function check_controller_update()
3257
3258
return $ this ->fetch_results ('/api/s/ ' . $ this ->site . '/stat/fwupdate/latest-version ' );
3258
3259
}
3259
3260
3261
+ /**
3262
+ * Get the recent firmware update for an UniFi-OS console
3263
+ *
3264
+ * @return array|bool returns an array with a single object containing details of the current known latest
3265
+ * UniFi OS version info on success, else returns false
3266
+ * @throws Exception
3267
+ */
3268
+ public function get_update_os_console ()
3269
+ {
3270
+ if (!$ this ->is_unifi_os ) {
3271
+ throw new NotAnOsConsoleException ();
3272
+ }
3273
+
3274
+ return $ this ->fetch_results ('/api/firmware/update ' , null , false , true , false );
3275
+ }
3276
+
3277
+ /**
3278
+ * Update the OS for an UniFi-OS console
3279
+ *
3280
+ * @note triggers an UniFi OS Update in Control Plane > Updates > UniFi OS
3281
+ * @return bool true upon success
3282
+ * @throws Exception
3283
+ */
3284
+ public function update_os_console (): bool
3285
+ {
3286
+ if (!$ this ->is_unifi_os ) {
3287
+ throw new NotAnOsConsoleException ();
3288
+ }
3289
+
3290
+ $ payload = ['persistFullData ' => true ];
3291
+
3292
+ return $ this ->fetch_results_boolean ('/api/firmware/update ' , $ payload , true , false );
3293
+ }
3294
+
3260
3295
/**
3261
3296
* Check firmware update.
3262
3297
*
@@ -4174,19 +4209,23 @@ protected function fetch_results(
4174
4209
string $ path ,
4175
4210
$ payload = null ,
4176
4211
bool $ boolean = false ,
4177
- bool $ login_required = true
4212
+ bool $ login_required = true ,
4213
+ bool $ prefix_path = true
4178
4214
)
4179
4215
{
4180
4216
/** Guard clause to check if logged in when needed. */
4181
4217
if ($ login_required && !$ this ->is_logged_in ) {
4182
4218
throw new LoginRequiredException ();
4183
4219
}
4184
4220
4185
- $ this ->last_results_raw = $ this ->exec_curl ($ path , $ payload );
4221
+ $ this ->last_results_raw = $ this ->exec_curl ($ path , $ payload, $ prefix_path );
4186
4222
4187
4223
if (is_string ($ this ->last_results_raw )) {
4188
4224
$ response = json_decode ($ this ->last_results_raw );
4189
- $ this ->get_json_last_error ();
4225
+
4226
+ if (!empty ($ this ->last_results_raw )) {
4227
+ $ this ->get_json_last_error ();
4228
+ }
4190
4229
4191
4230
if (isset ($ response ->meta ->rc )) {
4192
4231
if ($ response ->meta ->rc === 'ok ' ) {
@@ -4226,6 +4265,24 @@ protected function fetch_results(
4226
4265
4227
4266
return $ response ;
4228
4267
}
4268
+
4269
+ /** Deal with a responses from an UniFi OS console */
4270
+ if (strpos ($ path , '/api/ ' ) === 0 ) {
4271
+ if (isset ($ response ->code )) {
4272
+ $ this ->last_error_message = 'An unknown error was returned by an UniFi OS endpoint. ' ;
4273
+ if (isset ($ response ->message )) {
4274
+ $ this ->last_error_message = $ response ->message ;
4275
+ }
4276
+
4277
+ throw new Exception ('Error code: ' . $ response ->code . ', message: ' . $ this ->last_error_message );
4278
+ }
4279
+
4280
+ if (is_object ($ response ) && !$ boolean ) {
4281
+ return [$ response ];
4282
+ }
4283
+
4284
+ return true ;
4285
+ }
4229
4286
}
4230
4287
4231
4288
return false ;
@@ -4241,9 +4298,14 @@ protected function fetch_results(
4241
4298
* @return bool [description]
4242
4299
* @throws Exception
4243
4300
*/
4244
- protected function fetch_results_boolean (string $ path , $ payload = null , bool $ login_required = true ): bool
4301
+ protected function fetch_results_boolean (
4302
+ string $ path ,
4303
+ $ payload = null ,
4304
+ bool $ login_required = true ,
4305
+ bool $ prefix_path = true
4306
+ ): bool
4245
4307
{
4246
- return $ this ->fetch_results ($ path , $ payload , true , $ login_required );
4308
+ return $ this ->fetch_results ($ path , $ payload , true , $ login_required, $ prefix_path );
4247
4309
}
4248
4310
4249
4311
/**
@@ -4433,15 +4495,15 @@ protected function response_header_callback($ch, string $header_line): int
4433
4495
* @return bool|string response returned by the controller API
4434
4496
* @throws CurlGeneralErrorException|CurlTimeoutException|InvalidCurlMethodException|LoginFailedException
4435
4497
*/
4436
- protected function exec_curl (string $ path , $ payload = null )
4498
+ protected function exec_curl (string $ path , $ payload = null , $ prefix_path = true )
4437
4499
{
4438
4500
if (!in_array ($ this ->curl_method , self ::CURL_METHODS_ALLOWED )) {
4439
4501
throw new InvalidCurlMethodException ();
4440
4502
}
4441
4503
4442
4504
$ url = $ this ->baseurl . $ path ;
4443
4505
4444
- if ($ this ->is_unifi_os ) {
4506
+ if ($ this ->is_unifi_os && $ prefix_path ) {
4445
4507
$ url = $ this ->baseurl . '/proxy/network ' . $ path ;
4446
4508
}
4447
4509
0 commit comments