Class: OneWire::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/one_wire/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, path, options = {}) ⇒ Transaction

Returns a new instance of Transaction.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/one_wire/transaction.rb', line 7

def initialize(type, path, options = {})
  host = options[:host] || OneWire::Config.host
  port = options[:port] || OneWire::Config.port
  socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  socket.connect(Socket.pack_sockaddr_in(port, host))
  OneWire::Request.new(socket, type, path, options)
  if type == :dir
    @response = []
    while true do
      response = OneWire::Response.new(socket)
      response.data ? @response << response : break
    end
  else
    @response = OneWire::Response.new(socket)
  end
ensure
  socket.close if socket && !socket.closed?
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/one_wire/transaction.rb', line 5

def response
  @response
end

Class Method Details

.ping(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/one_wire/transaction.rb', line 37

def ping(options = {})
  host = options[:host] || OneWire::Config.host
  port = options[:port] || OneWire::Config.port
  socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  socket.connect(Socket.pack_sockaddr_in(port, host))
  return true
rescue SystemCallError
  return false
ensure
  socket.close if socket && !socket.closed?
end

.write(path, value, options = {}) ⇒ Object



33
34
35
# File 'lib/one_wire/transaction.rb', line 33

def write(path, value, options = {})
  new(:write, path, options.merge(:value => value))
end