Class: Tinkerforge::IPConnection
- Inherits:
-
Object
- Object
- Tinkerforge::IPConnection
- Includes:
- Shared::Logger
- Defined in:
- lib/tinderfridge/ip_connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the host for the IP Connection.
-
#port ⇒ Object
readonly
Returns the port for the IP Connection.
-
#socket ⇒ Object
readonly
Returns the network socket used by the IP Connection.
Instance Method Summary collapse
-
#connect(host, port) ⇒ Object
Creates a TCP/IP connection to the given host and port.
-
#disconnect ⇒ Object
Disconnects the TCP/IP connection.
-
#discover(seconds = nil) ⇒ Object
Returns a Tinkerforge::DeviceCollection with devices discovered for this IP Connection.
-
#inspect ⇒ Object
Returns a programmer-friendly representation of the object.
-
#localhost? ⇒ Boolean
Returns true if connected to localhost, false if connected via the network.
-
#log_path ⇒ Object
Returns the path for the Brick Daemon log file.
-
#log_size ⇒ Object
Returns the size (in bytes) of the Brick Daemon log file.
-
#open_brick_viewer ⇒ Object
(also: #brickv)
On Mac OS, opens a new Brick Viewer, connected to the IP Connection’s host and port.
- #original_connect ⇒ Object
- #original_disconnect ⇒ Object
-
#state ⇒ Object
Returns the state of the IP Connection.
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the host for the IP Connection.
11 12 13 |
# File 'lib/tinderfridge/ip_connection.rb', line 11 def host @host end |
#port ⇒ Object (readonly)
Returns the port for the IP Connection.
14 15 16 |
# File 'lib/tinderfridge/ip_connection.rb', line 14 def port @port end |
#socket ⇒ Object (readonly)
Returns the network socket used by the IP Connection.
17 18 19 |
# File 'lib/tinderfridge/ip_connection.rb', line 17 def socket @socket end |
Instance Method Details
#connect(host, port) ⇒ Object
Creates a TCP/IP connection to the given host and port. Logs events if event logging is enabled.
22 23 24 25 26 27 |
# File 'lib/tinderfridge/ip_connection.rb', line 22 def connect(host, port) logger_debug "Connecting to %s:%s" % [host, port] ts = Time.now.to_f original_connect(host, port) logger_debug "Connected to %s:%s (%5.3fs)" % [host, port, Time.now.to_f - ts] end |
#disconnect ⇒ Object
Disconnects the TCP/IP connection. Logs events if event logging is enabled.
32 33 34 35 36 |
# File 'lib/tinderfridge/ip_connection.rb', line 32 def disconnect logger_debug "Disconnecting from #{host}:#{port}" original_disconnect logger_debug "Disconnected from #{host}:#{port}" end |
#discover(seconds = nil) ⇒ Object
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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tinderfridge/ip_connection.rb', line 107 def discover(seconds=nil) list = Tinkerforge::DeviceCollection.new self.register_callback(CALLBACK_ENUMERATE) do |*args| logger_log_enum(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] end end self.enumerate sleep(seconds.to_f) if seconds list end |
#inspect ⇒ Object
Returns a programmer-friendly representation of the object.
39 40 41 |
# File 'lib/tinderfridge/ip_connection.rb', line 39 def inspect "#{self.class} (%s:%s)" % (host ? [host, port] : ['-', '-'] ) end |
#localhost? ⇒ Boolean
Returns true if connected to localhost, false if connected via the network.
58 59 60 |
# File 'lib/tinderfridge/ip_connection.rb', line 58 def localhost? @localhost ||= %w(localhost 127.0.0.1 ::1).include? host end |
#log_path ⇒ Object
Returns the path for the Brick Daemon log file. Nil if connected to Brickd via the network.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tinderfridge/ip_connection.rb', line 63 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 |
#log_size ⇒ Object
Returns the size (in bytes) of the Brick Daemon log file. Nil if connected to Brickd via the network.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tinderfridge/ip_connection.rb', line 80 def log_size if log_path if File.exist? log_path File.size log_path else 0 end else nil end end |
#open_brick_viewer ⇒ Object Also known as: brickv
On Mac OS, opens a new Brick Viewer, connected to the IP Connection’s host and port.
Not supported on other platforms.
Requires Brick Viewer version 2.4.23 or later.
135 136 137 138 139 140 |
# File 'lib/tinderfridge/ip_connection.rb', line 135 def open_brick_viewer if host and (RUBY_PLATFORM =~ /darwin/) `open -n -a Brickv --args #{host} --port #{port}` "#{host}:#{port}" end end |
#original_connect ⇒ Object
19 |
# File 'lib/tinderfridge/ip_connection.rb', line 19 alias original_connect connect |
#original_disconnect ⇒ Object
29 |
# File 'lib/tinderfridge/ip_connection.rb', line 29 alias original_disconnect disconnect |
#state ⇒ Object
Returns the state of the IP Connection.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tinderfridge/ip_connection.rb', line 44 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 |