Class: Domotics::Core::Room
Direct Known Subclasses
Constant Summary collapse
- @@rooms =
All rooms
Hash.new
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.[](symbol = nil) ⇒ Object
Return requested room like element of array.
Instance Method Summary collapse
-
#[](symbol = nil) ⇒ Object
Return element object.
- #destroy ⇒ Object
-
#event_handler(msg = {}) ⇒ Object
Default - simple prints event.
-
#initialize(args = {}) ⇒ Room
constructor
A new instance of Room.
-
#light(action = :toggle) ⇒ Object
Perform action with light.
- #light_off? ⇒ Boolean
-
#method_missing(symbol, *args) ⇒ Object
Return requested room like variable.
-
#notify(msg) ⇒ Object
Method for pushing into queue.
-
#register_element(element, name) ⇒ Object
Register element.
- #state ⇒ Object
- #to_s ⇒ Object
-
#verbose_state ⇒ Object
Return state of all elements.
Constructor Details
#initialize(args = {}) ⇒ Room
Returns a new instance of Room.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/domotics/core/room.rb', line 6 def initialize(args = {}) @logger = args[:logger] || Logger.new(STDERR) # Save self @@rooms[@name = args[:name]] = self @type = args[:type] # Hash of elements @elements = {} class << @elements def light select { |name, element| (element.is_a? Element) and (element.type == :switch) and (name =~ /light/) } end end # New queue thread @room_queue = Queue.new @queue_thread = Thread.new { loop { event_handler @room_queue.pop } } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
5 6 7 |
# File 'lib/domotics/core/room.rb', line 5 def elements @elements end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/domotics/core/room.rb', line 5 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/domotics/core/room.rb', line 5 def type @type end |
Class Method Details
.[](symbol = nil) ⇒ Object
Return requested room like element of array
74 75 76 77 |
# File 'lib/domotics/core/room.rb', line 74 def self.[](symbol = nil) return @@rooms[symbol] if symbol @@rooms end |
Instance Method Details
#[](symbol = nil) ⇒ Object
Return element object
69 70 71 72 |
# File 'lib/domotics/core/room.rb', line 69 def [](symbol = nil) return @elements[symbol] if symbol @elements end |
#destroy ⇒ Object
64 65 66 |
# File 'lib/domotics/core/room.rb', line 64 def destroy @queue_thread.exit end |
#event_handler(msg = {}) ⇒ Object
Default - simple prints event
58 59 60 61 62 |
# File 'lib/domotics/core/room.rb', line 58 def event_handler(msg = {}) event, element = msg[:event], msg[:element] @logger.info { "[Domotics] event [#{event}] element [#{element}] state [#{element.state}]" } Domotics::Core::WsServer.publish "#{element.room.name}/#{element.name}" end |
#light(action = :toggle) ⇒ Object
Perform action with light
42 43 44 45 46 47 48 49 |
# File 'lib/domotics/core/room.rb', line 42 def light(action = :toggle) case action when :on, :off @elements.light.values.each { |element| element.public_send(action) if element.respond_to? action } when :toggle light_off? ? light(:on) : light(:off) end end |
#light_off? ⇒ Boolean
50 51 52 |
# File 'lib/domotics/core/room.rb', line 50 def light_off? @elements.light.values.reduce(true) { |res, el| res && el.off? } end |
#notify(msg) ⇒ Object
Method for pushing into queue
54 55 56 |
# File 'lib/domotics/core/room.rb', line 54 def notify(msg) @room_queue.push(msg) end |
#register_element(element, name) ⇒ Object
Register element
23 24 25 26 27 |
# File 'lib/domotics/core/room.rb', line 23 def register_element(element, name) @elements[name] = element # define accessor method (singleton) instance_eval(%(def #{name}; @elements[:#{name}]; end;), __FILE__, __LINE__) unless respond_to? name end |
#state ⇒ Object
38 39 40 |
# File 'lib/domotics/core/room.rb', line 38 def state nil end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/domotics/core/room.rb', line 83 def to_s "Room[#{@name}](id:#{__id__})" end |
#verbose_state ⇒ Object
Return state of all elements
29 30 31 32 33 34 35 36 37 |
# File 'lib/domotics/core/room.rb', line 29 def verbose_state { @name => { :elements => @elements.inject(Hash.new) { |hash, element| hash.merge element[1].verbose_state[@name][:elements] }, :state => state, :info => (info if respond_to? :info) } } end |