Class: Wemo::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/wemo/switch.rb

Class Method Summary collapse

Class Method Details

.send_command(wemo, action) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wemo/switch.rb', line 7

def self.send_command(wemo, action)
  case action
  when "on"
    signal = 1
  when "off"
    signal = 0
  end
  include_location_details = true
  wemo_device = nil
  device_name = nil
  device_location = nil

  SimpleUpnp::Discovery.find do |device|
    begin
      device_json = device.to_json(include_location_details)
    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']
          # puts "WeMo Friendly Name: " + friendlyName
          if friendlyName.downcase == wemo.strip.downcase
            wemo_device = device_json['root']['device']
            device_name = friendlyName
            device_location = /https?:\/\/[\S]+\//.match(device.location)
            break
          end
        end
      end
    end
  end
  if wemo_device
    c = Curl::Easy.new(device_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 Curb::Err
    end
    # c.body_str
  end
end