Created
January 18, 2014 10:28
-
-
Save KonradIT/8488636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GoproClient | |
attr_reader :ip_address | |
attr_reader :password | |
attr_reader :delay | |
def initialize(ip_address, password, delay = 1.0) | |
@ip_address = ip_address | |
@password = password | |
@delay = delay | |
end | |
def power_on | |
bacpac('PW', 1) | |
end | |
def power_off | |
bacpac('PW', 0) | |
end | |
def mode_photo | |
camera('CM', 1) | |
end | |
def photo_resolution(preset) | |
camera('PR', preset) | |
end | |
def start_capture | |
bacpac('SH', 1) | |
end | |
def stop_capture | |
bacpac('SH', 0) | |
end | |
def capture_photo | |
start_capture | |
last_dir = directories.last | |
last_file = files(last_dir[:href]).last | |
last_file[:url] | |
end | |
def directories | |
html = Nokogiri::HTML(open(storage_url)) | |
html.search('a.link').map do |a| | |
{name: a.text, href: a[:href], url: storage_url(a[:href])} | |
end | |
end | |
def files(dir) | |
html = Nokogiri::HTML(open(storage_url(dir))) | |
html.search('a.link').map do |a| | |
{name: a.text, href: a[:href], url: storage_url(dir + '/' + a[:href]), } | |
end | |
end | |
private | |
def bacpac(method, param) | |
output = open(api_url('bacpac', method, param)).read | |
sleep delay | |
output | |
end | |
def camera(method, param) | |
output = open(api_url('camera', method, param)).read | |
sleep delay | |
output | |
end | |
def api_url(component, method, param) | |
url = sprintf('http://%s/%s/%s?t=%s&p=%%%02d', ip_address, component, method, password, param) | |
puts url | |
url | |
end | |
def storage_url(path = '') | |
url = sprintf('http://%s:8080/videos/DCIM/%s', ip_address, path) | |
puts url | |
url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment