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.



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
  
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



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

def cookies
  @cookies
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#site_idObject

Returns the value of attribute site_id.



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

def site_id
  @site_id
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#api_urlObject



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_alertsObject



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 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



83
84
85
# File 'lib/unifi/api.rb', line 83

def block_client(mac)
  mac_cmd mac, 'block-sta'
end

#create_backupObject



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_alertsObject



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

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

#get_alerts_unarchivedObject



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_apsObject



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_clientsObject



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

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

#get_eventsObject



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_24hObject



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

def get_statistics_last_24h
  get_statistics_24h(Time.now)
end

#get_user_groupsObject



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

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

#get_usersObject



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

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

#get_wlan_confObject



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

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

#loginObject



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

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



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

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

#logoutObject



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 unauthorize_guest(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

#urlObject



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

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