Class: Unifi::Api::Controller
- Inherits:
-
Object
- Object
- Unifi::Api::Controller
- Defined in:
- lib/unifi/api.rb
Instance Attribute Summary collapse
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#site_id ⇒ Object
Returns the value of attribute site_id.
-
#username ⇒ Object
Returns the value of attribute username.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #api_url ⇒ Object
- #archive_all_alerts ⇒ Object
- #authorize_guest(guest_mac, minutes, up_bandwidth = nil, down_bandwidth = nil, byte_quota = nil, ap_mac = nil) ⇒ Object
- #block_client(mac) ⇒ Object
- #create_backup ⇒ Object
- #disconnect_client(mac) ⇒ Object
- #get_alerts ⇒ Object
- #get_alerts_unarchived ⇒ Object
- #get_aps ⇒ Object
- #get_backup(target_file = 'unifi-backup.unf') ⇒ Object
- #get_clients ⇒ Object
- #get_events ⇒ Object
- #get_statistics_24h(endtime) ⇒ Object
- #get_statistics_last_24h ⇒ Object
- #get_user_groups ⇒ Object
- #get_users ⇒ Object
- #get_wlan_conf ⇒ Object
-
#initialize(host, username, password, port = 8443, version = 'v2', site_id = 'default') ⇒ Controller
constructor
A new instance of Controller.
- #login ⇒ Object
- #login_url ⇒ Object
- #logout ⇒ Object
- #restart_ap(mac) ⇒ Object
- #restart_ap_name(name = nil) ⇒ Object
- #unauthorize_guest(guest_mac) ⇒ Object
- #unblock_client(mac) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(host, username, password, port = 8443, version = 'v2', site_id = 'default') ⇒ Controller
Returns a new instance of Controller.
12 13 14 15 16 17 18 19 20 |
# File 'lib/unifi/api.rb', line 12 def initialize(host, username, password, port=8443, version='v2', site_id='default') @host = host @port = port @version = version @username = username @password = password @site_id = site_id login end |
Instance Attribute Details
#cookies ⇒ Object
Returns the value of attribute cookies.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def end |
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def port @port end |
#site_id ⇒ Object
Returns the value of attribute site_id.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def site_id @site_id end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def username @username end |
#version ⇒ Object
Returns the value of attribute version.
10 11 12 |
# File 'lib/unifi/api.rb', line 10 def version @version end |
Instance Method Details
#api_url ⇒ Object
26 27 28 29 30 31 |
# File 'lib/unifi/api.rb', line 26 def api_url v2_path = 'api/' v3_path = "api/s/#{@site_id}/" api_path = %w(v3 v4).include?(@version) ? v3_path : v2_path "#{url}#{api_path}" end |
#archive_all_alerts ⇒ Object
111 112 113 114 |
# File 'lib/unifi/api.rb', line 111 def archive_all_alerts params = {'cmd': 'archive-all-alarms'} read "#{api_url}cmd/evtmgr", params, :post end |
#authorize_guest(guest_mac, minutes, up_bandwidth = nil, down_bandwidth = nil, byte_quota = nil, ap_mac = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/unifi/api.rb', line 131 def (guest_mac, minutes, up_bandwidth=nil, down_bandwidth=nil, byte_quota=nil, ap_mac=nil) cmd = 'authorize-guest' params = {'mac': guest_mac, 'minutes': minutes} if up_bandwidth params['up'] = up_bandwidth end if down_bandwidth params['down'] = down_bandwidth end if byte_quota params['bytes'] = byte_quota end if ap_mac && @version != 'v2' params['ap_mac'] = ap_mac end run_command cmd, params end |
#block_client(mac) ⇒ Object
84 85 86 |
# File 'lib/unifi/api.rb', line 84 def block_client(mac) mac_cmd mac, 'block-sta' end |
#create_backup ⇒ Object
116 117 118 119 120 |
# File 'lib/unifi/api.rb', line 116 def create_backup params = {'cmd': 'backup'} result = read "#{api_url}cmd/system", params, :post result[0]['url'] end |
#disconnect_client(mac) ⇒ Object
92 93 94 |
# File 'lib/unifi/api.rb', line 92 def disconnect_client(mac) mac_cmd mac, 'kick-sta' end |
#get_alerts ⇒ Object
37 38 39 |
# File 'lib/unifi/api.rb', line 37 def get_alerts read "#{api_url}list/alarm" end |
#get_alerts_unarchived ⇒ Object
41 42 43 44 |
# File 'lib/unifi/api.rb', line 41 def get_alerts_unarchived params ={'json': {'_sort': '-time', 'archived': false}} read "#{api_url}list/alarm", params end |
#get_aps ⇒ Object
63 64 65 66 |
# File 'lib/unifi/api.rb', line 63 def get_aps params = {'_depth': 2, 'test': 0} read "#{api_url}stat/device", params end |
#get_backup(target_file = 'unifi-backup.unf') ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/unifi/api.rb', line 122 def get_backup(target_file='unifi-backup.unf') download_path = create_backup unifi_archive = read("#{url}#{download_path}", nil, :post) File.open(target_file, "wb") do |f| f.write(unifi_archive) end end |
#get_clients ⇒ Object
68 69 70 |
# File 'lib/unifi/api.rb', line 68 def get_clients read "#{api_url}stat/sta" end |
#get_events ⇒ Object
59 60 61 |
# File 'lib/unifi/api.rb', line 59 def get_events read "#{api_url}stat/event" end |
#get_statistics_24h(endtime) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/unifi/api.rb', line 50 def get_statistics_24h(endtime) params = { 'attrs': ["wlan_bytes", "wlan-num_sta", "time"], 'start': "#{(endtime - 1.day).to_i * 1000}", 'end': "#{(endtime - 1.hour).to_i * 1000}" } return read "#{api_url}stat/report/hourly.site", params, :post end |
#get_statistics_last_24h ⇒ Object
46 47 48 |
# File 'lib/unifi/api.rb', line 46 def get_statistics_last_24h get_statistics_24h(Time.now) end |
#get_user_groups ⇒ Object
76 77 78 |
# File 'lib/unifi/api.rb', line 76 def get_user_groups read "#{api_url}list/usergroup" end |
#get_users ⇒ Object
72 73 74 |
# File 'lib/unifi/api.rb', line 72 def get_users read "#{api_url}list/user" end |
#get_wlan_conf ⇒ Object
80 81 82 |
# File 'lib/unifi/api.rb', line 80 def get_wlan_conf read "#{api_url}list/wlanconf" end |
#login ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/unifi/api.rb', line 157 def login params = {'username': @username, 'password': @password} params['login'] = 'login' unless version == 'v4' = { verify: false, body: params.to_json, headers: {'Content-Type': 'application/json','Accept': 'application/json'} } res = HTTParty.post(login_url, ) = res.headers['set-cookie'] get_data res end |
#login_url ⇒ Object
33 34 35 |
# File 'lib/unifi/api.rb', line 33 def login_url @version == 'v4' ? "#{url}api/login" : "#{url}login" end |
#logout ⇒ Object
172 173 174 |
# File 'lib/unifi/api.rb', line 172 def logout = nil end |
#restart_ap(mac) ⇒ Object
96 97 98 |
# File 'lib/unifi/api.rb', line 96 def restart_ap(mac) mac_cmd mac, 'restart', 'devmgr' end |
#restart_ap_name(name = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/unifi/api.rb', line 100 def restart_ap_name(name=nil) if name.nil? raise APIError("#{name} is not valid name") end get_aps.each do |ap| if ap['state'] == 1 and ap['name'] == name restart_ap(ap['mac']) end end end |
#unauthorize_guest(guest_mac) ⇒ Object
151 152 153 154 155 |
# File 'lib/unifi/api.rb', line 151 def (guest_mac) cmd = 'unauthorize-guest' params = {'mac': guest_mac} run_command(cmd, params) end |
#unblock_client(mac) ⇒ Object
88 89 90 |
# File 'lib/unifi/api.rb', line 88 def unblock_client(mac) mac_cmd mac, 'unblock-sta' end |
#url ⇒ Object
22 23 24 |
# File 'lib/unifi/api.rb', line 22 def url "https://#{host}:#{port}/" end |