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

cloudprint[RW]
gcp_printer_control[R]
model[R]
poll_thread[RW]
was_state[R]

Public Class Methods

new( gcp_info={}, model_info=nil ) click to toggle source

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 :

  • 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

print_gcp_registration_info( cups_printer, msg ) click to toggle source

Public Instance Methods

cups_reason( reason ) click to toggle source
# File lib/kinokero/printer.rb, line 147
def cups_reason( reason )
  return ( reason == 'none' ?  ''  :  reason )
end
cups_state_to_sym( state ) click to toggle source

# 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
is_printer_ready?() click to toggle source

# 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
print_file( file ) click to toggle source

**************************************************************************** PRINTER CONTROL

****************************************************************************

setup_gcp( gcp_info ) click to toggle source

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( model_info ) click to toggle source

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
start_poll_thread() click to toggle source

# 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
stop_poll_thread() click to toggle source
# 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

similar_values?(ref, x) click to toggle source

# 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
validate_gcp_options(options) click to toggle source

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
validate_hash(msg, valid_keys, sample, options) click to toggle source

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