class Tinkerforge::IPConnection
Attributes
Returns the host for the IP Connection.
Returns the port for the IP Connection.
Public Instance Methods
Returns a Tinkerforge::DeviceCollection
with devices discovered for this IP Connection. Discovery may take a few moments.
Accepts an optional argument for the number of seconds to wait, otherwise returns immediately.
A good idea is to store the result in a variable, which will then be filled with devices as they are found. @example Using Tinkerforge.connect
my_devices = Tinkerforge.connect.discover
@example Wait 1 second
Tinkerforge.connect.discover(1).ls
@example Classic
ipcon = Tinkerforge::IPConnection.new ipcon.connect 'localhost', 4223 my_devices = ipcon.discover
# File lib/tinderfridge/ip_connection.rb, line 82 def discover(seconds=nil) list = Tinkerforge::DeviceCollection.new self.register_callback(CALLBACK_ENUMERATE) do |*args| case args[6] when 0, 1 unless list.key?(args[0]) if dev = device_instance_from_enum_data(args) list[args[0]] = dev end end when 2 list.delete args[0] else raise "Unknown Enumeration Type: #{args[6]}" end end self.enumerate sleep(seconds.to_f) if seconds list end
Returns a programmer-friendly representation of the object.
# File lib/tinderfridge/ip_connection.rb, line 14 def inspect "%s (%s:%s)" % [self.class, host, port] end
Returns true if connected to localhost, false if connected via the network.
# File lib/tinderfridge/ip_connection.rb, line 33 def localhost? @localhost ||= %w(localhost 127.0.0.1 ::1).include? host end
Returns the path for the Brick Daemon log file. Nil if connected to Brickd via the network.
# File lib/tinderfridge/ip_connection.rb, line 38 def log_path unless defined? @local_log_path @local_log_path = if localhost? if Gem.win_platform? 'C:\ProgramData\Tinkerforge\Brickd\brickd.log' else '/var/log/brickd.log' end else nil end end @local_log_path end
Returns the size (in bytes) of the Brick Daemon log file. Nil if connected to Brickd via the network.
# File lib/tinderfridge/ip_connection.rb, line 55 def log_size if log_path if File.exist? log_path File.size log_path else 0 end else nil end end
Returns the state of the IP Connection.
# File lib/tinderfridge/ip_connection.rb, line 19 def state { 'host' => host, 'port' => port, 'update_time' => Time.now.gmtime, 'connection_state' => get_connection_state, 'auto_reconnect' => get_auto_reconnect, 'timeout' => get_timeout, 'log_path' => log_path, 'log_size' => log_size, } end
Private Instance Methods
Takes the args supplied by an enumeration callback, and returns a device instance.
# File lib/tinderfridge/ip_connection.rb, line 108 def device_instance_from_enum_data(enum_data) if dev_info = Tinkerforge.device_info(enum_data[5]) require "tinkerforge/#{dev_info[2][1]}" Tinkerforge.const_get(dev_info[2][0]).new enum_data[0], self else warn "Unknown Device Identifier: #{enum_data[5]} (UID: #{enum_data[0]})" nil end end