Module: Winker::Devices::DeviceMethods

Included in:
Winker::Device
Defined in:
lib/winker/devices/device_methods.rb

Constant Summary collapse

VALID_TYPES =

hub must always be last since the hub_id is listed with every device

%w{
  sensor_pod
  light_bulb
  unknown_device
  eggtray
  hub
}
WAIT_BLOCK_SLEEP =
1
WAIT_BLOCK_TIMEOUT =
20.seconds

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



18
19
20
# File 'lib/winker/devices/device_methods.rb', line 18

def id
  @id
end

#obj_dataObject

Returns the value of attribute obj_data.



18
19
20
# File 'lib/winker/devices/device_methods.rb', line 18

def obj_data
  @obj_data
end

#typeObject

Returns the value of attribute type.



18
19
20
# File 'lib/winker/devices/device_methods.rb', line 18

def type
  @type
end

#updated_atObject

Returns the value of attribute updated_at.



18
19
20
# File 'lib/winker/devices/device_methods.rb', line 18

def updated_at
  @updated_at
end

Instance Method Details

#find_type_and_idObject



20
21
22
23
24
# File 'lib/winker/devices/device_methods.rb', line 20

def find_type_and_id
  @type = VALID_TYPES.find{|t| @obj_data.singleton_methods.map(&:to_s).include?("#{t}_id")}
  @id = @obj_data.send("#{@type}_id")
  @updated_at = 2.weeks.ago
end

#wait_for_update(boolean_proc, options = {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/winker/devices/device_methods.rb', line 26

def wait_for_update(boolean_proc, options = {}, &block)
  if Winker.wait_for_updates
    options = {
      timeout: WAIT_BLOCK_TIMEOUT
    }.merge(options)
    start = Time.now
    until (success = boolean_proc.call) || Time.now > start+options[:timeout]
      result = yield
      sleep WAIT_BLOCK_SLEEP
    end
    if success
      return result
    else
      raise "Device #{self.type} #{self.name} not updated and timed out after #{options[:timeout]}."
    end
  end
end