class Kinokero::Printer
all printer-specific information & handling mixes in Device
for any superset device common stuff
data structure¶ ↑
relates together:
higher-level linkages (such as to a persistance/recall mechanism) GCP-specific info (such as printer_id, access_tokens, etc) GCP-requesting info for device
Constants
- SAMPLE_GCP_OPTIONS
SAMPLE_GCP_OPTIONS
are indicative of the data furnished by GCP after printer registration; the data here has been sanitized- VALID_GCP_OPTIONS
VALID_GCP_OPTIONS
is used to determine if user options valid if (in future) any default options were to be off-limits, then a specific sets of keys will have to be enumerated below
Attributes
Public Class Methods
new object constructor; any/all args can be missing
-
Args :
-
gcp_info
- hash of on-going gcp required information -
request_info
- hash of info needed for a request -
model_info
- nil or higher-level object itself
-
-
Returns :
-
Printer
object
-
-
Raises : -
# File lib/kinokero/printer.rb, line 35 def initialize( gcp_info={}, model_info=nil ) # TODO: uncomment when Device mix-in is fleshed out # super @model = nil @poll_thread = nil # will be the polling thread object @cloudprint = nil # Proxy fills this in later @was_state = false # previous state, printer not ready @gcp_printer_control = nil # default if empty setup_model( model_info ) setup_gcp( gcp_info ) end
Protected Class Methods
# File lib/kinokero/printer.rb, line 291 def self.print_gcp_registration_info( cups_printer, msg ) # display in log or the SYSOUT Kinokero::Log.debug ("\n------------------------------------------------------------------\n") Kinokero::Log.info( msg ) Kinokero::Log.debug ("\n------------------------------------------------------------------\n") # print out on the new printer command = ( system("which enscript") ? 'enscript -f Helvetica12' : 'lp' ) system("echo '#{msg}' | #{command} -d #{cups_printer}") end
Public Instance Methods
# File lib/kinokero/printer.rb, line 147 def cups_reason( reason ) return ( reason == 'none' ? '' : reason ) end
# File lib/kinokero/printer.rb, line 137 def cups_state_to_sym( state ) case ( state ) when '3' then :idle when '4' then :processing when '5' then :stopped else state.to_sym end # case end
# File lib/kinokero/printer.rb, line 171 def is_printer_ready?() state_hash = Cups.options_for( @gcp_printer_control[:cups_alias] ) return state_hash['printer-is-accepting-jobs'] end
**************************************************************************** PRINTER CONTROL
****************************************************************************
# File lib/kinokero/printer.rb, line 156 def print_file( file ) printer_command = "lp -d #{@gcp_printer_control[:cups_alias]} #{file} " Kinokero::Log.verbose_debug "#{@gcp_printer_control[:gcp_printer_name]}: " + printer_command + "\n" status = system( "#{printer_command}" ) # TODO: figure out what the status means? or get printer status # so we know when the job has successfully printed return status end
setup_gcp
info from hash (possibly empty)
-
Args :
-
gcp_info
- hash of on-going gcp required information
-
-
Returns : -
-
Raises :
-
ArgumentError upon invalid values or keys for gcp_info
-
# File lib/kinokero/printer.rb, line 78 def setup_gcp( gcp_info ) unless gcp_info.empty? validate_gcp_options( gcp_info ) @gcp_printer_control = gcp_info # persist the hash end end
setup_model
info
-
Args :
-
model_info
- some type of model object meaningful to calling appliance
-
-
Returns : -
-
Raises : -
# File lib/kinokero/printer.rb, line 62 def setup_model( model_info ) @model = model_info end
# File lib/kinokero/printer.rb, line 90 def start_poll_thread() if @poll_thread.nil? && @gcp_printer_control[:is_active] @poll_thread = Thread.new do while true # LOOP indefinitely # get current device cups state state_hash = Cups.options_for( @gcp_printer_control[:cups_alias] ) # convert string to boolean for is_accepting jobs is_accepting = (state_hash['printer-is-accepting-jobs'] == 'true') # if different from before if @was_state ^ is_accepting # remember the changed state @was_state = is_accepting # then tell GCP about the change @cloudprint.gcp_ready_state_changed( @was_state, cups_state_to_sym( state_hash['printer-state'] ), cups_reason( state_hash['printer-state-reasons'] ) ) end # go back to sleep again sleep Kinokero.printer_poll_cycle # <<<< SLEEPING HERE <<<<< end end # polling thread; only ends when killed # force abort of everything if exception in thread @poll_thread.abort_on_exception = true end # if no existing poll_thread and this is an active printer end
# File lib/kinokero/printer.rb, line 129 def stop_poll_thread() @poll_thread.kill unless @poll_thread.nil? @poll_thread = nil end
Protected Instance Methods
# File lib/kinokero/printer.rb, line 283 def similar_values?(ref, x) return true if ref.kind_of?( x.class ) || ( (x == true) ^ (x == false) ) return false end
validates gcp options
-
Args :
-
options
- gcp_control as a hash
-
-
Returns : -
-
Raises :
-
ArgumentError if invalid option present
-
# File lib/kinokero/printer.rb, line 234 def validate_gcp_options(options) validate_hash( 'VALIDATE gcp_control', VALID_GCP_OPTIONS, SAMPLE_GCP_OPTIONS, options ) end
validates any hash options against a standard
-
Args :
-
msg
- string to display if exception occurs -
valid_keys
- list of expected hash keys (to look for invalid keys) -
sample
- hash of sample values for all those keys, used to validate options hash values -
options
- hash of values to be used
-
-
Returns : -
-
Raises :
-
ArgumentError if invalid option present
-
# File lib/kinokero/printer.rb, line 263 def validate_hash(msg, valid_keys, sample, options) # options validations: check for invalid keys options.assert_valid_keys(valid_keys) # options validations: check for invalid values valid_keys.each do |key| if options[key].nil? || !similar_values?(options[key], sample[key]) err_msg = "[#{msg}] value for key #{key} should be similar to <#{sample[key]}>, a #{sample[key].class}; " + "instead it was <#{options[key]}>" raise ArgumentError, err_msg end end # do validate each key end