Class: Unifi::Api::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/unifi/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



10
11
12
# File 'lib/unifi/api.rb', line 10

def cookies
  @cookies
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/unifi/api.rb', line 10

def host
  @host
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/unifi/api.rb', line 10

def password
  @password
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/unifi/api.rb', line 10

def port
  @port
end

#site_idObject

Returns the value of attribute site_id.



10
11
12
# File 'lib/unifi/api.rb', line 10

def site_id
  @site_id
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/unifi/api.rb', line 10

def username
  @username
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/unifi/api.rb', line 10

def version
  @version
end

Instance Method Details

#api_urlObject



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_alertsObject



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 authorize_guest(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_backupObject



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_alertsObject



37
38
39
# File 'lib/unifi/api.rb', line 37

def get_alerts
  read "#{api_url}list/alarm"
end

#get_alerts_unarchivedObject



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_apsObject



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_clientsObject



68
69
70
# File 'lib/unifi/api.rb', line 68

def get_clients
  read "#{api_url}stat/sta"
end

#get_eventsObject



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_24hObject



46
47
48
# File 'lib/unifi/api.rb', line 46

def get_statistics_last_24h
  get_statistics_24h(Time.now)
end

#get_user_groupsObject



76
77
78
# File 'lib/unifi/api.rb', line 76

def get_user_groups
  read "#{api_url}list/usergroup"
end

#get_usersObject



72
73
74
# File 'lib/unifi/api.rb', line 72

def get_users
  read "#{api_url}list/user"
end

#get_wlan_confObject



80
81
82
# File 'lib/unifi/api.rb', line 80

def get_wlan_conf
  read "#{api_url}list/wlanconf"
end

#loginObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/unifi/api.rb', line 157

def 
  params = {'username': @username, 'password': @password}
  params['login'] = 'login' unless version == 'v4'

  options = {
    verify: false,
    body: params.to_json,
    headers: {'Content-Type': 'application/json','Accept': 'application/json'}
  }

  res = HTTParty.post(, options)
  @cookies = res.headers['set-cookie']
  get_data res
end

#login_urlObject



33
34
35
# File 'lib/unifi/api.rb', line 33

def 
  @version == 'v4' ? "#{url}api/login" : "#{url}login"
end

#logoutObject



172
173
174
# File 'lib/unifi/api.rb', line 172

def logout
  @cookies = 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 unauthorize_guest(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

#urlObject



22
23
24
# File 'lib/unifi/api.rb', line 22

def url
  "https://#{host}:#{port}/"
end