unifi-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d49f93045b59a6ef961ca4006b3f66b0239d5f2
4
+ data.tar.gz: b81e2738cbce359dd31b845384a1f4af110e2534
5
+ SHA512:
6
+ metadata.gz: 90c6783ae4b8c06bc4a6efb77d33d765e7db0dea91979d1768aece7dc9c03104a7f22f4896017cf881866db121c3deec691f3a95b47b3e28c4de8a523ee40ddb
7
+ data.tar.gz: ee5b4b8f378fc29812df617d565181a1bba609a15294058febbddeebc7d315176b74c842a12c64e028a14456110b61e59b52b8ae68f321fc256a1cf34542c0b5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unifi-api.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # Unifi::Api
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/unifi/api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'unifi-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install unifi-api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/unifi-api.
36
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unifi/api"
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
@@ -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
@@ -0,0 +1,218 @@
1
+ require "unifi/api/version"
2
+
3
+ module Unifi
4
+ module Api
5
+ class APIError < Exception
6
+ end
7
+
8
+ class Controller
9
+ attr_accessor :host, :username, :password, :port, :version, :site_id, :cookies
10
+
11
+ def initialize(host, username, password, port=8443, version='v2', site_id='default')
12
+ @host = host
13
+ @port = port
14
+ @version = version
15
+ @username = username
16
+ @password = password
17
+ @site_id = site_id
18
+ login
19
+ end
20
+
21
+ def url
22
+ "https://#{host}:#{port}/"
23
+ end
24
+
25
+ def api_url
26
+ v2_path = 'api/'
27
+ v3_path = "api/s/#{@site_id}/"
28
+ api_path = %w(v3 v4).include?(@version) ? v3_path : v2_path
29
+ "#{url}#{api_path}"
30
+ end
31
+
32
+ def login_url
33
+ @version == 'v4' ? "#{url}api/login" : "#{url}login"
34
+ end
35
+
36
+ def get_alerts
37
+ read "#{api_url}list/alarm"
38
+ end
39
+
40
+ def get_alerts_unarchived
41
+ params ={'json': {'_sort': '-time', 'archived': false}}
42
+ read "#{api_url}list/alarm", params
43
+ end
44
+
45
+ def get_statistics_last_24h
46
+ get_statistics_24h(Time.now)
47
+ end
48
+
49
+ def get_statistics_24h(endtime)
50
+ params = {
51
+ 'attrs': ["wlan_bytes", "wlan-num_sta", "time"],
52
+ 'start': "#{(endtime - 1.day).to_i * 1000}",
53
+ 'end': "#{(endtime - 1.hour).to_i * 1000}"
54
+ }
55
+ return read "#{api_url}stat/report/hourly.site", params, :post
56
+ end
57
+
58
+ def get_events
59
+ read "#{api_url}stat/event"
60
+ end
61
+
62
+ def get_aps
63
+ params = {'_depth': 2, 'test': 0}
64
+ read "#{api_url}stat/device", params
65
+ end
66
+
67
+ def get_clients
68
+ read "#{api_url}stat/sta"
69
+ end
70
+
71
+ def get_users
72
+ read "#{api_url}list/user"
73
+ end
74
+
75
+ def get_user_groups
76
+ read "#{api_url}list/usergroup"
77
+ end
78
+
79
+ def get_wlan_conf
80
+ read "#{api_url}list/wlanconf"
81
+ end
82
+
83
+ def block_client(mac)
84
+ mac_cmd mac, 'block-sta'
85
+ end
86
+
87
+ def unblock_client(mac)
88
+ mac_cmd mac, 'unblock-sta'
89
+ end
90
+
91
+ def disconnect_client(mac)
92
+ mac_cmd mac, 'kick-sta'
93
+ end
94
+
95
+ def restart_ap(mac)
96
+ mac_cmd mac, 'restart', 'devmgr'
97
+ end
98
+
99
+ def restart_ap_name(name=nil)
100
+ if name.nil?
101
+ raise APIError("#{name} is not valid name")
102
+ end
103
+ get_aps.each do |ap|
104
+ if ap['state'] == 1 and ap['name'] == name
105
+ restart_ap(ap['mac'])
106
+ end
107
+ end
108
+ end
109
+
110
+ def archive_all_alerts
111
+ params = {'cmd': 'archive-all-alarms'}
112
+ read "#{api_url}cmd/evtmgr", params, :post
113
+ end
114
+
115
+ def create_backup
116
+ params = {'cmd': 'backup'}
117
+ result = read "#{api_url}cmd/system", params, :post
118
+ result[0]['url']
119
+ end
120
+
121
+ def get_backup(target_file='unifi-backup.unf')
122
+ download_path = create_backup
123
+
124
+ unifi_archive = read("#{url}#{download_path}", nil, :post)
125
+ File.open(target_file, "wb") do |f|
126
+ f.write(unifi_archive)
127
+ end
128
+ end
129
+
130
+ def authorize_guest(guest_mac, minutes, up_bandwidth=nil, down_bandwidth=nil, byte_quota=nil, ap_mac=nil)
131
+ cmd = 'authorize-guest'
132
+ params = {'mac': guest_mac, 'minutes': minutes}
133
+
134
+ if up_bandwidth
135
+ params['up'] = up_bandwidth
136
+ end
137
+ if down_bandwidth
138
+ params['down'] = down_bandwidth
139
+ end
140
+ if byte_quota
141
+ params['bytes'] = byte_quota
142
+ end
143
+ if ap_mac && @version != 'v2'
144
+ params['ap_mac'] = ap_mac
145
+ end
146
+
147
+ run_command cmd, params
148
+ end
149
+
150
+ def unauthorize_guest(guest_mac)
151
+ cmd = 'unauthorize-guest'
152
+ params = {'mac': guest_mac}
153
+ run_command(cmd, params)
154
+ end
155
+
156
+ def login
157
+ params = {'username': @username, 'password': @password}
158
+ params['login'] = 'login' unless version == 'v4'
159
+
160
+ options = {
161
+ verify: false,
162
+ body: params.to_json,
163
+ headers: {'Content-Type': 'application/json','Accept': 'application/json'}
164
+ }
165
+
166
+ res = HTTParty.post(login_url, options)
167
+ @cookies = res.headers['set-cookie']
168
+ get_data res
169
+ end
170
+
171
+ def logout
172
+ @cookies = nil
173
+ end
174
+
175
+ private
176
+
177
+ def read(url, params=nil, method=:get)
178
+ options = {
179
+ verify: false,
180
+ body: params.to_json,
181
+ headers: {
182
+ 'Content-Type': 'application/json',
183
+ 'Accept': 'application/json',
184
+ 'Cookie': @cookies || ''
185
+ }
186
+ }
187
+
188
+ if method == :get
189
+ get_data HTTParty.get(url, options)
190
+ elsif method == :post
191
+ get_data HTTParty.post(url, options)
192
+ end
193
+ end
194
+
195
+ def get_data(res)
196
+ obj = res.parsed_response
197
+ if obj && obj['meta'] && obj['meta']['rc'] != 'ok'
198
+ raise APIError, obj['meta']['msg']
199
+ end
200
+ if obj && obj['data']
201
+ return obj['data']
202
+ end
203
+ obj
204
+ end
205
+
206
+ def run_command(command, params={}, mgr='stamgr')
207
+ params['cmd'] = command
208
+ read "#{api_url}cmd/#{mgr}", params, :post
209
+ end
210
+
211
+ def mac_cmd(target_mac, command, mgr='stamgr')
212
+ params = {'mac': target_mac}
213
+ run_command(command, params, mgr)
214
+ end
215
+
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,5 @@
1
+ module Unifi
2
+ module Api
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unifi/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unifi-api"
8
+ spec.version = Unifi::Api::VERSION
9
+ spec.authors = ["hculap"]
10
+ spec.email = ["hculap@gmail.com"]
11
+
12
+ spec.summary = %q{A rewrite of https://github.com/calmh/unifi-api in Ruby.}
13
+ spec.description = %q{Ruby Api for UniFi Controller - Ubiquiti Networks Access Points}
14
+ spec.homepage = "https://github.com/hculap/unifi-api"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency 'httparty', '~> 0'
24
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unifi-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hculap
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-02 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: Ruby Api for UniFi Controller - Ubiquiti Networks Access Points
56
+ email:
57
+ - hculap@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/unifi/api.rb
69
+ - lib/unifi/api/version.rb
70
+ - unifi-api.gemspec
71
+ homepage: https://github.com/hculap/unifi-api
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: A rewrite of https://github.com/calmh/unifi-api in Ruby.
94
+ test_files: []