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.
@@ -3261,6 +3262,40 @@ public function check_controller_update()
3261
3262
return $ this ->fetch_results ('/api/s/ ' . $ this ->site . '/stat/fwupdate/latest-version ' );
3262
3263
}
3263
3264
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
+
3264
3299
/**
3265
3300
* Check firmware update.
3266
3301
*
@@ -4122,19 +4157,23 @@ protected function fetch_results(
4122
4157
string $ path ,
4123
4158
$ payload = null ,
4124
4159
bool $ boolean = false ,
4125
- bool $ login_required = true
4160
+ bool $ login_required = true ,
4161
+ bool $ prefix_path = true
4126
4162
)
4127
4163
{
4128
4164
/** Guard clause to check if logged in when needed. */
4129
4165
if ($ login_required && !$ this ->is_logged_in ) {
4130
4166
throw new LoginRequiredException ();
4131
4167
}
4132
4168
4133
- $ this ->last_results_raw = $ this ->exec_curl ($ path , $ payload );
4169
+ $ this ->last_results_raw = $ this ->exec_curl ($ path , $ payload, $ prefix_path );
4134
4170
4135
4171
if (is_string ($ this ->last_results_raw )) {
4136
4172
$ 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
+ }
4138
4177
4139
4178
if (isset ($ response ->meta ->rc )) {
4140
4179
if ($ response ->meta ->rc === 'ok ' ) {
@@ -4174,6 +4213,24 @@ protected function fetch_results(
4174
4213
4175
4214
return $ response ;
4176
4215
}
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
+ }
4177
4234
}
4178
4235
4179
4236
return false ;
@@ -4189,9 +4246,14 @@ protected function fetch_results(
4189
4246
* @return bool [description]
4190
4247
* @throws Exception
4191
4248
*/
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
4193
4255
{
4194
- return $ this ->fetch_results ($ path , $ payload , true , $ login_required );
4256
+ return $ this ->fetch_results ($ path , $ payload , true , $ login_required, $ prefix_path );
4195
4257
}
4196
4258
4197
4259
/**
@@ -4381,15 +4443,15 @@ protected function response_header_callback($ch, string $header_line): int
4381
4443
* @return bool|string response returned by the controller API
4382
4444
* @throws CurlGeneralErrorException|CurlTimeoutException|InvalidCurlMethodException|LoginFailedException
4383
4445
*/
4384
- protected function exec_curl (string $ path , $ payload = null )
4446
+ protected function exec_curl (string $ path , $ payload = null , $ prefix_path = true )
4385
4447
{
4386
4448
if (!in_array ($ this ->curl_method , self ::CURL_METHODS_ALLOWED )) {
4387
4449
throw new InvalidCurlMethodException ();
4388
4450
}
4389
4451
4390
4452
$ url = $ this ->baseurl . $ path ;
4391
4453
4392
- if ($ this ->is_unifi_os ) {
4454
+ if ($ this ->is_unifi_os && $ prefix_path ) {
4393
4455
$ url = $ this ->baseurl . '/proxy/network ' . $ path ;
4394
4456
}
4395
4457
0 commit comments