class ForestClient::Printer
Public Class Methods
new(ip)
click to toggle source
Create a new instance of Printer
by passing its IP address
# File lib/forest-client/printer.rb, line 8 def initialize(ip) @ip = ip end
Public Instance Methods
get_consumable(id)
click to toggle source
# File lib/forest-client/printer.rb, line 122 def get_consumable(id) color = snmp_get("1.3.6.1.2.1.43.12.1.1.4.1.#{id}").to_s level = Integer(snmp_get("1.3.6.1.2.1.43.11.1.1.9.1.#{id}")) capacity = Integer(snmp_get("1.3.6.1.2.1.43.11.1.1.8.1.#{id}")) percentage = Float(level) * 100 / Float(capacity) return {:color => color, :level => level, :capacity => capacity, :percentage => percentage} end
get_consumable_names()
click to toggle source
# File lib/forest-client/printer.rb, line 133 def get_consumable_names return snmp_walk('1.3.6.1.2.1.43.11.1.1.6.1').each { |item| item.chop! } end
get_consumables()
click to toggle source
# File lib/forest-client/printer.rb, line 137 def get_consumables consumables = get_consumable_names ret = Array.new consumables.length.times do |id| cons = get_consumable(id + 1) cons[:name] = consumables.at(id) ret.push(cons) end return ret end
get_device(id)
click to toggle source
Returns the name and status of the specified device as a hash
# File lib/forest-client/printer.rb, line 87 def get_device(id) # Get the device name name = snmp_get("1.3.6.1.2.1.25.3.2.1.3.#{id}") # Make the status human-readable case snmp_get("1.3.6.1.2.1.25.3.2.1.5.#{id}") when 1 status = 'Unknown' when 2 status = 'Running' when 3 status = 'Warning' when 4 status = 'Testing' when 5 status = 'Down' else status = 'Unknown' end return {:name => name, :status => status} end
get_device_ids()
click to toggle source
Returns an array of all device IDs
# File lib/forest-client/printer.rb, line 82 def get_device_ids return snmp_walk('1.3.6.1.2.1.25.3.2.1.1') end
get_devices()
click to toggle source
Returns an array of the results of get_device_status for all of the printer's devices
# File lib/forest-client/printer.rb, line 113 def get_devices devices = get_device_ids ret = Array.new devices.each do |device| ret.push(get_device(device)) end return ret end
get_display()
click to toggle source
Query the printer for its display
# File lib/forest-client/printer.rb, line 59 def get_display return snmp_walk('1.3.6.1.2.1.43.16.5.1.2.1').at(0) end
get_ip()
click to toggle source
Return the IP address of the printer
# File lib/forest-client/printer.rb, line 13 def get_ip return @ip end
get_location()
click to toggle source
# File lib/forest-client/printer.rb, line 199 def get_location return snmp_get('1.3.6.1.2.1.1.6.0') end
get_messages()
click to toggle source
Query the printer for its message
# File lib/forest-client/printer.rb, line 49 def get_messages return snmp_walk('1.3.6.1.2.1.43.18.1.1.8').at(0) end
get_model()
click to toggle source
Query the printer for its model number
# File lib/forest-client/printer.rb, line 39 def get_model return snmp_get('1.3.6.1.2.1.25.3.2.1.3.1') end
get_name()
click to toggle source
# File lib/forest-client/printer.rb, line 203 def get_name return snmp_get('1.3.6.1.2.1.1.5.0') end
get_page_count()
click to toggle source
Query the printer for its pagecount
# File lib/forest-client/printer.rb, line 54 def get_page_count return Integer(snmp_get('1.3.6.1.2.1.43.10.2.1.4.1.1')) end
get_serial()
click to toggle source
Query the printer for its serial
# File lib/forest-client/printer.rb, line 44 def get_serial return snmp_walk('1.3.6.1.2.1.43.5.1.1.17').at(0) end
get_status()
click to toggle source
Query the printer for its status The printer will return an integer which we convert into a meaninful string
# File lib/forest-client/printer.rb, line 66 def get_status case snmp_get('1.3.6.1.2.1.25.3.5.1.1.1') when 1 return 'Other' when 3 return 'Idle' when 4 return 'Printing' when 5 return 'Warmup' else return 'Unknown' end end
get_trays()
click to toggle source
# File lib/forest-client/printer.rb, line 151 def get_trays trays = Array.new snmp_walk('1.3.6.1.2.1.43.8.2.1.10.1').length.times do |id| tray = id + 1 name = snmp_get("1.3.6.1.2.1.43.8.2.1.13.1.#{tray}") rem = snmp_get("1.3.6.1.2.1.43.8.2.1.10.1.#{tray}") case rem when -3 status = 'OK' when -2 status = 'Unknown' when 0 status = 'Empty' else status = "#{rem} sheets remaining" end feed_dim = snmp_get("1.3.6.1.2.1.43.8.2.1.4.1.#{tray}") xfeed_dim = snmp_get("1.3.6.1.2.1.43.8.2.1.5.1.#{tray}") dim_units = snmp_get("1.3.6.1.2.1.43.8.2.1.3.1.#{tray}") if feed_dim.respond_to?(:to_i) && xfeed_dim.respond_to?(:to_i) && dim_units.respond_to?(:to_i) if feed_dim > 0 && xfeed_dim > 0 if dim_units.to_i == 3 feed_dim = Float(feed_dim) / 10000 xfeed_dim = Float(xfeed_dim) / 10000 elsif dim_units.to_i == 4 feed_dim = Float(feed_dim) * 0.0000393700787 xfeed_dim = Float(xfeed_dim) * 0.0000393700787 end end end capacity = snmp_get("1.3.6.1.2.1.43.8.2.1.9.1.#{tray}") if capacity.respond_to?(:to_i) capacity = capacity.to_i else capacity = 0 end trays.push({:name => name, :status => status, :y => feed_dim, :x => xfeed_dim, :capacity => capacity}) end return trays end
Private Instance Methods
snmp_get(oid)
click to toggle source
Get the value of an SNMP OID
# File lib/forest-client/printer.rb, line 18 def snmp_get(oid) SNMP::Manager.open(:host => @ip) do |manager| query = SNMP::ObjectId.new(oid) response = manager.get(query) response.each_varbind do |res| return res.value end end end
snmp_walk(oid)
click to toggle source
Walk the SNMP tree starting at the given OID
# File lib/forest-client/printer.rb, line 29 def snmp_walk(oid) SNMP::Manager.open(:host => @ip) do |manager| query = SNMP::ObjectId.new(oid) ret = Array.new manager.walk(oid) { |vb| ret.push(vb.value) } return ret end end