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.
11 12 13 14 15 16 17 18 19 |
# File 'lib/unifi/api.rb', line 11 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.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def @cookies end |
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def port @port end |
#site_id ⇒ Object
Returns the value of attribute site_id.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def site_id @site_id end |
#username ⇒ Object
Returns the value of attribute username.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def username @username end |
#version ⇒ Object
Returns the value of attribute version.
9 10 11 |
# File 'lib/unifi/api.rb', line 9 def version @version end |
Instance Method Details
#api_url ⇒ Object
25 26 27 28 29 30 |
# File 'lib/unifi/api.rb', line 25 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
110 111 112 113 |
# File 'lib/unifi/api.rb', line 110 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/unifi/api.rb', line 130 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
83 84 85 |
# File 'lib/unifi/api.rb', line 83 def block_client(mac) mac_cmd mac, 'block-sta' end |
#create_backup ⇒ Object
115 116 117 118 119 |
# File 'lib/unifi/api.rb', line 115 def create_backup params = {'cmd': 'backup'} result = read "#{api_url}cmd/system", params, :post result[0]['url'] end |
#disconnect_client(mac) ⇒ Object
91 92 93 |
# File 'lib/unifi/api.rb', line 91 def disconnect_client(mac) mac_cmd mac, 'kick-sta' end |
#get_alerts ⇒ Object
36 37 38 |
# File 'lib/unifi/api.rb', line 36 def get_alerts read "#{api_url}list/alarm" end |
#get_alerts_unarchived ⇒ Object
40 41 42 43 |
# File 'lib/unifi/api.rb', line 40 def get_alerts_unarchived params ={'json': {'_sort': '-time', 'archived': false}} read "#{api_url}list/alarm", params end |
#get_aps ⇒ Object
62 63 64 65 |
# File 'lib/unifi/api.rb', line 62 def get_aps params = {'_depth': 2, 'test': 0} read "#{api_url}stat/device", params end |
#get_backup(target_file = 'unifi-backup.unf') ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/unifi/api.rb', line 121 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
67 68 69 |
# File 'lib/unifi/api.rb', line 67 def get_clients read "#{api_url}stat/sta" end |
#get_events ⇒ Object
58 59 60 |
# File 'lib/unifi/api.rb', line 58 def get_events read "#{api_url}stat/event" end |
#get_statistics_24h(endtime) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/unifi/api.rb', line 49 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
45 46 47 |
# File 'lib/unifi/api.rb', line 45 def get_statistics_last_24h get_statistics_24h(Time.now) end |
#get_user_groups ⇒ Object
75 76 77 |
# File 'lib/unifi/api.rb', line 75 def get_user_groups read "#{api_url}list/usergroup" end |
#get_users ⇒ Object
71 72 73 |
# File 'lib/unifi/api.rb', line 71 def get_users read "#{api_url}list/user" end |
#get_wlan_conf ⇒ Object
79 80 81 |
# File 'lib/unifi/api.rb', line 79 def get_wlan_conf read "#{api_url}list/wlanconf" end |
#login ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/unifi/api.rb', line 156 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, ) @cookies = res.headers['set-cookie'] get_data res end |
#login_url ⇒ Object
32 33 34 |
# File 'lib/unifi/api.rb', line 32 def login_url @version == 'v4' ? "#{url}api/login" : "#{url}login" end |
#logout ⇒ Object
171 172 173 |
# File 'lib/unifi/api.rb', line 171 def logout @cookies = nil end |
#restart_ap(mac) ⇒ Object
95 96 97 |
# File 'lib/unifi/api.rb', line 95 def restart_ap(mac) mac_cmd mac, 'restart', 'devmgr' end |
#restart_ap_name(name = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/unifi/api.rb', line 99 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
150 151 152 153 154 |
# File 'lib/unifi/api.rb', line 150 def (guest_mac) cmd = 'unauthorize-guest' params = {'mac': guest_mac} run_command(cmd, params) end |
#unblock_client(mac) ⇒ Object
87 88 89 |
# File 'lib/unifi/api.rb', line 87 def unblock_client(mac) mac_cmd mac, 'unblock-sta' end |
#url ⇒ Object
21 22 23 |
# File 'lib/unifi/api.rb', line 21 def url "https://#{host}:#{port}/" end |