class Wemo::Switch
Attributes
details[RW]
location[RW]
name[RW]
state[RW]
Public Class Methods
new(name)
click to toggle source
# File lib/wemo/switch.rb, line 11 def initialize(name) self.name = name self.find_device self end
off(name)
click to toggle source
# File lib/wemo/switch.rb, line 21 def self.off(name) new(name).off end
on(name)
click to toggle source
# File lib/wemo/switch.rb, line 17 def self.on(name) new(name).on end
send_command(name, action)
click to toggle source
# File lib/wemo/switch.rb, line 104 def self.send_command(name, action) case action when "on" signal = 1 when "off" signal = 0 when 'status' signal = false end d = Wemo::Switch.new(name) if d.details if signal response = d.set_binary_state(signal) else response = d.get_binary_state end end end
status(name)
click to toggle source
# File lib/wemo/switch.rb, line 25 def self.status(name) new(name).status end
Public Instance Methods
find_device()
click to toggle source
# File lib/wemo/switch.rb, line 78 def find_device SimpleUpnp::Discovery.find do |device| begin device_json = device.to_json(true) rescue next end # puts device_json.inspect if device_json['root'] if device_json['root']['device'] if device_json['root']['device']['friendlyName'] friendlyName = device_json['root']['device']['friendlyName'] if friendlyName.downcase == self.name.strip.downcase self.details = device_json['root']['device'] self.name = friendlyName self.location = /https?:\/\/[\S]+\//.match(device_json[:location]).to_s break end end end end end status end
get_binary_state()
click to toggle source
# File lib/wemo/switch.rb, line 57 def get_binary_state find_device unless self.location != nil c = Curl::Easy.new(self.location.to_s + 'upnp/control/basicevent1') c.headers["Content-type"] = 'text/xml; charset="utf-8"' c.headers["SOAPACTION"] = "\"urn:Belkin:service:basicevent:1#GetBinaryState\"" c.verbose = false begin c.http_post("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"></u:GetBinaryState></s:Body></s:Envelope>") c.perform rescue end xml = Nokogiri::XML(c.body_str).text.strip.to_i self.state = xml if xml == 0 return false else return true end end
off()
click to toggle source
# File lib/wemo/switch.rb, line 33 def off set_binary_state(0) end
on()
click to toggle source
# File lib/wemo/switch.rb, line 29 def on set_binary_state(1) end
set_binary_state(signal)
click to toggle source
# File lib/wemo/switch.rb, line 41 def set_binary_state(signal) find_device unless self.location != nil c = Curl::Easy.new(self.location.to_s + 'upnp/control/basicevent1') c.headers["Content-type"] = 'text/xml; charset="utf-8"' c.headers["SOAPACTION"] = "\"urn:Belkin:service:basicevent:1#SetBinaryState\"" c.verbose = false begin c.http_post("<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><s:Body><u:SetBinaryState xmlns:u='urn:Belkin:service:basicevent:1'><BinaryState>#{signal}</BinaryState></u:SetBinaryState></s:Body></s:Envelope>") c.perform rescue end self.state = signal # c.body_str end
status()
click to toggle source
# File lib/wemo/switch.rb, line 37 def status get_binary_state end