Class: Wemote::Switch
- Inherits:
-
Object
- Object
- Wemote::Switch
- Defined in:
- lib/wemote/switch.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- GET_HEADERS =
{ "SOAPACTION" => '"urn:Belkin:service:basicevent:1#GetBinaryState"', "Content-type" => 'text/xml; charset="utf-8"' }
- SET_HEADERS =
{ "SOAPACTION" => '"urn:Belkin:service:basicevent:1#SetBinaryState"', "Content-type" => 'text/xml; charset="utf-8"' }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.all(refresh = false) ⇒ Array
Returns all Switches detected on the local network.
- .device_type ⇒ Object
-
.find(name) ⇒ Wemote::Switch
Returns a Switch of a given name.
Instance Method Summary collapse
- #device_type ⇒ Object
-
#initialize(host, port = nil) ⇒ Switch
constructor
A new instance of Switch.
-
#off! ⇒ Object
Turn the Switch off.
-
#off? ⇒ Boolean
Return whether the Switch is off.
-
#on! ⇒ Object
Turn the Switch on.
-
#on? ⇒ Boolean
Return whether the Switch is on.
-
#poll(rate = 0.25, async = true, &block) ⇒ Thread
Monitors the state of the Switch via polling, and yields to the block given with the updated state.
-
#toggle! ⇒ Object
Turn the Switch on or off, based on its current state.
Constructor Details
#initialize(host, port = nil) ⇒ Switch
Returns a new instance of Switch.
60 61 62 63 |
# File 'lib/wemote/switch.rb', line 60 def initialize(host,port=nil) @host, @port = host, port end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
58 59 60 |
# File 'lib/wemote/switch.rb', line 58 def name @name end |
Class Method Details
.all(refresh = false) ⇒ Array
Returns all Switches detected on the local network
34 35 36 37 |
# File 'lib/wemote/switch.rb', line 34 def all(refresh=false) @switches = nil if refresh @switches ||= Wemote::Collection::Switch.new(discover) end |
.device_type ⇒ Object
26 27 28 |
# File 'lib/wemote/switch.rb', line 26 def device_type 'urn:Belkin:device:controllee:1' end |
.find(name) ⇒ Wemote::Switch
Returns a Switch of a given name
43 44 45 |
# File 'lib/wemote/switch.rb', line 43 def find(name) all.detect{|s|s.name == name} end |
Instance Method Details
#device_type ⇒ Object
65 66 67 |
# File 'lib/wemote/switch.rb', line 65 def device_type 'urn:Belkin:device:controllee:1' end |
#off! ⇒ Object
Turn the Switch off
75 76 77 |
# File 'lib/wemote/switch.rb', line 75 def off! set_state(0) end |
#off? ⇒ Boolean
Return whether the Switch is off
87 88 89 |
# File 'lib/wemote/switch.rb', line 87 def off? get_state == :off end |
#on! ⇒ Object
Turn the Switch on
80 81 82 |
# File 'lib/wemote/switch.rb', line 80 def on! set_state(1) end |
#on? ⇒ Boolean
Return whether the Switch is on
94 95 96 |
# File 'lib/wemote/switch.rb', line 94 def on? get_state == :on end |
#poll(rate = 0.25, async = true, &block) ⇒ Thread
Monitors the state of the Switch via polling, and yields to the block given with the updated state.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/wemote/switch.rb', line 114 def poll(rate=0.25,async=true,&block) old_state = get_state poller = Thread.start do loop do begin state = get_state if state != old_state old_state = state yield state end rescue Exception end sleep rate end end puts "Monitoring #{@name} for changes" async ? poller : poller.join end |
#toggle! ⇒ Object
Turn the Switch on or off, based on its current state
70 71 72 |
# File 'lib/wemote/switch.rb', line 70 def toggle! on? ? off! : on! end |