Class: Samsung::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/samsung/remote.rb

Constant Summary collapse

PORT =
55000
APP_NAME =
"net.kapati.samsung.remote"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, display_name = "Ruby") ⇒ Remote

Returns a new instance of Remote.



10
11
12
13
14
15
# File 'lib/samsung/remote.rb', line 10

def initialize(ip, display_name = "Ruby")
  self.ip = ip
  self.display_name = display_name
  connect
  send_auth
end

Instance Attribute Details

#display_nameObject

Returns the value of attribute display_name.



8
9
10
# File 'lib/samsung/remote.rb', line 8

def display_name
  @display_name
end

#ipObject

Returns the value of attribute ip.



8
9
10
# File 'lib/samsung/remote.rb', line 8

def ip
  @ip
end

Instance Method Details

#connectObject



32
33
34
# File 'lib/samsung/remote.rb', line 32

def connect
  @socket = TCPSocket.open ip, PORT
end

#has_data?(wait = 0) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/samsung/remote.rb', line 17

def has_data?(wait = 0)
  ready = IO.select([@socket], nil, nil, wait)
  return ready != nil
end

#peek_frame(wait = 1) ⇒ Object



22
23
24
25
# File 'lib/samsung/remote.rb', line 22

def peek_frame(wait = 1)
  return nil unless has_data?(wait)
  read_frame
end

#read_frameObject



27
28
29
30
# File 'lib/samsung/remote.rb', line 27

def read_frame
  data = @socket.readpartial(512)
  Protocol::Response.new(data)
end

#send_authObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/samsung/remote.rb', line 46

def send_auth
  net = Network.new
  send_frame(
    Protocol::AuthFrame.new(
      net.local_ip,
      net.local_hw_address,
      display_name
    )
  )
end

#send_frame(frame) ⇒ Object



36
37
38
39
40
# File 'lib/samsung/remote.rb', line 36

def send_frame(frame)
  f = build_header
  f.push_frame(frame)
  @socket.write(f.data)
end

#send_key(key_code) ⇒ Object



42
43
44
# File 'lib/samsung/remote.rb', line 42

def send_key key_code
  send_frame(Protocol::KeyFrame.new(key_code))
end