unifi_gem 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +151 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/unifi_gem/client/guests.rb +58 -0
- data/lib/unifi_gem/client/main.rb +338 -0
- data/lib/unifi_gem/client/sites.rb +38 -0
- data/lib/unifi_gem/client/vouchers.rb +38 -0
- data/lib/unifi_gem/client/wlan.rb +68 -0
- data/lib/unifi_gem/client.rb +58 -0
- data/lib/unifi_gem/version.rb +3 -0
- data/lib/unifi_gem.rb +7 -0
- data/unifi_gem.gemspec +29 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 835b4724b0957e69c93920dded5c872c0a53a5519aaadf67ca2494b3c6d759f1
|
4
|
+
data.tar.gz: 6d002c3694118ebb92613563097a923f27a30b23c4c40ad9b44a7ed299f2e425
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97f8bbd6bc7c413276e8cf54d0c22bf26df15458d414a5e375fb3300b239b84c15cbe2570aa580a3a3f69c5954650f97b32119663cc27b1612edf0b1664fc35c
|
7
|
+
data.tar.gz: ac12e0800a37e5368f7c5d76ff845010f0e60d1840037c12d5f88bca4d80afce18db4ae59e96a1bc42602420157c40b8bbecba155283257f12b33301a86789bf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Sergey Korolev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# Unifi
|
2
|
+
A Ruby interface to UniFi Controller API. Supported versions 5.x.x.
|
3
|
+
Reimplemented from [Unifi Api Client](https://github.com/Art-of-WiFi/UniFi-API-client) written in php.
|
4
|
+
|
5
|
+
Follow the fork tree for the original:
|
6
|
+
* https://github.com/kirpen/unifi
|
7
|
+
* https://github.com/zumkorn/unifi
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'unifi'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install unifi
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Unifi gem needs url, site, login, password:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
client = Unifi::Client.new({url:"demo.ubnt.com",
|
31
|
+
site: "default"
|
32
|
+
username:"superadmin",
|
33
|
+
password:"superadmin"})
|
34
|
+
client.list_health
|
35
|
+
```
|
36
|
+
It will return something like that:
|
37
|
+
```
|
38
|
+
{"data"=>[{"num_adopted"=>118, "num_ap"=>118, "num_disabled"=>0,
|
39
|
+
"num_disconnected"=>0, "num_guest"=>0, "num_pending"=>0,
|
40
|
+
"num_user"=>1180, "rx_bytes-r"=>504, "status"=>"ok",
|
41
|
+
"subsystem"=>"wlan", "tx_bytes-r"=>535}...}
|
42
|
+
```
|
43
|
+
|
44
|
+
Create new voucher code:
|
45
|
+
```ruby
|
46
|
+
client.create_voucher({expire: 360, note: 'test-voucher'})
|
47
|
+
```
|
48
|
+
It will return something like that:
|
49
|
+
```
|
50
|
+
{"data"=>[{"create_time"=>1500804202}], "meta"=>{"rc"=>"ok"}}
|
51
|
+
```
|
52
|
+
Get voucher code, need create_time in second:
|
53
|
+
```ruby
|
54
|
+
client.stat_voucher(1500804202)
|
55
|
+
```
|
56
|
+
It will return something like that:
|
57
|
+
```
|
58
|
+
{"data"=>[{"_id"=>"5974746a619469d63475866a",
|
59
|
+
"admin_name"=>"sergey", "code"=>"9224743381", ...}],
|
60
|
+
"meta"=>{"rc"=>"ok"}}
|
61
|
+
```
|
62
|
+
## Methods
|
63
|
+
|
64
|
+
* login
|
65
|
+
* logout
|
66
|
+
* add_site
|
67
|
+
* adopt_device
|
68
|
+
* authorize_guest
|
69
|
+
* unauthorize_guest
|
70
|
+
* block_sta
|
71
|
+
* unblock_sta
|
72
|
+
* create_hotspotop
|
73
|
+
* create_voucher
|
74
|
+
* delete_site
|
75
|
+
* disable_ap
|
76
|
+
* led_override
|
77
|
+
* list_admins
|
78
|
+
* list_alarms
|
79
|
+
* count_alarms
|
80
|
+
* list_devices
|
81
|
+
* list_clients
|
82
|
+
* list_dashboard
|
83
|
+
* list_dynamicdns
|
84
|
+
* list_events
|
85
|
+
* list_extension
|
86
|
+
* list_guests
|
87
|
+
* list_health
|
88
|
+
* list_hotspotop
|
89
|
+
* list_networkconf
|
90
|
+
* list_portconf
|
91
|
+
* list_portforward_stats
|
92
|
+
* list_portforwarding
|
93
|
+
* list_rogueaps
|
94
|
+
* list_self
|
95
|
+
* list_settings
|
96
|
+
* list_sites
|
97
|
+
* list_usergroups
|
98
|
+
* list_users
|
99
|
+
* list_wlan_groups
|
100
|
+
* list_wlanconf
|
101
|
+
* list_current_channels
|
102
|
+
* list_dpi_stats
|
103
|
+
* reconnect_sta
|
104
|
+
* rename_ap
|
105
|
+
* restart_ap
|
106
|
+
* revoke_voucher
|
107
|
+
* extend_guest_validity
|
108
|
+
* set_ap_radiosettings
|
109
|
+
* set_guestlogin_settings
|
110
|
+
* set_sta_name
|
111
|
+
* set_sta_note
|
112
|
+
* set_usergroup
|
113
|
+
* edit_usergroup
|
114
|
+
* add_usergroup
|
115
|
+
* delete_usergroup
|
116
|
+
* set_wlansettings
|
117
|
+
* create_wlan
|
118
|
+
* delete_wlan
|
119
|
+
* site_leds
|
120
|
+
* upgrade_device
|
121
|
+
* upgrade_device_external
|
122
|
+
* spectrum_scan
|
123
|
+
* spectrum_scan_state
|
124
|
+
* stat_allusers
|
125
|
+
* stat_auths
|
126
|
+
* stat_client
|
127
|
+
* stat_daily_site
|
128
|
+
* stat_daily_aps
|
129
|
+
* stat_hourly_aps
|
130
|
+
* stat_hourly_site
|
131
|
+
* stat_payment
|
132
|
+
* stat_sessions
|
133
|
+
* stat_sites
|
134
|
+
* stat_sta_sessions_latest
|
135
|
+
* stat_sysinfo
|
136
|
+
* stat_voucher
|
137
|
+
|
138
|
+
You can see the documentation for the methods [here](https://github.com/Art-of-WiFi/UniFi-API-client/blob/master/src/Client.php).
|
139
|
+
|
140
|
+
## Useful links
|
141
|
+
|
142
|
+
* [Original Unifi API Browser](https://github.com/Art-of-WiFi/UniFi-API-browser)
|
143
|
+
* [API as published by Ubiquiti](https://www.ubnt.com/downloads/unifi/5.4.14/unifi_sh_api)
|
144
|
+
|
145
|
+
## Contributing
|
146
|
+
|
147
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davisonja/unifi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
148
|
+
|
149
|
+
## License
|
150
|
+
|
151
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "unifi"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# ./lib/unifi/client/guests.rb
|
2
|
+
module Unifi
|
3
|
+
class Client
|
4
|
+
|
5
|
+
module Guests
|
6
|
+
|
7
|
+
def list_guests(within = 8760)
|
8
|
+
body = { within: within }
|
9
|
+
response = self.class.get("/s/#{@site}/stat/guest", { body: body.to_json })
|
10
|
+
response.parsed_response
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_guestlogin_settings(portal_enabled,
|
14
|
+
portal_customized,
|
15
|
+
redirect_enabled,
|
16
|
+
redirect_url,
|
17
|
+
x_password,
|
18
|
+
expire_number,
|
19
|
+
expire_unit,
|
20
|
+
site_id)
|
21
|
+
body = { portal_enabled: portal_enabled,
|
22
|
+
portal_customized: portal_customized,
|
23
|
+
redirect_enabled: redirect_enabled,
|
24
|
+
redirect_url: redirect_url,
|
25
|
+
x_password: x_password,
|
26
|
+
expire_number: expire_number,
|
27
|
+
expire_unit: expire_unit,
|
28
|
+
site_id: site_id}
|
29
|
+
response = self.class.get("/s/#{@site}/set/setting/guest_access", { body: body.to_json })
|
30
|
+
response.parsed_response
|
31
|
+
end
|
32
|
+
|
33
|
+
def extend_guest_validity(guest_id)
|
34
|
+
body = { id: guest_id, cmd: 'extend' }
|
35
|
+
response = self.class.get("/s/#{@site}/cmd/hotspot", { body: body.to_json })
|
36
|
+
response.parsed_response
|
37
|
+
end
|
38
|
+
|
39
|
+
def unauthorize_guest(mac)
|
40
|
+
body = { cmd: 'unauthorize-guest', mac: mac.downcase }
|
41
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
42
|
+
response.parsed_response
|
43
|
+
end
|
44
|
+
|
45
|
+
def authorize_guest(mac, minutes, up = nil, down = nil, mbytes = nil, ap_mac = nil)
|
46
|
+
body = { cmd: 'authorize-guest', mac: mac.downcase, minutes: minutes }
|
47
|
+
body[:up] = up if up
|
48
|
+
body[:down] = down if down
|
49
|
+
body[:bytes] = mbytes if mbytes
|
50
|
+
body[:ap_mac] = ap_mac if ap_mac
|
51
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
52
|
+
response.parsed_response
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,338 @@
|
|
1
|
+
# ./lib/unifi/client/main.rb
|
2
|
+
module Unifi
|
3
|
+
class Client
|
4
|
+
|
5
|
+
module Main
|
6
|
+
|
7
|
+
def list_dashboard
|
8
|
+
response = self.class.get("/s/#{@site}/stat/dashboard")
|
9
|
+
response.parsed_response
|
10
|
+
end
|
11
|
+
|
12
|
+
def list_clients(mac = '')
|
13
|
+
response = self.class.get("/s/#{@site}/stat/sta/#{mac.delete(" :")}")
|
14
|
+
response.parsed_response
|
15
|
+
end
|
16
|
+
|
17
|
+
def stat_client(mac = '')
|
18
|
+
response = self.class.get("/s/#{@site}/stat/user/#{mac.delete(" :")}")
|
19
|
+
response.parsed_response
|
20
|
+
end
|
21
|
+
|
22
|
+
def list_devices(mac = '')
|
23
|
+
response = self.class.get("/s/#{@site}/stat/device/#{mac.delete(" :")}")
|
24
|
+
response.parsed_response
|
25
|
+
end
|
26
|
+
|
27
|
+
def list_dynamicdns
|
28
|
+
response = self.class.get("/s/#{@site}/list/dynamicdns")
|
29
|
+
response.parsed_response
|
30
|
+
end
|
31
|
+
|
32
|
+
def list_events(options = {})
|
33
|
+
body = { _sort: '-time', }
|
34
|
+
body[:within] = options[:historyhours] || 720
|
35
|
+
body[:_start] = options[:start] || 0
|
36
|
+
body[:_limit] = options[:limit] || 3000
|
37
|
+
response = self.class.get("/s/#{@site}/stat/event", { body: body.to_json })
|
38
|
+
response.parsed_response
|
39
|
+
end
|
40
|
+
|
41
|
+
def list_extension
|
42
|
+
response = self.class.get("/s/#{@site}/list/extension")
|
43
|
+
response.parsed_response
|
44
|
+
end
|
45
|
+
|
46
|
+
def list_health
|
47
|
+
response = self.class.get("/s/#{@site}/stat/health")
|
48
|
+
response.parsed_response
|
49
|
+
end
|
50
|
+
|
51
|
+
def list_hotspotop
|
52
|
+
response = self.class.get("/s/#{@site}/list/hotspotop")
|
53
|
+
response.parsed_response
|
54
|
+
end
|
55
|
+
|
56
|
+
def list_networkconf
|
57
|
+
response = self.class.get("/s/#{@site}/list/networkconf")
|
58
|
+
response.parsed_response
|
59
|
+
end
|
60
|
+
|
61
|
+
def list_portconf
|
62
|
+
response = self.class.get("/s/#{@site}/list/portconf")
|
63
|
+
response.parsed_response
|
64
|
+
end
|
65
|
+
|
66
|
+
def list_portforward_stats
|
67
|
+
response = self.class.get("/s/#{@site}/stat/portforward")
|
68
|
+
response.parsed_response
|
69
|
+
end
|
70
|
+
|
71
|
+
def list_rogueaps(within = 24)
|
72
|
+
body = { within: within }
|
73
|
+
response = self.class.get("/s/#{@site}/stat/rogueap", { body: body.to_json })
|
74
|
+
response.parsed_response
|
75
|
+
end
|
76
|
+
|
77
|
+
def list_self
|
78
|
+
response = self.class.get("/s/#{@site}/self")
|
79
|
+
response.parsed_response
|
80
|
+
end
|
81
|
+
|
82
|
+
def list_settings
|
83
|
+
response = self.class.get("/s/#{@site}/get/setting")
|
84
|
+
response.parsed_response
|
85
|
+
end
|
86
|
+
|
87
|
+
def list_usergroups
|
88
|
+
response = self.class.get("/s/#{@site}/list/usergroup")
|
89
|
+
response.parsed_response
|
90
|
+
end
|
91
|
+
|
92
|
+
def list_users
|
93
|
+
response = self.class.get("/s/#{@site}/list/user")
|
94
|
+
response.parsed_response
|
95
|
+
end
|
96
|
+
|
97
|
+
def list_current_channels
|
98
|
+
response = self.class.get("/s/#{@site}/stat/current-channel")
|
99
|
+
response.parsed_response
|
100
|
+
end
|
101
|
+
|
102
|
+
def list_dpi_stats
|
103
|
+
response = self.class.get("/s/#{@site}/stat/dpi")
|
104
|
+
response.parsed_response
|
105
|
+
end
|
106
|
+
|
107
|
+
def reconnect_sta(mac)
|
108
|
+
body = { cmd: 'kick-sta', mac: mac.downcase }
|
109
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
110
|
+
response.parsed_response
|
111
|
+
end
|
112
|
+
|
113
|
+
def rename_ap(ap_id, apname)
|
114
|
+
body = { name: apname }
|
115
|
+
response = self.class.get("/s/#{@site}/upd/device/#{ap_id.delete(" ")}", { body: body.to_json })
|
116
|
+
response.parsed_response
|
117
|
+
end
|
118
|
+
|
119
|
+
def restart_ap(mac)
|
120
|
+
body = { cmd: 'restart', mac: mac.downcase }
|
121
|
+
response = self.class.get("/s/#{@site}/cmd/devmgr", { body: body.to_json })
|
122
|
+
response.parsed_response
|
123
|
+
end
|
124
|
+
|
125
|
+
def stat_sta_sessions_latest(mac, limit = 5)
|
126
|
+
body = { mac: mac.downcase, _limit: limit, _sort: '-assoc_time' }
|
127
|
+
response = self.class.get("/s/#{@site}/stat/session", { body: body.to_json })
|
128
|
+
response.parsed_response
|
129
|
+
end
|
130
|
+
|
131
|
+
def stat_sessions(start_time = nil, end_time = Time.now.to_i, mac = nil)
|
132
|
+
body = { start: start_time ? start_time : end_time - (7 * 24 * 3600), end: end_time, type: 'all' }
|
133
|
+
body[:mac] = mac if mac
|
134
|
+
response = self.class.get("/s/#{@site}/stat/session", { body: body.to_json })
|
135
|
+
response.parsed_response
|
136
|
+
end
|
137
|
+
|
138
|
+
def stat_payment(within = nil)
|
139
|
+
response = self.class.get("/s/#{@site}/stat/payment#{within ? "?within=#{within}" : '' }")
|
140
|
+
response.parsed_response
|
141
|
+
end
|
142
|
+
|
143
|
+
def stat_hourly_site(start_time = nil, end_time = Time.now.to_i * 1000)
|
144
|
+
body = { start: start_time ? start_time : end_time - (7 * 24 * 3600 * 1000),
|
145
|
+
end: end_time,
|
146
|
+
attributes: ['bytes',
|
147
|
+
'wan-tx_bytes',
|
148
|
+
'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'] }
|
149
|
+
response = self.class.get("/s/#{@site}/stat/report/hourly.site", { body: body.to_json })
|
150
|
+
response.parsed_response
|
151
|
+
end
|
152
|
+
|
153
|
+
def stat_hourly_aps(start_time = nil, end_time = Time.now.to_i * 1000)
|
154
|
+
body = { start: start_time ? start_time : end_time - (7 * 24 * 3600 * 1000),
|
155
|
+
end: end_time,
|
156
|
+
attrs: ['bytes', 'num_sta', 'time'] }
|
157
|
+
response = self.class.get("/s/#{@site}/stat/report/hourly.ap", { body: body.to_json })
|
158
|
+
response.parsed_response
|
159
|
+
end
|
160
|
+
|
161
|
+
def stat_daily_aps(start_time = nil, end_time = Time.now.to_i * 1000)
|
162
|
+
body = { start: start_time ? start_time : end_time - (7 * 24 * 3600 * 1000),
|
163
|
+
end: end_time,
|
164
|
+
attrs: ['bytes', 'num_sta', 'time'] }
|
165
|
+
response = self.class.get("/s/#{@site}/stat/report/daily.ap", { body: body.to_json })
|
166
|
+
response.parsed_response
|
167
|
+
end
|
168
|
+
|
169
|
+
def stat_daily_site(start_time = nil, end_time = Time.now.to_i - (Time.now.to_i % 3600) * 1000)
|
170
|
+
body = { start: start_time ? start_time : end_time - (52 * 7 * 24 * 3600 * 1000),
|
171
|
+
end: end_time,
|
172
|
+
attributes: ['bytes',
|
173
|
+
'wan-tx_bytes',
|
174
|
+
'wan-rx_bytes',
|
175
|
+
'wlan_bytes',
|
176
|
+
'num_sta',
|
177
|
+
'lan-num_sta',
|
178
|
+
'wlan-num_sta',
|
179
|
+
'time'] }
|
180
|
+
response = self.class.get("/s/#{@site}/stat/report/daily.site", { body: body.to_json })
|
181
|
+
response.parsed_response
|
182
|
+
end
|
183
|
+
|
184
|
+
def stat_auths(start_time = nil, end_time = Time.now.to_i)
|
185
|
+
body = { start: start_time ? start_time : end_time - (7 * 24 * 3600),
|
186
|
+
end: end_time }
|
187
|
+
response = self.class.get("/s/#{@site}/stat/authorization", { body: body.to_json })
|
188
|
+
response.parsed_response
|
189
|
+
end
|
190
|
+
|
191
|
+
def stat_allusers(historyhours = 8760)
|
192
|
+
body = { type: 'all', conn: 'all', within: historyhours}
|
193
|
+
response = self.class.get("/s/#{@site}/stat/alluser", { body: body.to_json })
|
194
|
+
response.parsed_response
|
195
|
+
end
|
196
|
+
|
197
|
+
def spectrum_scan_state(mac)
|
198
|
+
response = self.class.get("/s/#{@site}/stat/spectrum-scan/#{mac.delete(': ')}")
|
199
|
+
response.parsed_response
|
200
|
+
end
|
201
|
+
|
202
|
+
def spectrum_scan(mac)
|
203
|
+
body = { cmd: 'spectrum-scan', mac: mac }
|
204
|
+
response = self.class.post("/s/#{@site}/cmd/devmgr", { body: body.to_json })
|
205
|
+
response.parsed_response
|
206
|
+
end
|
207
|
+
|
208
|
+
def upgrade_device_external(firmware_url, device_mac)
|
209
|
+
body = { url: firmware_url, mac: device_mac }
|
210
|
+
response = self.class.get("/s/#{@site}/cmd/devmgr/upgrade-external", { body: body.to_json })
|
211
|
+
response.parsed_response
|
212
|
+
end
|
213
|
+
|
214
|
+
def upgrade_device(mac)
|
215
|
+
body = { mac: mac }
|
216
|
+
response = self.class.get("/s/#{@site}/cmd/devmgr/upgrade", { body: body.to_json })
|
217
|
+
response.parsed_response
|
218
|
+
end
|
219
|
+
|
220
|
+
def site_leds(enable)
|
221
|
+
body = { led_enabled: enable }
|
222
|
+
response = self.class.post("/s/#{@site}/set/setting/mgmt", { body: body.to_json })
|
223
|
+
response.parsed_response
|
224
|
+
end
|
225
|
+
|
226
|
+
def delete_usergroup(group_id)
|
227
|
+
response = self.class.delete("/s/#{@site}/rest/usergroup/#{group_id}")
|
228
|
+
response.parsed_response
|
229
|
+
end
|
230
|
+
|
231
|
+
def add_usergroup(group_name, group_dn = -1, group_up = -1)
|
232
|
+
body = { name: group_name, qos_rate_max_down: group_dn, qos_rate_max_up: group_up }
|
233
|
+
response = self.class.get("/s/#{@site}/rest/usergroup", { body: body.to_json })
|
234
|
+
response.parsed_response
|
235
|
+
end
|
236
|
+
|
237
|
+
def edit_usergroup(group_id, site_id, group_name, group_dn = -1, group_up = -1)
|
238
|
+
body = { _id: group_id,
|
239
|
+
name: group_name,
|
240
|
+
qos_rate_max_down: group_dn,
|
241
|
+
qos_rate_max_up: group_up,
|
242
|
+
site_id: site_id }
|
243
|
+
response = self.class.put("/s/#{@site}/rest/usergroup/#{group_id}", { body: body.to_json })
|
244
|
+
response.parsed_response
|
245
|
+
end
|
246
|
+
|
247
|
+
def set_usergroup(user_id, group_id)
|
248
|
+
body = { usergroup_id: group_id }
|
249
|
+
response = self.class.get("/s/#{@site}/upd/user/#{user_id}", { body: body.to_json })
|
250
|
+
response.parsed_response
|
251
|
+
end
|
252
|
+
|
253
|
+
def set_sta_note(user_id, note = nil)
|
254
|
+
body = { noted: note ? true : false }
|
255
|
+
body[:note] = note if note
|
256
|
+
response = self.class.get("/s/#{@site}/upd/user/#{user_id}", { body: body.to_json })
|
257
|
+
response.parsed_response
|
258
|
+
end
|
259
|
+
|
260
|
+
def set_sta_name(user_id, name = '')
|
261
|
+
body = { name: name }
|
262
|
+
response = self.class.get("/s/#{@site}/upd/user/#{user_id}", { body: body.to_json })
|
263
|
+
response.parsed_response
|
264
|
+
end
|
265
|
+
|
266
|
+
def locate_ap(mac, enable)
|
267
|
+
body = { cmd: enable ? 'set-locate' : 'unset-locate', mac: mac.downcase }
|
268
|
+
response = self.class.get("/s/#{@site}/cmd/devmgr", { body: body.to_json })
|
269
|
+
response.parsed_response
|
270
|
+
end
|
271
|
+
|
272
|
+
def set_ap_radiosettings(ap_id, radio, channel, ht, tx_power_mode, tx_power)
|
273
|
+
body = { radio_table: { radio: radio,
|
274
|
+
channel: channel,
|
275
|
+
ht: ht,
|
276
|
+
tx_power_mode: tx_power_mode,
|
277
|
+
tx_power: tx_power } }
|
278
|
+
end
|
279
|
+
|
280
|
+
def count_alarms(archived)
|
281
|
+
response = self.class.get("/s/#{@site}/cnt/alarm#{archived ? '' : '?archived=false'}")
|
282
|
+
response.parsed_response
|
283
|
+
end
|
284
|
+
|
285
|
+
def list_alarms
|
286
|
+
response = self.class.get("/s/#{@site}/list/alarm")
|
287
|
+
response.parsed_response
|
288
|
+
end
|
289
|
+
|
290
|
+
def list_admins
|
291
|
+
body = { cmd: 'get-admins' }
|
292
|
+
response = self.class.post("/s/#{@site}/cmd/sitemgr", { body: body.to_json })
|
293
|
+
response.parsed_response
|
294
|
+
end
|
295
|
+
|
296
|
+
def led_override(device_id, override_mode)
|
297
|
+
body = { led_override: override_mode }
|
298
|
+
if ['off', 'on', 'default'].include?(override_mode)
|
299
|
+
response = self.class.put("/s/#{@site}/rest/device/#{device_id}", { body: body.to_json })
|
300
|
+
response.parsed_response
|
301
|
+
else
|
302
|
+
false
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
def disable_ap(ap_id, disable)
|
307
|
+
body = { disabled: disable }
|
308
|
+
response = self.class.put("/s/#{@site}/rest/device/#{ap_id}", { body: body.to_json })
|
309
|
+
response.parsed_response
|
310
|
+
end
|
311
|
+
|
312
|
+
def create_hotspotop(name, x_password, note = '')
|
313
|
+
body = { name: name, x_password: x_password }
|
314
|
+
body[:note] = note if note
|
315
|
+
response = self.class.post("/s/#{@site}/rest/hotspotop", { body: body.to_json })
|
316
|
+
response.parsed_response
|
317
|
+
end
|
318
|
+
|
319
|
+
def unblock_sta(mac)
|
320
|
+
body = { cmd: 'unblock-sta', mac: mac.downcase }
|
321
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
322
|
+
response.parsed_response
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def block_sta(mac)
|
327
|
+
body = { cmd: 'block-sta', mac: mac.downcase }
|
328
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
329
|
+
response.parsed_response
|
330
|
+
end
|
331
|
+
|
332
|
+
def adopt_device(mac)
|
333
|
+
body = { cmd: 'adopt', mac: mac.downcase }
|
334
|
+
response = self.class.post("/s/#{@site}/cmd/stamgr", { body: body.to_json })
|
335
|
+
response.parsed_response
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# ./lib/unifi/client/sites.rb
|
2
|
+
module Unifi
|
3
|
+
class Client
|
4
|
+
|
5
|
+
module Sites
|
6
|
+
|
7
|
+
def add_site(description)
|
8
|
+
body = { cmd: 'add-site', desc: description }
|
9
|
+
response = self.class.post("/s/#{@site}/cmd/sitemgr", { body: body.to_json })
|
10
|
+
response.parsed_response
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def delete_site(site_id)
|
15
|
+
body = { site: site_id, cmd: 'delete-site' }
|
16
|
+
response = self.class.post("/s/#{@site}/cmd/sitemgr", { body: body.to_json })
|
17
|
+
response.parsed_response
|
18
|
+
end
|
19
|
+
|
20
|
+
def list_sites
|
21
|
+
response = self.class.get("/self/sites")
|
22
|
+
response.parsed_response
|
23
|
+
end
|
24
|
+
|
25
|
+
def stat_sites
|
26
|
+
response = self.class.get("/stat/sites")
|
27
|
+
response.parsed_response
|
28
|
+
end
|
29
|
+
|
30
|
+
def stat_sysinfo
|
31
|
+
response = self.class.get("/s/#{@site}/stat/sysinfo")
|
32
|
+
response.parsed_response
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# ./lib/unifi/client/vouchers.rb
|
2
|
+
module Unifi
|
3
|
+
class Client
|
4
|
+
|
5
|
+
module Vouchers
|
6
|
+
|
7
|
+
def create_voucher(options = {})
|
8
|
+
body = { cmd: 'create-voucher',
|
9
|
+
expire: options[:expire] ||= 120,
|
10
|
+
n: options[:amount] ||= 1,
|
11
|
+
quota: options[:quota] ||= 1 }
|
12
|
+
body[:note] = options[:note] if options[:note]
|
13
|
+
body[:up] = options[:up] if options[:up]
|
14
|
+
body[:down] = options[:down] if options[:down]
|
15
|
+
body[:bytes] = options[:bytes] if options[:bytes]
|
16
|
+
response = self.class.post("/s/#{@site}/cmd/hotspot",
|
17
|
+
{ body: body.to_json } )
|
18
|
+
response.parsed_response
|
19
|
+
end
|
20
|
+
|
21
|
+
def stat_voucher(create_time = nill)
|
22
|
+
body = { create_time: create_time }
|
23
|
+
response = self.class.get("/s/#{@site}/stat/voucher",
|
24
|
+
{ body: body.to_json })
|
25
|
+
response.parsed_response
|
26
|
+
end
|
27
|
+
|
28
|
+
def revoke_voucher(voucher_id = nill)
|
29
|
+
body = { cmd: 'delete-voucher', _id: voucher_id }
|
30
|
+
response = self.class.post("/s/#{@site}/cmd/hotspot",
|
31
|
+
{ body: body.to_json } )
|
32
|
+
response.parsed_response
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# ./lib/unifi/client/wlan.rb
|
2
|
+
module Unifi
|
3
|
+
class Client
|
4
|
+
|
5
|
+
module Wlan
|
6
|
+
|
7
|
+
def delete_wlan(wlan_id)
|
8
|
+
body = {}
|
9
|
+
response = self.class.post("/s/#{@site}/del/wlanconf/#{wlan_id}", { body: body.to_json })
|
10
|
+
response.parsed_response
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_wlan(name,
|
14
|
+
x_passphrase,
|
15
|
+
usergroup_id,
|
16
|
+
wlangroup_id,
|
17
|
+
enabled = true,
|
18
|
+
hide_ssid = false,
|
19
|
+
is_guest = false,
|
20
|
+
security = 'open',
|
21
|
+
wpa_mode = 'wpa2',
|
22
|
+
wpa_enc = 'ccmp',
|
23
|
+
vlan_enabled = false,
|
24
|
+
vlan = null,
|
25
|
+
uapsd_enabled = false,
|
26
|
+
schedule_enabled = false,
|
27
|
+
schedule = [])
|
28
|
+
body = { name: name,
|
29
|
+
x_passphrase: x_passphrase,
|
30
|
+
usergroup_id: usergroup_id,
|
31
|
+
wlangroup_id: wlangroup_id,
|
32
|
+
enabled: enabled,
|
33
|
+
hide_ssid: hide_ssid,
|
34
|
+
is_guest: is_guest,
|
35
|
+
security: security,
|
36
|
+
wpa_mode: wpa_mode,
|
37
|
+
wpa_enc: wpa_enc,
|
38
|
+
vlan_enabled: vlan_enabled,
|
39
|
+
uapsd_enabled: uapsd_enabled,
|
40
|
+
schedule_enabled: schedule_enabled,
|
41
|
+
schedule: schedule }
|
42
|
+
body[:vlan] = vlan if vlan && vlan_enabled
|
43
|
+
response = self.class.get("/s/#{@site}/add/wlanconf", { body: body.to_json })
|
44
|
+
response.parsed_response
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_wlansettings(wlan_id, x_passphrase, name = '')
|
48
|
+
body = {}
|
49
|
+
body[:x_passphrase] = x_passphrase if x_passphrase
|
50
|
+
body[:name] = name if name
|
51
|
+
response = self.class.get("/s/#{@site}/upd/wlanconf/#{wlan_id}", { body: body.to_json })
|
52
|
+
response.parsed_response
|
53
|
+
end
|
54
|
+
|
55
|
+
def list_wlan_groups
|
56
|
+
response = self.class.get("/s/#{@site}/list/wlangroup")
|
57
|
+
response.parsed_response
|
58
|
+
end
|
59
|
+
|
60
|
+
def list_wlanconf
|
61
|
+
response = self.class.get("/s/#{@site}/list/wlanconf")
|
62
|
+
response.parsed_response
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# ./lib/unifi-gem/client.rb
|
2
|
+
require "unifi-gem/client/vouchers"
|
3
|
+
require "unifi-gem/client/sites"
|
4
|
+
require "unifi-gem/client/guests"
|
5
|
+
require "unifi-gem/client/wlan"
|
6
|
+
require "unifi-gem/client/main"
|
7
|
+
require 'logger'
|
8
|
+
module Unifi
|
9
|
+
|
10
|
+
class Client
|
11
|
+
include HTTParty
|
12
|
+
include Unifi::Client::Vouchers
|
13
|
+
include Unifi::Client::Sites
|
14
|
+
include Unifi::Client::Guests
|
15
|
+
include Unifi::Client::Wlan
|
16
|
+
include Unifi::Client::Main
|
17
|
+
|
18
|
+
# logger ::Logger.new(STDOUT), :debug, :curl
|
19
|
+
|
20
|
+
format :json
|
21
|
+
def initialize(options = {})
|
22
|
+
# @logger = ::Logger.new STDOUT, :debug, :curl
|
23
|
+
options[:url] ||= ENV["UNIFI_URL"]
|
24
|
+
self.class.base_uri "https://#{options[:url]}/api"
|
25
|
+
@selfbaseuri = "https://#{options[:url]}"
|
26
|
+
@site = options[:site] || ENV["UNIFI_SITE"] || "default"
|
27
|
+
@username = options[:username] || ENV["UNIFI_USERNAME"]
|
28
|
+
@password = options[:password] || ENV["UNIFI_PASSWORD"]
|
29
|
+
self.class.default_options.merge!(headers: { 'Content-Type'=>'application/json',
|
30
|
+
'Accept'=>'application/json' },
|
31
|
+
verify: false)
|
32
|
+
login
|
33
|
+
self.class.default_options.merge!(headers: { 'Cookie'=>@cookies } )
|
34
|
+
end
|
35
|
+
|
36
|
+
def login
|
37
|
+
options = {
|
38
|
+
headers: {
|
39
|
+
Referer: "#{@selfbaseuri}/login"
|
40
|
+
},
|
41
|
+
body: JSON.generate({
|
42
|
+
username: "#{@username}",
|
43
|
+
password: "#{@password}"
|
44
|
+
})
|
45
|
+
}
|
46
|
+
response = self.class.post("/login", options)
|
47
|
+
puts response
|
48
|
+
@cookies = response.headers['set-cookie']
|
49
|
+
end
|
50
|
+
|
51
|
+
def logout
|
52
|
+
self.class.get("/logout")
|
53
|
+
@cookies = ''
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/lib/unifi_gem.rb
ADDED
data/unifi_gem.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unifi_gem/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unifi_gem"
|
8
|
+
spec.version = Unifi::VERSION
|
9
|
+
spec.date = '2019-02-26'
|
10
|
+
spec.authors = ["Julian Davison"]
|
11
|
+
spec.email = ["julian@davison.org.nz"]
|
12
|
+
|
13
|
+
spec.summary = %q{A Ruby interface to UniFi Controller API}
|
14
|
+
spec.description = %q{A Ruby interface to UniFi Controller API.}
|
15
|
+
spec.homepage = "https://github.com/davisonja/unifi-gem"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_runtime_dependency "httparty", "~> 0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unifi_gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julian Davison
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A Ruby interface to UniFi Controller API.
|
56
|
+
email:
|
57
|
+
- julian@davison.org.nz
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/unifi_gem.rb
|
70
|
+
- lib/unifi_gem/client.rb
|
71
|
+
- lib/unifi_gem/client/guests.rb
|
72
|
+
- lib/unifi_gem/client/main.rb
|
73
|
+
- lib/unifi_gem/client/sites.rb
|
74
|
+
- lib/unifi_gem/client/vouchers.rb
|
75
|
+
- lib/unifi_gem/client/wlan.rb
|
76
|
+
- lib/unifi_gem/version.rb
|
77
|
+
- unifi_gem.gemspec
|
78
|
+
homepage: https://github.com/davisonja/unifi-gem
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.7.6
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: A Ruby interface to UniFi Controller API
|
102
|
+
test_files: []
|