class Domotics::Core::Switch

Constants

MINIMUM_LAG

Public Class Methods

new(args = {}) click to toggle source
Calls superclass method Domotics::Core::Element::new
# File lib/domotics/core/element/switch.rb, line 4
def initialize(args = {})
  @type = args[:type] || :switch
  @lag = nil
  @lag_lock = Mutex.new
  args[:driver] = "DigitalPin"
  load_driver args
  @initialized = false
  super
  @initialized = true
end

Public Instance Methods

delay_off(timer, &block) click to toggle source
# File lib/domotics/core/element/switch.rb, line 41
def delay_off(timer, &block)
  lag(:off, timer, &block)
end
delay_on(timer) click to toggle source
# File lib/domotics/core/element/switch.rb, line 28
def delay_on(timer)
  lag(:on, timer)
end
delay_toggle(timer) click to toggle source
# File lib/domotics/core/element/switch.rb, line 50
def delay_toggle(timer)
  lag(:toggle, timer)
end
off(timer = nil) click to toggle source
# File lib/domotics/core/element/switch.rb, line 32
def off(timer = nil)
  set_state :off
  lag(:on, timer)
end
off?() click to toggle source
# File lib/domotics/core/element/switch.rb, line 37
def off?
  state == :off
end
on(timer = nil) click to toggle source
# File lib/domotics/core/element/switch.rb, line 19
def on(timer = nil)
  set_state :on
  lag(:off, timer)
end
on?() click to toggle source
# File lib/domotics/core/element/switch.rb, line 24
def on?
  state == :on
end
set_state(value) click to toggle source
Calls superclass method Domotics::Core::Element#set_state
# File lib/domotics/core/element/switch.rb, line 15
def set_state(value)
  @initialized ? (super unless state == value) : super
end
toggle(timer = nil) click to toggle source
# File lib/domotics/core/element/switch.rb, line 45
def toggle(timer = nil)
  set_state state == :off ? :on : :off
  lag(:toggle, timer)
end

Private Instance Methods

lag(action = nil, timer = nil, &block) click to toggle source
# File lib/domotics/core/element/switch.rb, line 56
def lag(action = nil, timer = nil, &block)
  # Kill previous action -> out of date
  @lag_lock.synchronize do
    if @lag && @lag.alive?
      @lag.kill
      @lag = nil
    end
    raise ArgumentError unless timer.is_a?(Integer) && (timer >= MINIMUM_LAG)

    # Delayed action
    @lag = Thread.new {
      sleep timer
      block_given? ? (public_send(action) if block.call) : public_send(action)
    }
  end
rescue ArgumentError
  nil
end