class Array

Searches inside binfile for Homie-patterns and returns a string with firmware-brand, name or version.

Extends Array class with some specific selection-methods for devices and firmware

Public Instance Methods

create_output_table(attribs, _style) click to toggle source

Creates an array of rows with desired output from HomiePair-objects.

# File lib/hodmin/hodmin_tools.rb, line 292
def create_output_table(attribs, _style)
  pastel = Pastel.new
  empty_field = ' '
  empty_field = configatron.output.nil.strip unless configatron.output.nil? || configatron.output.nil.nil?
  empty_field = pastel.dim(empty_field) # dim this message
  rows = []
  each do |r|
    row = []
    # color for checksum if applicable:
    checksum_color = if r.hdev.nil?
                       'none'
                     else
                       r.hdev.upgradable ? 'yellow' : 'green'
                     end
    attribs.each do |a|
      row << case a.slice(0, 2)
             when 'HD' then
               var_name = a.gsub(/HD./, '')
               var = r.hdev.nil? ? empty_field : r.hdev.instance_variable_get("@#{a.gsub(/HD./, '')}")
               case var_name
               when 'online'
                 var = pastel.green(var) if var == 'true'
                 var = pastel.red(var) if var == 'false'
                 var if var != 'true' && var != 'false'
               else
                 var = var.nil? ? empty_field : var
               end
             when 'FW' then
               if r.hfw.nil?
                 empty_field
               else
                 var_name = a.gsub(/FW./, '')
                 var = r.hfw.instance_variable_get("@#{var_name}")
                 case var_name
                 when 'checksum'
                   case checksum_color
                   when 'none' then var
                   when 'green' then pastel.green(var)
                   when 'yellow' then pastel.yellow(var)
                   end
                 else
                   var.nil? ? empty_field : var
                 end
               end
             when 'AD' then
               var = r.instance_variable_get("@#{a.gsub(/AD./, '')}")
               var.nil? ? empty_field : var
             end
    end
    rows << row
  end
  rows
end
find_pattern(binfile) click to toggle source
# File lib/hodmin/hodmin_tools.rb, line 60
def find_pattern(binfile)
  result = ''
  if binfile.include?(first)
    result = binfile.split(first)[1].split(last).first.to_s
    result = [result].pack('H*') unless result.empty?
  end
  result
end
select_by_opts(options) click to toggle source

Selects Array of HomieDevices or firmwares based on options

# File lib/hodmin/hodmin_tools.rb, line 267
def select_by_opts(options)
  this_object = first.class == HomieDevice ? 'HD' : 'FW'

  # Options valid for selecting Homie-Devices OR for firmwares
  valid_dev_options =
    this_object == 'HD' ? [:mac, :fw_name, :checksum, :localip] : [:checksum, :fw_name, :config]

  # use only valid options:
  my_opts = options.select { |k, _v| valid_dev_options.include?(k) }

  # remove all options not used as CLI argument:
  my_opts = my_opts.select { |_k, v| !v.to_s.empty? }
  return self if my_opts.empty? # no options set, so all devices are selected

  my_devs = self
  # selects objects (devices or firmwares) from an array due to a filter defined by key-value-pair
  # Example: [:checksum => 'c79*']
  my_opts.each_pair do |k, v|
    # puts "looking for #{k} = #{v}"
    my_devs = my_devs.select { |h| SelectObject.new(v) =~ h.instance_variable_get("@#{k}") }
  end
  my_devs
end