-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
unifi-helper.js
2220 lines (1993 loc) · 83.5 KB
/
unifi-helper.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
const axios = require('axios');
const { CookieJar } = require('tough-cookie');
const { HttpCookieAgent, HttpsCookieAgent } = require('http-cookie-agent/http');
var Controller = function (hostname, port, unifios, ssl, debug) {
var _self = this;
_self._cookieJar = new CookieJar();
_self._unifios = unifios;
_self._ssl = ssl;
_self._baseurl = 'https://127.0.0.1:8443';
_self._loggedIn = false;
_self._debug = debug;
if (typeof (hostname) !== 'undefined' && typeof (port) !== 'undefined') {
_self._baseurl = 'https://' + hostname + ':' + port;
}
const jar = _self._cookieJar;
const axiosinstance = axios.create({
httpAgent: new HttpCookieAgent({ cookies: { jar } }),
httpsAgent: new HttpsCookieAgent({ cookies: { jar }, rejectUnauthorized: _self._ssl, requestCert: true })
});
/**
* Login to UniFi Controller - login()
*/
_self.login = function (username, password, cb) {
if (_self._loggedIn) {
cb(false, "");
} else {
if (_self._unifios)
_self._request('/api/auth/login', { username: username, password: password }, null, cb, 'POST');
else
_self._request('/api/login', { username: username, password: password }, null, cb, 'POST');
_self._loggedIn = true;
}
};
/**
* Logout from UniFi Controller - logout()
*/
_self.logout = function (cb) {
if (_self._unifios) {
//_self._request('/api/auth/logout', {}, null, cb, 'POST');
}
else {
//_self._request('/api/logout', {}, null, cb, 'POST');
}
};
//#region
/**
* Authorize a client device - authorize_guest()
* -------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
* required parameter <minutes> = minutes (from now) until authorization expires
* required paramater <cb> = the callback function that is called with the results
* optional parameter <up> = upload speed limit in kbps
* optional parameter <down> = download speed limit in kbps
* optional parameter <MBytes> = data transfer limit in MB
* optional parameter <ap_mac> = AP MAC address to which client is connected, should result in faster authorization
*/
_self.authorizeGuest = function (sites, mac, minutes, cb, up, down, mbytes, ap_mac) {
var json = { cmd: 'authorize-guest', mac: mac.toLowerCase() };
if (typeof (minutes) !== 'undefined') json.minutes = minutes;
if (typeof (up) !== 'undefined') json.up = up;
if (typeof (down) !== 'undefined') json.down = down;
if (typeof (mbytes) !== 'undefined') json.bytes = mbytes;
if (typeof (ap_mac) !== 'undefined') json.ap_mac = ap_mac;
_self._request('/api/s/<SITE>/cmd/stamgr', json, sites, cb);
};
/**
* Unauthorize a client device - unauthorize_guest()
* ---------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
*/
_self.unauthorizeGuest = function (sites, mac, cb) {
var json = { cmd: 'unauthorize-guest', mac: mac.toLowerCase() };
_self._request('/api/s/<SITE>/cmd/stamgr', json, sites, cb);
};
/**
* Reconnect a client device - reconnect_sta()
* -------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
*/
_self.reconnectClient = function (sites, mac, cb) {
var json = { cmd: 'kick-sta', mac: mac.toLowerCase() };
_self._request('/api/s/<SITE>/cmd/stamgr', json, sites, cb);
};
/**
* Block a client device - block_sta()
* ---------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
*/
_self.blockClient = function (sites, mac, cb) {
var json = { cmd: 'block-sta', mac: mac.toLowerCase() };
_self._request('/api/s/<SITE>/cmd/stamgr', json, sites, cb);
};
/**
* Unblock a client device - unblock_sta()
* -----------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
*/
_self.unblockClient = function (sites, mac, cb) {
var json = { cmd: 'unblock-sta', mac: mac.toLowerCase() };
_self._request('/api/s/<SITE>/cmd/stamgr', json, sites, cb);
};
/**
* Add/modify a client device note - set_sta_note()
* -------------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <user_id> = id of the user device to be modified
* optional parameter <note> = note to be applied to the user device
*
* NOTES:
* - when note is empty or not set, the existing note for the user will be removed and "noted" attribute set to FALSE
*/
_self.setClientNote = function (sites, user_id, cb, note) {
var noted = 1;
if (typeof (note) === 'undefined') {
note = '';
noted = 0;
}
_self._request('/api/s/<SITE>/upd/user/' + user_id.trim(), { note: note, noted: noted }, sites, cb);
};
/**
* Add/modify a client device name - set_sta_name()
* -------------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <user_id> = id of the user device to be modified
* optional parameter <name> = name to be applied to the user device
*
* NOTES:
* - when name is empty or not set, the existing name for the user will be removed
*/
_self.setClientName = function (sites, user_id, cb, name) {
if (typeof (name) === 'undefined')
name = '';
_self._request('/api/s/<SITE>/upd/user/' + user_id.trim(), { name: name }, sites, cb);
};
/**
* Daily site stats method - stat_daily_site()
* -----------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 52*7*24 hours
* - "bytes" are no longer returned with controller version 4.9.1 and later
*/
_self.getDailySiteStats = function (sites, cb, start, end) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (52 * 7 * 24 * 3600);
var json = {
attrs: ['bytes',
'wan-tx_bytes',
'wan-rx_bytes',
'wlan_bytes',
'num_sta',
'lan-num_sta',
'wlan-num_sta',
'time'],
start: start,
end: end
};
_self._request('/api/s/<SITE>/stat/report/daily.site', json, sites, cb);
};
/**
* Hourly site stats method - stat_hourly_site()
* ------------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
* - "bytes" are no longer returned with controller version 4.9.1 and later
*/
_self.getHourlySiteStats = function (sites, cb, start, end) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (7 * 24 * 3600);
var json = {
attrs: ['bytes',
'wan-tx_bytes',
'wan-rx_bytes',
'wlan_bytes',
'num_sta',
'lan-num_sta',
'wlan-num_sta',
'time'],
start: start,
end: end
};
_self._request('/api/s/<SITE>/stat/report/hourly.site', json, sites, cb);
};
/**
* Hourly stats method for all access points - stat_hourly_aps()
* -----------------------------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
* - UniFi controller does not keep these stats longer than 5 hours with versions < 4.6.6
*/
_self.getHourlyApStats = function (sites, cb, start, end) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (7 * 24 * 3600);
var json = {
attrs: ['bytes',
'num_sta',
'time'],
start: start,
end: end
};
_self._request('/api/s/<SITE>/stat/report/hourly.ap', json, sites, cb);
};
/**
* Daily stats method for all access points - stat_daily_aps()
* ----------------------------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
* - UniFi controller does not keep these stats longer than 5 hours with versions < 4.6.6
*/
_self.getDailyApStats = function (sites, cb, start, end) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (7 * 24 * 3600);
var json = {
attrs: ['bytes',
'num_sta',
'time'],
start: start,
end: end
};
_self._request('/api/s/<SITE>/stat/report/daily.ap', json, sites, cb);
};
/**
* Show all login sessions - stat_sessions()
* -----------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = client MAC address to return sessions for (can only be used when start and end are also provided)
*
* NOTES:
* - defaults to the past 7*24 hours
*/
_self.getSessions = function (sites, cb, start, end, mac) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (7 * 24 * 3600);
var json = {
type: 'all',
start: start,
end: end
};
if (typeof (mac) !== 'undefined')
json.mac = mac.toLowerCase();
_self._request('/api/s/<SITE>/stat/session', json, sites, cb);
};
/**
* Show latest 'n' login sessions for a single client device - stat_sta_sessions_latest()
* ---------------------------------------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <mac> = client MAC address
* optional parameter <limit> = maximum number of sessions to get (defaults to 5)
*
*/
_self.getLatestSessions = function (sites, mac, cb, limit) {
if (typeof (limit) === 'undefined')
limit = 5;
var json = {
mac: mac.toLowerCase(),
_limit: limit,
_sort: '-assoc_time'
};
_self._request('/api/s/<SITE>/stat/session', json, sites, cb);
};
/**
* Show all authorizations - stat_auths()
* -----------------------
*
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
*/
_self.getAllAuthorizations = function (sites, cb, start, end) {
if (typeof (end) === 'undefined')
end = Math.floor(Date.now() / 1000);
if (typeof (start) === 'undefined')
start = end - (7 * 24 * 3600);
_self._request('/api/s/<SITE>/stat/authorization', { start: start, end: end }, sites, cb);
};
/**
* List all client devices ever connected to the site - stat_allusers()
* --------------------------------------------------
*
* optional parameter <historyhours> = hours to go back (default is 8760 hours or 1 year)
*
* NOTES:
* - <historyhours> is only used to select clients that were online within that period,
* the returned stats per client are all-time totals, irrespective of the value of <historyhours>
*/
_self.getAllUsers = function (sites, cb, within) {
if (typeof (within) === 'undefined')
within = 8760;
var json = {
type: 'all',
conn: 'all',
within: within
};
_self._request('/api/s/<SITE>/stat/alluser', json, sites, cb, 'POST');
};
/**
* List all blocked client devices ever connected to the site
* ----------------------------------------------------------
*
* optional parameter <historyhours> = hours to go back (default is 8760 hours or 1 year)
*
* NOTES:
* - <historyhours> is only used to select clients that were online within that period,
* the returned stats per client are all-time totals, irrespective of the value of <historyhours>
*/
_self.getBlockedUsers = function (sites, cb, within) {
if (typeof (within) === 'undefined')
within = 8760;
var json = {
type: 'blocked',
conn: 'all',
within: within
};
_self._request('/api/s/<SITE>/stat/alluser', json, sites, cb, 'POST');
};
/**
* List guest devices - list_guests()
* ------------------
*
* optional parameter <within> = time frame in hours to go back to list guests with valid access (default = 24*365 hours)
*
*/
_self.getGuests = function (sites, cb, within) {
if (typeof (within) === 'undefined')
within = 8760;
_self._request('/api/s/<SITE>/stat/guest', { within: within }, sites, cb);
};
/**
* List online client device(s) - list_clients()
* ----------------------------
* returns an array of online client device objects, or in case of a single device request, returns a single client device object
*
* required paramater <sites> = name or array of site names
* optional parameter <client_mac> = the MAC address of a single online client device for which the call must be made
*/
_self.getClientDevices = function (sites, cb, client_mac) {
if (typeof (client_mac) === 'undefined')
client_mac = '';
_self._request('/api/s/<SITE>/stat/sta/' + client_mac.trim().toLowerCase(), null, sites, cb);
};
/**
* Get data for a single client device - stat_client()
* -----------------------------------
*
* required paramater <sites> = name or array of site names
* optional parameter <client_mac> = the MAC address of a single online client device for which the call must be made
*/
_self.getClientDevice = function (sites, cb, client_mac) {
if (typeof (client_mac) === 'undefined')
client_mac = '';
_self._request('/api/s/<SITE>/stat/user/' + client_mac.trim().toLowerCase(), null, sites, cb);
};
/**
* List user groups - list_usergroups()
* ----------------
*
* required paramater <sites> = name or array of site names
*/
_self.getUserGroups = function (sites, cb) {
_self._request('/api/s/<SITE>/list/usergroup', null, sites, cb);
};
/**
* Assign user device to another group - set_usergroup()
* -----------------------------------
*
* required paramater <sites> = name or array of site names
* required parameter <user_id> = id of the user device to be modified
* required parameter <group_id> = id of the user group to assign user to
*
*/
_self.setUserGroup = function (sites, user_id, group_id, cb) {
_self._request('/api/s/<SITE>/upd/user/' + user_id.trim(), { usergroup_id: group_id }, sites, cb);
};
/**
* Edit user group - edit_usergroup()
* ---------------
* returns an array containing a single object with attributes of the updated usergroup on success
*
* required paramater <sites> = name or array of site names
* required parameter <group_id> = id of the user group
* required parameter <site_id> = id of the site
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*
*/
_self.editUserGroup = function (sites, group_id, site_id, group_name, cb,
group_dn, group_up) {
var json = {
_id: group_id,
site_id: site_id,
name: group_name,
qos_rate_max_down: typeof (group_dn) !== 'undefined' ? group_dn : -1,
qos_rate_max_up: typeof (group_up) !== 'undefined' ? group_up : -1
};
_self._request('/api/s/<SITE>/rest/usergroup/' + group_id.trim(), json, sites, cb);
};
/**
* Add user group - add_usergroup()
* --------------
* returns an array containing a single object with attributes of the new usergroup ("_id", "name", "qos_rate_max_down", "qos_rate_max_up", "site_id") on success
*
* required paramater <sites> = name or array of site names
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*
*/
_self.addUserGroup = function (sites, group_name, cb,
group_dn, group_up) {
var json = {
name: group_name,
qos_rate_max_down: typeof (group_dn) !== 'undefined' ? group_dn : -1,
qos_rate_max_up: typeof (group_up) !== 'undefined' ? group_up : -1
};
_self._request('/api/s/<SITE>/rest/usergroup', json, sites, cb);
};
/**
* Delete user group - delete_usergroup()
* -----------------
* returns true on success
*
* required paramater <sites> = name or array of site names
* required parameter <group_id> = id of the user group
*
*/
_self.deleteUserGroup = function (sites, group_id, cb) {
_self._request('/api/s/<SITE>/rest/usergroup/' + group_id.trim(), null, sites, cb, 'DELETE');
};
/**
* List health metrics - list_health()
* -------------------
*
* required paramater <sites> = name or array of site names
*/
_self.getHealth = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/health', null, sites, cb);
};
/**
* List dashboard metrics - list_dashboard()
* ----------------------
*
* required paramater <sites> = name or array of site names
*/
_self.getDashboard = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/dashboard', null, sites, cb);
};
/**
* List user devices - list_users()
* -----------------
*
* required paramater <sites> = name or array of site names
*/
_self.getUsers = function (sites, cb) {
_self._request('/api/s/<SITE>/list/user', null, sites, cb);
};
/**
* Update device settings, base (using REST)
* -----------------------------------------
* required paramater <sites> = name or array of site names
* required parameter <device_id> = 24 char string; _id of the device which can be found with the getAccessDevices() function
* required parameter <device_settings> = object containing the configuration to apply to the device, must be a
* (partial) object structured in the same manner as is returned by getAccessDevices() for the devic$
* optional paramater <cb> = the callback function that is called with the results
*/
_self.setDeviceSettingsBase = function (sites, device_id, deviceSettings, cb) {
_self._request('/api/s/<SITE>/rest/device/' + device_id, deviceSettings, sites, cb, 'PUT');
};
/**
* List access points and other devices under management of the controller (USW and/or USG devices) - list_devices()
* ------------------------------------------------------------------------------------------------
*
* required paramater <sites> = name or array of site names
* optional paramater <device_mac> = the MAC address of a single device for which the call must be made
*/
_self.getAccessDevices = function (sites, cb, device_mac) {
if (typeof (device_mac) === 'undefined')
device_mac = '';
_self._request('/api/s/<SITE>/stat/device/' + device_mac.trim().toLowerCase(), null, sites, cb);
};
/**
* List rogue access points - list_rogueaps()
* ------------------------
*
* optional parameter <within> = hours to go back to list discovered "rogue" access points (default = 24 hours)
*
*/
_self.getRogueAccessPoints = function (sites, cb, within) {
if (typeof (within) === 'undefined')
within = 24;
_self._request('/api/s/<SITE>/stat/rogueap', { within: within }, sites, cb);
};
/**
* List sites
* ----------
* calls callback function(err, result) with an array of the sites
* registered to the UniFi controller
*/
_self.getSites = function (cb) {
_self._request('/api/self/sites', null, null, cb);
};
/**
* List sites stats
* ----------------
* calls callback function(err, result) with an array of sysinfo information
* for all sites registered to the UniFi controller
*
* NOTES: endpoint was introduced with controller version 5.2.9
*/
_self.getSitesStats = function (cb) {
_self._request('/api/stat/sites', null, null, cb);
};
/**
* Add a site - add_site()
* ----------
*
* required parameter <description> = the long name for the new site
*
* NOTES: immediately after being added, the new site will be available in the output of the "list_sites" function
*/
_self.addSite = function (site, cb, description) {
if (typeof (description) === 'undefined')
description = '';
var json = {
desc: description,
cmd: 'add-site'
};
_self._request('/api/s/<SITE>/cmd/sitemgr', json, site, cb);
};
/**
* Delete a site - delete_site()
* -------------
*
* required parameter <site_id> = 24 char string; _id of the site to delete
*
*/
_self.deleteSite = function (site_id, cb) {
// lets get the _id first
_self.getSites(function (err, result) {
if (!err && result && result.length > 0) {
// only if name or _id matches the site paramater
if (result[0].name === site_id || result[0]._id === site_id) {
var json = {
site: result[0]._id,
cmd: 'delete-site'
};
_self._request('/api/s/<SITE>/cmd/sitemgr', json, result[0].name, cb);
}
}
});
};
/**
* List admins - list_admins()
* -----------
*
* required paramater <sites> = name or array of site names
*
*/
_self.listAdmins = function (sites, cb) {
_self._request('/api/s/<SITE>/cmd/sitemgr', { cmd: 'get-admins' }, sites, cb);
};
/**
* List wlan_groups - list_wlan_groups()
* ----------------
*
* required paramater <sites> = name or array of site names
*/
_self.getWLanGroups = function (sites, cb) {
_self._request('/api/s/<SITE>/list/wlangroup', null, sites, cb);
};
/**
* List site sysinfo
* -----------------
* returns an array of known sysinfo data via callback function(err, result)
* for all sites specified as a function parameter
*/
_self.getSiteSysinfo = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/sysinfo', null, sites, cb);
};
/**
* List self - list_self()
* ---------
* returns an array of information about the logged in user
*/
_self.getSelf = function (sites, cb) {
_self._request('/api/s/<SITE>/self', null, sites, cb);
};
/**
* List networkconf - list_networkconf()
* ----------------
* returns an array of network configuration data
*/
_self.getNetworkConf = function (sites, cb) {
_self._request('/api/s/<SITE>/list/networkconf', null, sites, cb);
};
/**
* List vouchers - stat_voucher()
* -------------
*
* optional parameter <create_time> = Unix timestamp in seconds
*/
_self.getVouchers = function (sites, cb, create_time) {
var json = {};
if (typeof (create_time) !== 'undefined')
json = { create_time: create_time };
_self._request('/api/s/<SITE>/stat/voucher', json, sites, cb);
};
/**
* List payments - stat_payment()
* -------------
* returns an array of hotspot payments
*/
_self.getPayments = function (sites, cb, within) {
if (typeof (within) !== 'undefined')
within = '?within=' + within.trim();
else
within = '';
_self._request('/api/s/<SITE>/stat/payment' + within, null, sites, cb);
};
/**
* Create hotspot operator - create_hotspotop()
* -----------------------
*
* required parameter <name> = name for the hotspot operator
* required parameter <x_password> = clear text password for the hotspot operator
* optional parameter <note> = note to attach to the hotspot operator
*/
_self.createHotspotOperator = function (sites, name, x_password, cb, note) {
var json = {
name: name,
x_password: x_password
};
if (typeof (note) !== 'undefined')
json.note = note;
_self._request('/api/s/<SITE>/rest/hotspotop', json, sites, cb);
};
/**
* List hotspot operators - list_hotspotop()
* ----------------------
* returns an array of hotspot operators
*/
_self.getHotspotOperators = function (sites, cb) {
_self._request('/api/s/<SITE>/list/hotspotop', null, sites, cb);
};
/**
* Create voucher(s) - create_voucher()
* -----------------
* returns an array of voucher codes (without the dash "-" in the middle) by calling the stat_voucher method
*
* required parameter <minutes> = minutes the voucher is valid after activation (expiration time)
* optional parameter <count> = number of vouchers to create, default value is 1
* optional parameter <quota> = single-use or multi-use vouchers, string value '0' is for multi-use, '1' is for single-use, "n" is for multi-use n times
* optional parameter <note> = note text to add to voucher when printing
* optional parameter <up> = upload speed limit in kbps
* optional parameter <down> = download speed limit in kbps
* optional parameter <mbytes> = data transfer limit in MB
*/
_self.createVouchers = function (sites, minutes, cb, count, quota, note, up, down, mbytes) {
if (typeof (count) === 'undefined') count = 1;
if (typeof (quota) === 'undefined') quota = 0;
var json = {
cmd: 'create-voucher',
expire: minutes,
n: count,
quota: quota
};
if (typeof (note) !== 'undefined') json.note = note;
if (typeof (up) !== 'undefined') json.up = up;
if (typeof (down) !== 'undefined') json.down = down;
if (typeof (mbytes) !== 'undefined') json.bytes = mbytes;
_self._request('/api/s/<SITE>/cmd/hotspot', json, sites, cb);
};
/**
* Revoke voucher - revoke_voucher()
*---------------
* return TRUE on success
*
* required parameter <voucher_id> = 24 char string; _id of the voucher to revoke
*/
_self.revokeVoucher = function (sites, voucher_id, cb) {
var json = {
cmd: 'delete-voucher',
_id: voucher_id
};
_self._request('/api/s/<SITE>/cmd/hotspot', json, sites, cb);
};
/**
* Extend guest validity - extend_guest_validity()
* ---------------------
* return TRUE on success
*
* required parameter <guest_id> = 24 char string; _id of the guest to extend validity
*/
_self.extendGuestValidity = function (sites, guest_id, cb) {
var json = {
cmd: 'extend',
_id: guest_id
};
_self._request('/api/s/<SITE>/cmd/hotspot', json, sites, cb);
};
/**
* List port forwarding stats - list_portforward_stats()
* --------------------------
* returns an array of port forwarding stats
*/
_self.getPortForwardingStats = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/portforward', null, sites, cb);
};
/**
* List DPI stats - list_dpi_stats()
* --------------
* returns an array of DPI stats
*/
_self.getDPIStats = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/dpi', null, sites, cb);
};
/**
* clear DPI stats
* --------------
* clears stats of DPI
*/
_self.ClearDPIStatus = function (sites, cb) {
var json = {
cmd: 'clear-dpi'
};
_self._request('/api/s/<SITE>/cmd/stat', json, sites, cb);
};
/**
* List current channels - list_current_channels()
* ---------------------
* returns an array of currently allowed channels
*/
_self.getCurrentChannels = function (sites, cb) {
_self._request('/api/s/<SITE>/stat/current-channel', null, sites, cb);
};
/**
* List port forwarding settings - list_portforwarding()
* -----------------------------
* returns an array of port forwarding settings
*/
_self.getPortForwarding = function (sites, cb) {
_self._request('/api/s/<SITE>/list/portforward', null, sites, cb);
};
/**
* List dynamic DNS settings - list_dynamicdns()
* -------------------------
* returns an array of dynamic DNS settings
*/
_self.getDynamicDNS = function (sites, cb) {
_self._request('/api/s/<SITE>/list/dynamicdns', null, sites, cb);
};
/**
* List port configuration - list_portconf()
* -----------------------
* returns an array of port configurations
*/
_self.getPortConfig = function (sites, cb) {
_self._request('/api/s/<SITE>/list/portconf', null, sites, cb);
};
/**
* List VoIP extensions - list_extensions()
* --------------------
* returns an array of VoIP extensions
*/
_self.getVoipExtensions = function (sites, cb) {
_self._request('/api/s/<SITE>/list/extension', null, sites, cb);
};
/**
* List site settings - list_settings()
* ------------------
* returns an array of site configuration settings
*/
_self.getSiteSettings = function (sites, cb) {
_self._request('/api/s/<SITE>/get/setting', null, sites, cb);
};
/**
* Adopt a device to the selected site - adopt_device()
* -----------------------------------
*
* required parameter <mac> = device MAC address
*/
_self.adoptDevice = function (sites, mac, cb) {
var json = {
cmd: 'adopt',
mac: mac.toLowerCase()
};
_self._request('/api/s/<SITE>/cmd/devmgr', json, sites, cb);
};
/**
* Reboot an access point - restart_ap()
* ----------------------
*
* required parameter <mac> = device MAC address
*/
_self.rebootAccessPoint = function (sites, mac, cb) {
var json = {
cmd: 'restart',
mac: mac.toLowerCase()
};
_self._request('/api/s/<SITE>/cmd/devmgr', json, sites, cb);
};
/**
* Disable/enable an access point - disable_ap()
* ------------------------------
*
* required parameter <device_id> = 24 char string; value of _id for the access point which can be obtained from the device list
* required parameter <disable> = boolean; TRUE will disable the device, FALSE will enable the device
*
* NOTES:
* - a disabled device will be excluded from the dashboard status and device count and its LED and WLAN will be turned off
* - appears to only be supported for access points
* - available since controller versions 5.2.X
*/
_self.disableAccessPoint = function (sites, device_id, disable, cb) {
_self._request('/api/s/<SITE>/rest/device/' + device_id.trim(), { disabled: disable }, sites, cb, 'PUT');
};
/**
* Override LED mode for a device - led_override()
* ------------------------------
*
* required parameter <device_id> = 24 char string; value of _id for the device which can be obtained from the device list
* required parameter <override_mode> = string, off/on/default; "off" will disable the LED of the device,
* "on" will enable the LED of the device,
* "default" will apply the site-wide setting for device LEDs
*/
_self.setLEDOverride = function (sites, device_id, override_mode, cb,) {
_self._request('/api/s/<SITE>/rest/device/' + device_id.trim(), { led_override: override_mode }, sites, cb, 'PUT');
};
/**
* Toggle flashing LED of an access point for locating purposes - locate_ap()
* ------------------------------------------------------------
*
* required parameter <mac> = device MAC address