Class: RZWaveWay::ZWay

Inherits:
Object
  • Object
show all
Includes:
Log4r, Singleton
Defined in:
lib/rzwaveway/zway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZWay

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

#devicesObject (readonly)

Returns the value of attribute devices.



12
13
14
# File 'lib/rzwaveway/zway.rb', line 12

def devices
  @devices
end

#logObject (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

Raises:

  • (ArgumentError)


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

#inspectObject



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

#processObject



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(options, *adapter_params)
  hostname = options[:hostname] || '127.0.0.1'
  port = options[:port] || 8083
  username = options[:username] || 'admin'
  password = options[: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 = options[:logger] if options.has_key? :logger
end

#startObject



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_sObject



99
100
101
# File 'lib/rzwaveway/zway.rb', line 99

def to_s
  "ZWay at '#{@base_uri}' with #{devices.count} device(s)"
end