Class: OneWire::Request

Inherits:
Object
  • Object
show all
Includes:
Constants::Functions
Defined in:
lib/one_wire/request.rb

Constant Summary

Constants included from Constants::Functions

Constants::Functions::DIR, Constants::Functions::DIRALL, Constants::Functions::PRESENCE, Constants::Functions::READ, Constants::Functions::WRITE

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



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

def initialize(socket, type, path, options = {})
  version = 0
  payload = path + "\000"
  case type
  when :dir
    request_type, data_size = DIR, 0
  when :dirall
    request_type, data_size = DIRALL, 0
  when :read
    request_type, data_size = READ, 8192
  when :write
    request_type, data_size = WRITE, options[:value].size
    payload << options[:value] << "\000"
  when :presence
    request_type, data_size = PRESENCE, 0
  end
  flags = 0x05000000 | 0x00000002 | Constants::Units.const_get((options[:units] || Config.units).to_s.upcase)
  offset = 0
  header = [ version, payload.size, request_type, flags, data_size, offset ].pack("NNNNNN")

  socket.write(header + payload)
end