Class: Domoticz::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/domoticz/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/domoticz/device.rb', line 30

def method_missing(method_sym, *arguments, &block)
  hash = Hash[@data.map { |k, v| [k.downcase, v] }]
  key = method_sym.to_s.downcase

  if hash.has_key?(key)
    hash[key]
  else
    super
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/domoticz/device.rb', line 4

def data
  @data
end

#idxObject

Returns the value of attribute idx.



3
4
5
# File 'lib/domoticz/device.rb', line 3

def idx
  @idx
end

Class Method Details

.allObject



45
46
47
48
49
# File 'lib/domoticz/device.rb', line 45

def self.all
  Domoticz.perform_api_request("type=devices&filter=all&used=true")["result"].map do |json|
    Device.new_from_json(json)
  end
end

.find_by_id(id) ⇒ Object



41
42
43
# File 'lib/domoticz/device.rb', line 41

def self.find_by_id(id)
  all.find { |d| d.idx == id.to_s }
end

.new_from_json(json) ⇒ Object



51
52
53
54
55
56
# File 'lib/domoticz/device.rb', line 51

def self.new_from_json(json)
  device = self.new
  device.data = json
  device.idx = json["idx"]
  device
end

Instance Method Details

#dimmer?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/domoticz/device.rb', line 26

def dimmer?
  isDimmer
end

#off!Object



14
15
16
# File 'lib/domoticz/device.rb', line 14

def off!
  Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=Off")
end

#on!Object



10
11
12
# File 'lib/domoticz/device.rb', line 10

def on!
  Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=On")
end

#seconds_since_updateObject



6
7
8
# File 'lib/domoticz/device.rb', line 6

def seconds_since_update
  Time.now - Time.parse(lastupdate)
end

#temperatureObject



22
23
24
# File 'lib/domoticz/device.rb', line 22

def temperature
  temp
end

#toggle!Object



18
19
20
# File 'lib/domoticz/device.rb', line 18

def toggle!
  Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=Toggle")
end