Class: Grandstream::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/grandstream/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(camera_ip) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/grandstream/client.rb', line 9

def initialize(camera_ip)
    @camera_ip  = camera_ip
end

Instance Attribute Details

#camera_ipObject

Returns the value of attribute camera_ip.



7
8
9
# File 'lib/grandstream/client.rb', line 7

def camera_ip
  @camera_ip
end

Instance Method Details

#base_urlObject



17
18
19
# File 'lib/grandstream/client.rb', line 17

def base_url 
    "http://#{camera_ip}"
end

#get(path) ⇒ Object



25
26
27
28
# File 'lib/grandstream/client.rb', line 25

def get(path)
    url = "#{base_url}/goform/#{path}?cmd=get"
    open(url).read
end

#get_snapshot(image_path) ⇒ Object



21
22
23
# File 'lib/grandstream/client.rb', line 21

def get_snapshot(image_path)
    File.write(image_path, open(snapshot_url).read, {mode: 'wb'})
end

#http_request(url) ⇒ Object



48
49
50
51
52
53
# File 'lib/grandstream/client.rb', line 48

def http_request(url) 
    uri = URI url
    Net::HTTP.start(uri.host, uri.port) do |http|
        yield(http, uri)
    end
end

#load_options(path) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/grandstream/client.rb', line 39

def load_options(path)
    options_hash = {}
    get(path).split("\n").each do |line|
        vals = line.split('=')
        options_hash[vals[0]] = vals[1].chomp if vals[1]
    end
    options_hash
end

#set(path, params) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/grandstream/client.rb', line 30

def set(path, params)
    url = "#{base_url}/goform/#{path}?cmd=set&#{params.map{|k,v| "#{k}=#{v}"}.join("&")}"
    http_request(url) do |http, uri|
        req = Net::HTTP::Get.new(uri)
        req.basic_auth 'admin', 'admin'
        response = http.request(req)
    end
end

#set_options(path, options) ⇒ Object



55
56
57
# File 'lib/grandstream/client.rb', line 55

def set_options(path, options)
    set(path, options)
end

#snapshot_urlObject



13
14
15
# File 'lib/grandstream/client.rb', line 13

def snapshot_url
    "#{base_url}/snapshot/view0.jpg"
end