-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
1671 lines (1573 loc) · 67.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let debug = require('debug')('UnifiAPI');
let merge = require('merge');
let UnifiRequest = require('./lib/unifi-request');
let SSHSession = require('./lib/ssh-session');
let defaultOptions = {
'username': 'unifi',
'password': 'unifi',
'baseUrl': 'https://127.0.0.1:8443',
'debug': false,
'debugNet': false,
'gzip': true,
'site': 'default'
};
/**
* The main class and the initialization of the Unifi Access
* @param {object} options the options during initialization
* @param {string} options.baseUrl the URL where the Unifi controller is. Default https://127.0.0.1:8443
* @param {string} options.username default username
* @param {string} options.password default password
* @param {string} options.site default site. Default is "default"
* @param {boolean} options.debug if the debug log is enabled
* @param {boolean} options.debugNet if the debug of the request module is enabled
* @returns this
* @example let UnifiAPI = require('node-unifiapi');
* let unifi = UnifiAPI({
* baseUrl: 'https://127.0.0.1:8443', // The URL of the Unifi Controller
* username: 'ubnt',
* password: 'ubnt',
* // debug: true, // More debug of the API (uses the debug module)
* // debugNet: true // Debug of the network requests (uses request module)
* });
*/
function UnifiAPI(options) {
if (!(this instanceof UnifiAPI)) return new UnifiAPI(options);
merge(this, defaultOptions, options);
this.debugging(this.debug);
if (typeof this.net === 'undefined') {
this.net = new UnifiRequest(merge(true, defaultOptions, options));
}
debug('UnifiAPI Initialized with options %o', options);
}
/**
* Enable or disable the debug of the module
* @param {boolean} enable Enable or disable the debugging
* @returns {undefined}
*/
UnifiAPI.prototype.debugging = function(enabled) {
this.debug = enabled;
debug.enabled = this.debug ? true : false;
debug('Debug is', this.debug ? 'enabled' : 'disabled');
};
/**
* Generic network operation, executing Ubiquiti command under /api/s/{site}/... rest api
* @param {string} url The right part of the URL (/api/s/{site}/ is automatically added)
* @param {object} jsonParams optional. Default undefined. If it is defined and it is object, those will be the JSON POST attributes sent to the URL and the the default method is changed from GET to POST
* @param {object} headers optional. Default {}. HTTP headers that we require to be sent in the request
* @param {object} method optional. Default undefined. The HTTP request method. If undefined, then it is automatic. If no jsonParams specified, it will be GET. If jsonParams are specified it will be POST
* @param {string} site optional. The {site} atribute of the request. If not specified, it is taken from the UnifiAPI init options, where if it is not specified, it is "default"
* @return {Promise}
* @example unifi.netsite('/cmd/stamgr', { cmd: 'authorize-guest', mac: '00:01:02:03:04:05', minutes: 60 }, {}, 'POST', 'default')
* .then(data => console.log('Success', data))
* .catch(error => console.log('Error', error));
*/
UnifiAPI.prototype.netsite = function(url = '', jsonParams = undefined, headers = {}, method = undefined, site = undefined) {
site = site || this.site;
if (typeof method === 'undefined') {
if (typeof jsonParams === 'undefined') method = 'GET';
else method = 'POST';
}
return this.net.req('/api/s/' + site + url, jsonParams, headers, method)
};
/**
* @description Explicit login to the controller. It is not necessary, as every other method calls implicid login (with the default username and password) before execution
* @param {string} username The username
* @param {string} password The password
* @return {Promise} success or failure
* @example unifi.login(username, password)
* .then(data => console.log('success', data))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.login = function(username, password) {
return this.net.login(username, password);
};
/**
* Logout of the controller
* @example unifi.logout()
* .then(() => console.log('Success'))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.logout = function() {
return this.net.logout();
};
/**
* Authorize guest by a MAC address
* @param {string} mac mac address of the guest - mandatory
* @param {string} minutes minutes for the authorization - optional, default 60 min
* @param {string} up upstream bandwidth in Kbps. Default no limit
* @param {string} down downstream bandwidth in Kbps. Default no _limit
* @param {string} mbytes download limit in Mbytes. Default no limit
* @param {string} apmac to which mac address the authorization belongs. Default any
* @param {string} site to which site (Ubiquiti) the command will be applied if it is different than the default
* @return {Promise} Promise
* @example unifi.authorize_guest('01:02:aa:bb:cc')
* .then(data => console.log('Successful authorization'))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.authorize_guest = function(mac = '', minutes = 60, up = undefined, down = undefined, mbytes = undefined, apmac = undefined, site = undefined) {
return this.netsite('/cmd/stamgr', {
cmd: 'authorize-guest',
mac: mac.toLowerCase(),
minutes: minutes,
up: up,
down: down,
bytes: mbytes,
ap_mac: apmac && apmac.toLowerCase()
}, {}, undefined, site);
};
/**
* De-authorize guest by a MAC address
* @param {string} mac the mac address
* @param {site} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.unauthorize_guest('00:01:02:03:aa:bb')
* .then(done => console.log('Success', done))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.unauthorize_guest = function(mac = '', site = undefined) {
return this.netsite('/cmd/stamgr', {
cmd: 'uauthorize-guest',
mac: mac.toLowerCase()
}, {}, undefined, site);
};
/**
* Kick a client (station) of the network. This will disconnect a wireless user if it is connected
* @param {string} mac Mac address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.kick_sta('00:00:11:22:33:44')
* .then(done => console.log('Success', done))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.kick_sta = function(mac = '', site = undefined) {
return this.netsite('/cmd/stamgr', {
cmd: 'kick-sta',
mac: mac.toLowerCase()
}, {}, undefined, site);
};
/**
* Terminate access of a Guest (logged in via Guest Authorization). It kicks it out of the wireless and authroization
* @param {string} id the ID of the guest that have to be kicked out
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.terminate_guest('aa01af0133d334d77d')
* .this(done => console.log('Success', done))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.terminate_guest = function(id = '', site = undefined) {
return this.netsite('/cmd/hotspot', {
_id: id,
cmd: 'terminate'
}, {}, undefined, site);
};
/**
* Block station of the network
* @param {string} mac Mac address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.block_sta('00:01:02:03:04:05')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.block_sta = function(mac = '', site = undefined) {
return this.netsite('/cmd/stamgr', {
cmd: 'block-sta',
mac: mac.toLowerCase()
}, {}, undefined, site);
};
/**
* Unblock station of the network
* @param {string} mac Mac address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.block_sta('00:01:02:03:04:05')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error', err))
*/
UnifiAPI.prototype.unblock_sta = function(mac = '', site = undefined) {
return this.netsite('/cmd/stamgr', {
cmd: 'unblock-sta',
mac: mac.toLowerCase()
}, {}, undefined, site);
};
/**
* Set or remove Note to a station
* @param {string} user User ID
* @param {string} note Note
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_sta_note('aabbaa0102aa03aa3322','Test note')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
* @example unifi.set_sta_note('aabbaa0102aa03aa3322','') // remove note
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_sta_note = function(user = '', note = '', site = undefined) {
return this.netsite('/upd/user/' + user, {
note: note,
noted: note ? true : false
}, {}, undefined, site);
};
/**
* Set or remove Name to a station
* @param {string} user User ID
* @param {string} name Name
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_sta_name('aabbaa0102aa03aa3322','Central Access Point')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
* @example unifi.set_sta_name('aabbaa0102aa03aa3322','') // remove name
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_sta_name = function(user = '', name = '', site = undefined) {
return this.netsite('/upd/user/' + user, {
name: name
}, {}, undefined, site);
};
/**
* List client sessions
* @param {number} start Start time in Unix Timestamp - Optional. Default 7 days ago
* @param {number} end End time in Unix timestamp - Optional. Default - now
* @param {string} type Sessions type. Optional. Default all
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_sessions()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_sessions = function(start = undefined, end = undefined, type = 'all', site = undefined) {
return this.netsite('/stat/sessions', {
type: type,
start: start || (new Date()).getTime() / 1000 - 7 * 24 * 3600 * 1000,
end: end || (new Date()).getTime()
}, {}, undefined, site);
};
/**
* List daily site statistics
* @param {number} start Start time in Unix Timestamp - Optional. Default 7 days ago
* @param {number} end End time in Unix timestamp - Optional. Default - now
* @param {array} attrs What attributes we are quering for. Optional. Default [ 'bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time' ]
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_daily_site()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_daily_site = function(start = undefined, end = undefined, attrs = ['bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'], site = undefined) {
return this.netsite('/stat/report/daily.site', {
start: start ? start : (new Date()).getTime() - 52 * 7 * 24 * 3600 * 1000,
end: end ? end : (new Date()).getTime(),
attrs: attrs
}, {}, undefined, site);
};
/**
* List hourly site statistics
* @param {number} start Start time in Unix Timestamp - Optional. Default 7 days ago
* @param {number} end End time in Unix timestamp - Optional. Default - now
* @param {array} attrs What attributes we are quering for. Optional. Default [ 'bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time' ]
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_hourly_site()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_hourly_site = function(start = undefined, end = undefined, attrs = ['bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'], site = undefined) {
return this.netsite('/stat/report/hourly.site', {
start: start ? start : (new Date()).getTime() - 7 * 24 * 3600 * 1000,
end: end ? end : (new Date()).getTime(),
attrs: attrs
}, {}, undefined, site);
};
/**
* List hourly site statistics for ap
* @param {number} start Start time in Unix Timestamp - Optional. Default 7 days ago
* @param {number} end End time in Unix timestamp - Optional. Default - now
* @param {array} attrs What attributes we are quering for. Optional. Default [ 'bytes', 'num_sta', 'time' ]
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_hourly_ap()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_hourly_ap = function(start = undefined, end = undefined, attrs = ['bytes', 'num_sta', 'time'], site = undefined) {
return this.netsite('/stat/report/hourly.ap', {
start: start ? start : (new Date()).getTime() - 7 * 24 * 3600 * 1000,
end: end ? end : (new Date()).getTime(),
attrs: attrs
}, {}, undefined, site);
};
/**
* Last station sessions
* @param {string} mac Mac address
* @param {number} limit How many sessions. Optional. Default 5
* @param {string} sort Sorting. Optional. Default Ascending (asc)
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_sta_sessions_latest('00:01:02:03:04:05', 10)
* .then(done => console.log('Success', done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_sta_sessions_latest = function(mac = '', limit = 5, sort = '-asoc-time', site = undefined) {
return this.netsite('/stat/sessions', {
mac: mac.toLowerCase(),
'_limit': limit,
'_sort': sort
}, {}, undefined, site);
};
/**
* List authorizations
* @param {number} start Start time in Unix Timestamp - Optional. Default 7 days ago
* @param {number} end End time in Unix timestamp - Optional. Default - now
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_auths()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_auths = function(start = undefined, end = undefined, site = undefined) {
return this.netsite('/stat/authorization', {
end: end || (new Date()).getTime(),
start: start || (new Date()).getTime() - 7 * 24 * 3600000
}, {}, undefined, site);
};
/**
* List all users
* @param {number} historyhours How many hours back to query. Optional. Default 8670
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_allusers()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_allusers = function(historyhours = 8670, type = 'all', conn = 'all', site = undefined) {
return this.netsite('/stat/alluser', {
type: type,
conn: conn,
within: historyhours
}, {}, undefined, site);
};
/**
* List of guests (authorized via the guest portal)
* @param {number} historyhours How many hours back to query. Optional. Default 8670
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_guests()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_guests = function(historyhours = 8670, site = undefined) {
return this.netsite('/stat/guest', {
within: historyhours
}, {}, undefined, site);
};
/**
* List of guests (authorized via the guest portal) but with modern internal api
* @param {number} historyhours How many hours back to query. Optional. Default 8670
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_guests2()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_guests2 = function(historyhours = 8670, site = undefined) {
return this.netsite('/stat/guest?within=' + historyhours, undefined, {}, undefined, site);
};
/**
* List of (all) clients per station
* @param {string} mac Mac address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_clients()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_clients = function(mac = '', site = undefined) {
return this.netsite('/stat/sta/' + mac, undefined, {}, undefined, site);
};
/**
* List of group of clients per station
* @param {string} macs String mac or array of mac addresses as strings, to get information about them
* @param {string} ap Station man address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_some_clients()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_some_clients = function(macs = undefined, ap = '', site = undefined) {
var clients = undefined;
if (macs) {
if (typeof macs == 'string') clients = { macs: [ macs ] }
else if (macs instanceof Array) clients = { macs: macs };
}
return this.netsite('/stat/sta/' + ap, clients, {}, undefined, site);
};
/**
* Statistics of (all) clients per station
* @param {string} mac Mac address
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_client()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_client = function(mac = '', site = undefined) {
return this.netsite('/stat/user/' + mac, undefined, {}, undefined, site);
};
/**
* List of the usergroups
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_usergroup()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_usergroup = function(site = undefined) {
return this.netsite('/list/usergroup', undefined, {}, undefined, site);
};
/**
* Add user to a group
* @param {string} userid ID of the user
* @param {string} groupid ID of the group
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_usergroup('11aa22bb33cc44dd55ee66ff', '112233445566778899aabb')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_usergroup = function(userid = '', groupid = '', site = undefined) {
return this.netsite('/upd/user/' + userid, {
usergroup_id: groupid
}, {}, undefined, site);
};
/**
* List health
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_health()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_health = function(site = undefined) {
return this.netsite('/stat/health', undefined, {}, undefined, site);
};
/**
* List dashboard
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_dashboard()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_dashboard = function(site = undefined) {
return this.netsite('/stat/dashboard', undefined, {}, undefined, site)
};
/**
* List users
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_users()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_users = function(site = undefined) {
return this.netsite('/list/user', undefined, {}, undefined, site);
};
/**
* List APs
* @param {string} mac AP mac/id, Optional
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_aps()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_aps = function(mac = '', site = undefined) { // TODO: not working with mac different than none
return this.netsite('/stat/device/' + mac, undefined, {}, undefined, site)
};
/**
* List Rogue APs
* @param {number} within For how many hours back. Optional. Default 24h
* @param {string} site Ubiquiti site, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_rogueaps()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_rogueaps = function(within = 24, site = undefined) {
return this.netsite('/stat/rogueap', {
within: within
}, {}, undefined, site);
};
/**
* List sites
* @return {Promise} Promise
* @example unifi.list_sites()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_sites = function() {
return this.net.req('/api/self/sites');
};
/**
* Sites stats
* @return {Promise} Promise
* @example unifi.stat_sites()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_sites = function() {
return this.net.req('/api/stat/sites');
};
/**
* Add new site
* @param {string} name name
* @param {string} description description - optional
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.add_site('mysite','Experimental site')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.add_site = function(name = 'default', description = '', site = undefined) {
return this.netsite('/cmd/sitemgr', site = site, {
cmd: 'add-site',
name: name,
desc: description
}, {}, undefined, site);
};
/**
* Remove site
* @param {string} name name
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.remove_site('mysite')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.remove_site = function(name = 'none', site = undefined) { // TODO: test it
return this.netsite('/cmd/sitemgr', site = site, {
cmd: 'remove-site',
name: name
}, {}, undefined, site);
};
/**
* List WLANGroups
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_wlan_groups()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_wlan_groups = function(site = undefined) {
return this.netsite('/list/wlangroup', undefined, {}, undefined, site);
};
/**
* Stat Sysinfo
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_sysinfo()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_sysinfo = function(site = undefined) {
return this.netsite('/stat/sysinfo', undefined, {}, undefined, site);
};
/**
* Get information aboult self (username, etc)
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_self()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_self = function(site = undefined) { // TODO: test
return this.netsite('/self', undefined, {}, undefined, site);
};
/**
* Get information aboult the network configuration
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_networkconf()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_networkconf = function(site = undefined) {
return this.netsite('/list/networkconf', undefined, {}, undefined, site);
};
/**
* Get accounting / status of the vouchers
* @param {number} createtime Unixtimestamp since when we return information about the vouchers. Optional. Default any
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_voucher()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_voucher = function(createtime = undefined, site = undefined) {
return this.netsite('/stat/voucher', {
create_time: createtime
}, {}, undefined, site);
};
/**
* Get accounting / status of the payments
* @param {number} within how many hours back we query. Optional. Default any
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.stat_payment()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.stat_payment = function(within = undefined, site = undefined) {
return this.netsite('/stat/payment', { // TODO: test it, is it payment or voucher
within: within
}, {}, undefined, site);
};
/**
* Create HotSpot (version 1)
* @todo Check if the URL of the rest service is correct
* @todo Test that it is working
* @param {string} name name
* @param {string} password password
* @param {string} note Note (optional)
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.create_hotspot('myhotspot', 'password', 'note')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.create_hotspot = function(name = '', password = '', note = '', site = undefined) {
return this.netsite('/stat/voucher', {
name: name,
note: note,
x_password: password
}, {}, undefined, site);
};
/**
* List all of the hotspots (v1)
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_hotspot()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_hotspot = function(site = undefined) {
return this.netsite('/list/hotspotop', undefined, {}, undefined, site);
};
/**
* Create vouchers. Generate a set of vouchers
* @param {number} count how many vouchers to generate. Optional. default is 1
* @param {number} minutes how long the voucher may be active after activation in minutes. Optional. default is 60 minutes
* @param {number} quota how many times a user may reuse (login with) this voucher. Default 0 = unlimited. 1 means only once. 2 means two times and so on
* @param {string} note the note of the voucher. Optional
* @param {number} up Upstream bandwidth rate limit in Kbits. Optional. Default - no limit
* @param {number} down Downstream bandwidth rate limit in Kbits. Optional. Default - no limit
* @param {number} mbytes Limit of the maximum download traffic in Mbytes. Optional. Default - no limit
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.create_voucher(10, 2880, 1, 'Test vouchers', 1000, 2000, 250)
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.create_voucher = function(count = 1, minutes = 60, quota = 0, note = undefined, up = undefined, down = undefined, mbytes = undefined, site = undefined) {
return this.netsite('/cmd/hotspot', {
note: note,
up: up,
down: down,
bytes: mbytes,
cmd: 'create-voucher',
expire: minutes,
n: count,
quota: quota
}, {}, undefined, site);
};
/**
* Revoke Voucher. Voucher revoking is the same as deleting the voucher. In most of the cases the authorized user is kicked out of the network too
* @param {string} voucher_id description
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.revoke_voucher('9912982aaff182728a0f03')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.revoke_voucher = function(voucher_id, site = undefined) {
return this.netsite('/cmd/hotspot', {
cmd: 'delete-voucher',
_id: voucher_id
}, {}, undefined, site);
};
/**
* List port forwarding configuration
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_portforwarding()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_portforwarding = function(site = undefined) {
return this.netsite('/list/portforward', undefined, {}, undefined, site);
};
/**
* List dynamic dns configuration
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_dynamicdns()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_dynamicdns = function(site = undefined) {
return this.netsite('/list/dynamicdns', undefined, {}, undefined, site);
};
/**
* List network port configuration
* @todo Test it
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_portconf()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_portconf = function(site = undefined) {
return this.netsite('/list/portconf', undefined, {}, undefined, site);
};
/**
* List extensions
* @todo Learn more what exactly is this
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_extension()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_extension = function(site = undefined) {
return this.netsite('/list/extension', undefined, {}, undefined, site);
};
/**
* Get array with all the settings refered by settings key
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.list_settings()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.list_settings = function(site = undefined) {
return this.netsite('/get/setting', undefined, {}, undefined, site)
};
/**
* Restart Wireless Access Point
* @param {string} mac mac address of the AP
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.restart_ap('00:01:02:03:aa:04')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.restart_ap = function(mac = '', site = undefined) {
return this.netsite('/cmd/devmgr', {
cmd: 'restart',
mac: mac.toLowerCase()
}, {}, undefined, site);
};
/**
* Disable Wireless Access Point
* @param {string} ap_id The internal ID of the AP
* @param {boolean} disable Shall we disable it. Optional. Default true. If false, the AP is enabled
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.disable_ap('001fa98a00a22328123')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.disable_ap = function(ap_id = '', disable = true, site = undefined) {
return this.netsite('/rest/device/' + ap_id, {
disabled: disable
}, {}, undefined, site);
};
/**
* Enable Wireless Access Point
* @param {string} ap_id The internal ID of the AP
* @param {boolean} disable Shall we disable it. Optional. Default true. If false, the AP is enabled
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.enable_ap('001fa98a00a22328123')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.enable_ap = function(ap_id = '', disable = false, site = undefined) {
return this.disable_ap(ap_id, disable, site);
};
/**
* Locate Wireless Access Point. The Access Point will start blinking
* @param {string} mac mac of the AP
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_locate_ap('00:01:aa:03:04:05')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_locate_ap = function(mac = '', site = undefined) {
return this.netsite('/cmd/devmgr', {
mac: mac.toLowerCase(),
cmd: 'set-locate'
}, {}, undefined, site);
};
/**
* Turn off Locate Wireless Access Point. The Access Point will stop blinking
* @param {string} mac mac of the AP
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.unset_locate_ap('00:01:aa:03:04:05')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.unset_locate_ap = function(mac = '', site = undefined) {
return this.netsite('/cmd/devmgr', {
mac: mac.toLowerCase(),
cmd: 'unset-locate'
}, {}, undefined, site);
};
/**
* All devices in the site group will start blinking
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.site_ledson()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.site_ledson = function(site = undefined) {
return this.netsite('/set/setting/mgmt', {
led_enabled: true
}, {}, undefined, site);
};
/**
* All devices in the site group will stop blinking
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.site_ledsoff()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.site_ledsoff = function(site = undefined) {
return this.netsite('/set/setting/mgmt', {
led_enabled: false
}, {}, undefined, site);
};
/**
* Change AP wireless settings
* @param {string} ap_id internal id of the AP
* @param {string} radio The radio type. Supports ng or ac. Default ng. Optional
* @param {number} channel Wireless channel. Optional. Default 1. Could be string 'auto'
* @param {number} ht HT width in MHz. 20, 40, 80, 160. Optional. Default 20
* @param {number} tx_power_mode TX Power Mode. Optional. Default 0
* @param {number} tx_power TX Power. Optional. Default 0
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_ap_radiosettings('aa0101023faabbaacc0c0', 'ng', 3, 20)
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_ap_radiosettings = function(ap_id = '', radio = 'ng', channel = 1, ht = '20', tx_power_mode = 0, tx_power = 0) {
return this.netsite('/upd/device/' + ap_id, {
radio: radio,
channel: channel,
ht: ht,
tx_power_mode: tx_power_mode,
tx_power: tx_power
}, {}, undefined, site);
};
/**
* Alias to list_settings. Retrieve array with settings defined by setting key.
* @alias list_settings
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.get_settings()
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.get_settings = function(site = undefined) {
return this.netsite('/get/setting', undefined, {}, undefined, site);
};
/**
* Retrieve settings by a specific settings key. Only elements with this settings key will be returned in the array. Usually 1 or 0
* Typical keys are mgmt, snmp, porta, locale, rsyslogd, auto_speedtest, country, connectivity
* @param {string} key key
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.get_settings_by_key('mgmt')
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.get_settings_by_key = function(key, site = undefined) {
return new Promise((resolve, reject) => {
this.get_settings(site)
.then((data) => {
data.data = data.data.filter(n => n.key == key);
resolve(data);
})
.catch(reject);
});
};
/**
* Set settings by key modifies properties of the settings, defined by key
* @param {string} key key
* @param {object} obj object of properties that overwrite the original values
* @param {string} site Ubiquiti site to query, if different from default - optional
* @return {Promise} Promise
* @example unifi.set_settings_by_key('mgmt', { auto_upgrade: true })
* .then(done => console.log('Success',done))
* .catch(err => console.log('Error',err))
*/
UnifiAPI.prototype.set_settings = function(key, obj, site = undefined) {
return new Promise((resolve, reject) => {
this.get_settings_by_key(key, site)
.then((data) => {
if (data.data.length < 1) return reject({ msg: 'No such key', meta: { rc: 'error' } });
let o = merge(true, data.data[0], obj);
return this.netsite('/set/setting/' + o.key + '/' + o._id, o, {}, undefined, site);
})
.then(resolve)
.catch(reject);
});
};
/**
* Set Guest Settings and Guest Access Portal are created with this method