Skip to content

Instantly share code, notes, and snippets.

@mchung
Last active December 31, 2015 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchung/2d0b2bdfe8a6b62fc895 to your computer and use it in GitHub Desktop.
Save mchung/2d0b2bdfe8a6b62fc895 to your computer and use it in GitHub Desktop.
Remote Roku Ruby controller
require "io/console"
require "socket"
require "net/http"
require "uri"
require 'cgi'
SSDP_ADDR = "239.255.255.250";
SSDP_PORT = 1900;
SSDP_ST = "roku:ecp";
def find_roku_url
ssdp_request = "M-SEARCH * HTTP/1.1\r\n" + \
"HOST: #{SSDP_ADDR}:#{SSDP_PORT}\r\n" + \
"MAN: \"ssdp:discover\"\r\n" + \
"ST: #{SSDP_ST}\r\n" + "\r\n";
print ssdp_request
s = UDPSocket.new
s.send(ssdp_request, 0, SSDP_ADDR, SSDP_PORT)
print s.recv(1000) # todo. parse this out
return "http://192.168.1.73:8060/"
end
@url = find_roku_url
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
input << STDIN.read_nonblock(2) rescue nil
end
ensure
STDIN.echo = true
STDIN.cooked!
return input
end
def call(url, path, method = :post)
if method == :get
Net::HTTP.get_response(URI(url + path))
else
Net::HTTP.post_form(URI(url + path), {})
end
end
def press(command)
call(@url, "keypress/#{command}")
end
def send_to_roku(c)
case c
when "`"; puts "HOME"; press "Home"
when "\eb"; puts "OPTION-LEFT"; press "Rev"
when "\ef"; puts "OPTION-RIGHT"; press "Fwd"
when " "; puts "SPACE"; press "Play"
when "\r"; puts "\n"; press "Select"
when "\e[D"; puts "LEFT"; press "Left"
when "\e[C"; puts "RIGHT"; press "Right"
when "\e[B"; puts "DOWN"; press "Down"
when "\e[A"; puts "UP"; press "Up"
when "\e"; puts "ESC"; press "Back"
when "\u0012"; puts "REPLAY"; press "InstantReplay"
when "?"; puts "INFO"; press "Info"
when "\177"; puts "BACKSPACE"; press "Backspace"
when "/"; puts "SEARCH"; press "Search"
when "\u0003"; puts "CONTROL-C"; exit 0
when /^.$/; print c; press "Lit_#{CGI.escape(c)}"
else puts "OTHER: #{c.inspect}"
end
end
loop do send_to_roku(read_char) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment