Class: RZWaveWay::ZWay
- Inherits:
-
Object
- Object
- RZWaveWay::ZWay
- Includes:
- Log4r, Singleton
- Defined in:
- lib/rzwaveway/zway.rb
Instance Attribute Summary collapse
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
- #execute(device_id, command_class, command_class_function, argument = nil) ⇒ Object
- #find_extension(name, device_id) ⇒ Object
-
#initialize ⇒ ZWay
constructor
A new instance of ZWay.
- #inspect ⇒ Object
- #on_event(event, &listener) ⇒ Object
- #process ⇒ Object
- #run_zway_no_operation(device_id) ⇒ Object
- #setup(options, *adapter_params) ⇒ Object
- #start ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ ZWay
Returns a new instance of ZWay.
15 16 17 18 19 20 |
# File 'lib/rzwaveway/zway.rb', line 15 def initialize @devices = {} @event_handlers = {} @log = default_logger @update_time = '0' end |
Instance Attribute Details
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
12 13 14 |
# File 'lib/rzwaveway/zway.rb', line 12 def devices @devices end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
13 14 15 |
# File 'lib/rzwaveway/zway.rb', line 13 def log @log end |
Instance Method Details
#execute(device_id, command_class, command_class_function, argument = nil) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/rzwaveway/zway.rb', line 22 def execute(device_id, command_class, command_class_function, argument = nil) raise "No device with id '#{device_id}'" unless @devices.has_key?(device_id) raise "Device with id '#{device_id}' does not support command class '#{command_class}'" unless @devices[device_id].support_commandclass?(command_class) function_name = command_class_function.to_s run_zway_function(device_id, command_class, function_name, argument) end |
#find_extension(name, device_id) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/rzwaveway/zway.rb', line 29 def find_extension(name, device_id) device = @devices[device_id.to_i] raise ArgumentError, "No device with id '#{device_id}'" unless device clazz = qualified_const_get "RZWaveWay::Extensions::#{name}" clazz.new(device) end |
#inspect ⇒ Object
36 37 38 39 40 |
# File 'lib/rzwaveway/zway.rb', line 36 def inspect content = to_s devices.values.each {|device| content << "\n#{device}"} content end |
#on_event(event, &listener) ⇒ Object
73 74 75 |
# File 'lib/rzwaveway/zway.rb', line 73 def on_event(event, &listener) @event_handlers[event] = listener end |
#process ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rzwaveway/zway.rb', line 77 def process updates = get_zway_data_tree_updates updates_per_device = group_per_device updates events = [] devices.each do |device_id, device| previous_status = device.status if updates_per_device.has_key? device_id device_updates = updates_per_device[device_id] device.process(device_updates) do |event| events << event end end if previous_status != device.update_status events << create_status_event_for(device) end end deliver_to_handlers(events) end |
#run_zway_no_operation(device_id) ⇒ Object
42 43 44 |
# File 'lib/rzwaveway/zway.rb', line 42 def run_zway_no_operation device_id run_zway "devices[#{device_id}].SendNoOperation()" end |
#setup(options, *adapter_params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rzwaveway/zway.rb', line 46 def setup(, *adapter_params) hostname = [:hostname] || '127.0.0.1' port = [:port] || 8083 username = [:username] || 'admin' password = [:password] || 'changeme' adapter_params = :httpclient if adapter_params.compact.empty? @base_uri="http://#{hostname}:#{port}" @connection = Faraday.new do |connection| connection.basic_auth username, password connection.adapter *adapter_params end @log = [:logger] if .has_key? :logger end |
#start ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rzwaveway/zway.rb', line 60 def start loop do results = get_zway_data_tree_updates if results.has_key?('devices') results['devices'].each {|device_id,device_data_tree| create_device(device_id.to_i, device_data_tree)} break else sleep 1.0 log.warn 'No devices found at start-up, retrying' end end end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/rzwaveway/zway.rb', line 99 def to_s "ZWay at '#{@base_uri}' with #{devices.count} device(s)" end |