Class: LmSensors::Sensor

Inherits:
AbsSensor show all
Defined in:
lib/lmsensors/sensors/sensor.rb

Overview

Sensor is a concrete implementation of the AbsSensor. This implementation is a specific object dedicated to an individual sensor chip.

Instance Attribute Summary collapse

Attributes inherited from AbsSensor

#chip_name, #filters, #fmap, #fmt, #subs

Instance Method Summary collapse

Methods inherited from AbsSensor

#initialize, #name, #reset_fmap, #set_filters, #set_fmap, #toggle_fmt, #toggle_subs, #unset_filters

Constructor Details

This class inherits a constructor from LmSensors::AbsSensor

Instance Attribute Details

#adapterObject (readonly)

Sensor :adapter is the adapter type, like the PCI or ISA adapter



19
20
21
# File 'lib/lmsensors/sensors/sensor.rb', line 19

def adapter
  @adapter
end

#pathObject (readonly)

Sensor :path is the ‘/sys/class/hwmon/’ path for this specific sensor



17
18
19
# File 'lib/lmsensors/sensors/sensor.rb', line 17

def path
  @path
end

Instance Method Details

#countObject

Get the number of features available for the current name (or total features to be read for all sensors). This overrides the abstract “.count“ method.



110
111
112
113
114
115
# File 'lib/lmsensors/sensors/sensor.rb', line 110

def count
  data = read
  if !data then return nil end
  # If the data is good
  data.count
end

#count_sfObject

Get the number of subfeatures available for the current name (or total features to be read for all sensors).



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lmsensors/sensors/sensor.rb', line 121

def count_sf
  total = 0 # Accumulator
  data = read
  if !data then return nil end
  # If the data is good, continue
  data.each do |_, v|
    # Ignore the :type symbol
    total += (v.select { |item| :type != item } .count)
  end
  total # Return accumulator
end

#enum(*args) ⇒ Object

Pseudo-alias abstract enum to stat, for this class



98
# File 'lib/lmsensors/sensors/sensor.rb', line 98

def enum(*args) stat end

#infoObject

Return the core chip data. This does NOT include the features and subfeatures, just the information on the chip itself.



61
# File 'lib/lmsensors/sensors/sensor.rb', line 61

def info() { name: name, adapter: @adapter, path: @path, } end

#locate(path) ⇒ Object

Find a device sensors by its path, in such a case where the app already has a device from sysfs or similar and needs to attach them together.

Sets the name of the sensor, as well, so this should be used as an instance.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lmsensors/sensors/sensor.rb', line 28

def locate(path)
  # Validate the path
  dir = path_valid?(path)
  if !dir then return nil end
  # Determine if it's a valid sensor, since the
  # sensors here use the path as the index.
  # If so, set it as this object's name
  raw = pro_enum(nil, nil)
  # Check if the enum value is valid
  if (![Hash, Array].include?(raw.class)) then
    STDERR.puts raw
    return nil
  end # End enum check
  raw.then do |sensors|
    # Return early, if it's not in the keys
    if !sensors.keys.include?(dir) then
      STDERR.puts "Path does not have an associated sensor"
      return nil
    end # End check for valid sensor path
    
    # Set the data and return the chip name
    data = sensors[dir]
    @chip_name = data[:name]
    @adapter = data[:adapter]
    @path = data[:path]
    @chip_name
  end # End enumeration handler
end

#readObject

Raw chip data – this is unfiltered and unformatted.

This has replaced the previous version of :stat, as :stat unintentionally did not respect the @filters.



138
139
140
141
142
143
144
145
146
# File 'lib/lmsensors/sensors/sensor.rb', line 138

def read
  c = pro_enum(true, @chip_name)
  if Hash === c then
    c[@path][:stat]
  else
    STDERR.puts c
    return nil
  end
end

#statObject Also known as: features

Stat will return the values and names of all the features associated with the currently-selected chip. This has replaced :features, which is now just an alias for :stat.

2021-May-31 (pre-release): Now respects @filters



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lmsensors/sensors/sensor.rb', line 70

def stat
  # For each chip, split it by key and value
  raw = read
  # Check that the data is still valid
  if raw.nil? then return nil end
  data = raw.collect do |feature, keys|
    # Is the filter set?
    no_filter = @filters.empty?
    # Is the feature of a type included in the filter?
    included = @filters.include?(keys[:type])
    
    # If the filter is NOT set, OR the keys IS included
    # in the filter, proceed.
    if no_filter or included then
      # If the subfeature option is set, return
      # the subfeature data and the unit.
      if @subs then
        @fmap.call(feature, keys)
      # If the subfeature option is not set, just return
      # the feature type.
      else { feature => keys[:type] } end
    # If filtered and not included, return empty array
    else [] end
  end.flatten # Remove any empty units
  # If format is set, format the output
  if (@fmt and @subs) then data.collect { |item| item.fmt } else data end
end