Class: Brewby::Inputs::DS18B20

Inherits:
Object
  • Object
show all
Defined in:
lib/brewby/inputs/ds18b20.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DS18B20

Returns a new instance of DS18B20.



5
6
7
8
9
# File 'lib/brewby/inputs/ds18b20.rb', line 5

def initialize options = {}
  @name = options[:name]
  @device_path = options[:device_path] || "/sys/bus/w1/devices"
  @hardware_id = options[:hardware_id] || find_hardware_id
end

Instance Attribute Details

#device_pathObject

Returns the value of attribute device_path.



4
5
6
# File 'lib/brewby/inputs/ds18b20.rb', line 4

def device_path
  @device_path
end

#hardware_idObject

Returns the value of attribute hardware_id.



4
5
6
# File 'lib/brewby/inputs/ds18b20.rb', line 4

def hardware_id
  @hardware_id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/brewby/inputs/ds18b20.rb', line 4

def name
  @name
end

Instance Method Details

#device_fileObject



45
46
47
# File 'lib/brewby/inputs/ds18b20.rb', line 45

def device_file
  "/sys/bus/w1/devices/#{@hardware_id}/w1_slave"
end

#find_hardware_idObject



11
12
13
# File 'lib/brewby/inputs/ds18b20.rb', line 11

def find_hardware_id
  w1_devices[0].gsub("#{device_path}/",'')
end

#parse(raw_data) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/brewby/inputs/ds18b20.rb', line 29

def parse raw_data
  if temp_data = raw_data.match(/t=([0-9]+)/)
    temp_data[1].to_f / 1000
  else
    nil
  end
end

#readObject



19
20
21
22
23
24
25
26
27
# File 'lib/brewby/inputs/ds18b20.rb', line 19

def read
  raw = read_raw

  if tempC = parse(raw)
    tempF = to_fahrenheit tempC
  else
    tempC
  end
end

#read_rawObject



41
42
43
# File 'lib/brewby/inputs/ds18b20.rb', line 41

def read_raw
  File.read device_file
end

#to_fahrenheit(temp) ⇒ Object



37
38
39
# File 'lib/brewby/inputs/ds18b20.rb', line 37

def to_fahrenheit temp
  ((temp * 1.8) + 32).round(3)
end

#w1_devicesObject



15
16
17
# File 'lib/brewby/inputs/ds18b20.rb', line 15

def w1_devices
  Dir["#{device_path}/28*"]
end