unifi 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf4bdf269f4a9d5e5cbce2c0f70b3e0c6c09f86c
4
+ data.tar.gz: ab357debaf347a9fdf4a7f689496b099679c1d4a
5
+ SHA512:
6
+ metadata.gz: 105bd6ab160bb386152e0bc254099a9b7e7a36a9dc391b0fe1cc6cb8b64b2ab5fc66cd7552de051b49c93c21cc46304ec179a2dc591b12aa7c9ac57890827366
7
+ data.tar.gz: 52e22133a360b5f64b1712faf8c086f2c701e82f8709e0173d57729cb4c144bc5e81da7296385230ca974de80d85e4783429d40b7a3376c6994852adc8f6a986
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea
14
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unifi.gemspec
4
+ gemspec
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,147 @@
1
+ # Unifi
2
+ A Ruby interface to UniFi Controller API. Supported versions 5.x.x.
3
+ It's fork [Unifi Api Browser](https://github.com/malle-pietje/UniFi-API-browser/blob/master/phpapi/class.unifi.php) written in php.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'unifi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install unifi
20
+
21
+ ## Usage
22
+
23
+ Unifi gem needs url, site, login, password:
24
+
25
+ ```ruby
26
+ client = Unifi::Client.new({url:"demo.ubnt.com",
27
+ site: "default"
28
+ username:"superadmin",
29
+ password:"superadmin"})
30
+ client.list_health
31
+ ```
32
+ It will return something like that:
33
+ ```json
34
+ {"data"=>[{"num_adopted"=>118, "num_ap"=>118, "num_disabled"=>0,
35
+ "num_disconnected"=>0, "num_guest"=>0, "num_pending"=>0,
36
+ "num_user"=>1180, "rx_bytes-r"=>504, "status"=>"ok",
37
+ "subsystem"=>"wlan", "tx_bytes-r"=>535}...}
38
+ ```
39
+
40
+ Create new voucher code:
41
+ ```ruby
42
+ client.create_voucher({expire: 360, note: 'test-voucher'})
43
+ ```
44
+ Get voucher code, need create_time in seconds:
45
+ ```json
46
+ {"data"=>[{"create_time"=>1500804202}], "meta"=>{"rc"=>"ok"}}
47
+ ```
48
+ Get voucher code, need create_time in second:
49
+ ```ruby
50
+ client.stat_voucher(1500804202)
51
+ ```
52
+ It will return something like that:
53
+ ```json
54
+ {"data"=>[{"_id"=>"5974746a619469d63475866a",
55
+ "admin_name"=>"sergey", "code"=>"9224743381", ...}],
56
+ "meta"=>{"rc"=>"ok"}}
57
+ ```
58
+ ## Methods
59
+
60
+ * login
61
+ * logout
62
+ * add_site
63
+ * adopt_device
64
+ * authorize_guest
65
+ * unauthorize_guest
66
+ * block_sta
67
+ * unblock_sta
68
+ * create_hotspotop
69
+ * create_voucher
70
+ * delete_site
71
+ * disable_ap
72
+ * led_override
73
+ * list_admins
74
+ * list_alarms
75
+ * count_alarms
76
+ * list_devices
77
+ * list_clients
78
+ * list_dashboard
79
+ * list_dynamicdns
80
+ * list_events
81
+ * list_extension
82
+ * list_guests
83
+ * list_health
84
+ * list_hotspotop
85
+ * list_networkconf
86
+ * list_portconf
87
+ * list_portforward_stats
88
+ * list_portforwarding
89
+ * list_rogueaps
90
+ * list_self
91
+ * list_settings
92
+ * list_sites
93
+ * list_usergroups
94
+ * list_users
95
+ * list_wlan_groups
96
+ * list_wlanconf
97
+ * list_current_channels
98
+ * list_dpi_stats
99
+ * reconnect_sta
100
+ * rename_ap
101
+ * restart_ap
102
+ * revoke_voucher
103
+ * extend_guest_validity
104
+ * set_ap_radiosettings
105
+ * set_guestlogin_settings
106
+ * set_sta_name
107
+ * set_sta_note
108
+ * set_usergroup
109
+ * edit_usergroup
110
+ * add_usergroup
111
+ * delete_usergroup
112
+ * set_wlansettings
113
+ * create_wlan
114
+ * delete_wlan
115
+ * site_leds
116
+ * upgrade_device
117
+ * upgrade_device_external
118
+ * spectrum_scan
119
+ * spectrum_scan_state
120
+ * stat_allusers
121
+ * stat_auths
122
+ * stat_client
123
+ * stat_daily_site
124
+ * stat_daily_aps
125
+ * stat_hourly_aps
126
+ * stat_hourly_site
127
+ * stat_payment
128
+ * stat_sessions
129
+ * stat_sites
130
+ * stat_sta_sessions_latest
131
+ * stat_sysinfo
132
+ * stat_voucher
133
+
134
+ You can see the documentation for the methods[here](https://github.com/malle-pietje/UniFi-API-browser/blob/master/phpapi/class.unifi.php).
135
+
136
+ ## Useful links
137
+
138
+ * [Original Unifi API Browser](https://github.com/malle-pietje/UniFi-API-browser)
139
+ * [API as published by Ubiquiti](https://www.ubnt.com/downloads/unifi/5.4.14/unifi_sh_api)
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zumkorn/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.
144
+
145
+ ## License
146
+
147
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/unifi.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "httparty"
2
+ require "unifi/version"
3
+ require "unifi/client"
4
+
5
+ module Unifi
6
+
7
+ end
@@ -0,0 +1,45 @@
1
+ # ./lib/unifi/client.rb
2
+ require "unifi/client/vouchers"
3
+ require "unifi/client/sites"
4
+ require "unifi/client/guests"
5
+ require "unifi/client/wlan"
6
+ require "unifi/client/main"
7
+
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
+ format :json
19
+ def initialize(options = {})
20
+ options[:url] ||= ENV["UNIFI_URL"]
21
+ self.class.base_uri "https://#{options[:url]}/api"
22
+ @site = options[:site] || ENV["UNIFI_SITE"] || "default"
23
+ @username = options[:username] || ENV["UNIFI_USERNAME"]
24
+ @password = options[:password] || ENV["UNIFI_PASSWORD"]
25
+ self.class.default_options.merge!(headers: { 'Content-Type': 'application/json',
26
+ 'Accept': 'application/json' },
27
+ verify: false)
28
+ login
29
+ self.class.default_options.merge!(headers: { 'Cookie': @cookies } )
30
+ end
31
+
32
+ def login
33
+ response = self.class.post("/login",
34
+ body: "{'username': '#{@username}', 'password': '#{@password}'}")
35
+ @cookies = response.headers['set-cookie']
36
+ end
37
+
38
+ def logout
39
+ self.class.get("/logout")
40
+ @cookies = ''
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -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/sitemgr", { 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,3 @@
1
+ module Unifi
2
+ VERSION = "0.1.0"
3
+ end
data/unifi.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/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unifi"
8
+ spec.version = Unifi::VERSION
9
+ spec.date = '2017-07-23'
10
+ spec.authors = ["Sergey Korolev"]
11
+ spec.email = ["zumkorn@gmail.com"]
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/zumkorn/unifi"
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,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unifi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Korolev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-23 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
+ - zumkorn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".idea/.rakeTasks"
64
+ - ".idea/misc.xml"
65
+ - ".idea/modules.xml"
66
+ - ".idea/unifi.iml"
67
+ - ".idea/vcs.xml"
68
+ - ".idea/workspace.xml"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/unifi.rb
76
+ - lib/unifi/client.rb
77
+ - lib/unifi/client/guests.rb
78
+ - lib/unifi/client/main.rb
79
+ - lib/unifi/client/sites.rb
80
+ - lib/unifi/client/vouchers.rb
81
+ - lib/unifi/client/wlan.rb
82
+ - lib/unifi/version.rb
83
+ - unifi.gemspec
84
+ homepage: https://github.com/zumkorn/unifi
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.10
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: A Ruby interface to UniFi Controller API
108
+ test_files: []