Class: MayGionIPCam

Inherits:
Object
  • Object
show all
Defined in:
lib/maygion-ipcam.rb

Constant Summary collapse

Factor =
500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ MayGionIPCam

Returns a new instance of MayGionIPCam.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/maygion-ipcam.rb', line 13

def initialize(opt)
  
  o = {username: 'user', password: 'user', address: '192.168.1.14', port: 80, cookie: ''}.merge(opt)
  @addr, @port = o[:address], o[:port]
  @raw_url = "http://#{@addr}/cgi.cmd?cmd=moveptz&user=user&pwd=user"
  @cookie = o[:cookie]
  
  if @cookie.empty? then
    @resp, data = (o[:username], o[:password])
    @cookie = @resp.response['set-cookie'].scan(/\w+\=[^=]+(?=;)/).join(';')
  end
end

Instance Attribute Details

Returns the value of attribute cookie.



10
11
12
# File 'lib/maygion-ipcam.rb', line 10

def cookie
  @cookie
end

#respObject (readonly)

Returns the value of attribute resp.



10
11
12
# File 'lib/maygion-ipcam.rb', line 10

def resp
  @resp
end

Instance Method Details

#down(i = 1) ⇒ Object



38
39
40
# File 'lib/maygion-ipcam.rb', line 38

def down(i=1)
  move_camera "&dir=btnPtzDown&tick=%s&nPtzTimes=1" %  [i * Factor]
end

#left(i = 1) ⇒ Object



26
27
28
# File 'lib/maygion-ipcam.rb', line 26

def left(i=1)
  move_camera "&dir=btnPtzLeft&tick=%s&nPtzTimes=1" %  [i * Factor]
end

#login(username, password) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/maygion-ipcam.rb', line 42

def (username, password)

  http = Net::HTTP.new(@addr, @port)
  path = '/cgi-bin/cgicmd'

  # POST request -> logging in
  data = 'szAccount=user&szPassword=user&cmd=Login&urlOnSuccess=/mobile.asp'
  headers = {
    'Referer' => "http://#{@addr}/login.asp",
    'Content-Type' => 'application/x-www-form-urlencoded'
  }

  http.post(path, data, headers)
end

#move_camera(instruction) ⇒ Object



57
58
59
60
# File 'lib/maygion-ipcam.rb', line 57

def move_camera(instruction)
  url = @raw_url + instruction
  open(url, 'UserAgent' => 'Ruby IPCO (IP Camera Operator)', 'Cookie' => @cookie).read
end

#right(i = 1) ⇒ Object



30
31
32
# File 'lib/maygion-ipcam.rb', line 30

def right(i=1)
  move_camera "&dir=btnPtzRight&tick=%s&nPtzTimes=1" %  [i * Factor]
end

#up(i = 1) ⇒ Object



34
35
36
# File 'lib/maygion-ipcam.rb', line 34

def up(i=1)
  move_camera "&dir=btnPtzUp&tick=%s&nPtzTimes=1" %  [i * Factor]
end