Class: Kinokero::Printer

Inherits:
Object show all
Includes:
Device
Defined in:
lib/kinokero/printer.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gcp_info = {}, model_info = nil) ⇒ Printer

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 : -



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#cloudprintObject

cloudprint object for this device



20
21
22
# File 'lib/kinokero/printer.rb', line 20

def cloudprint
  @cloudprint
end

#gcp_printer_controlObject (readonly)

Returns the value of attribute gcp_printer_control.



19
20
21
# File 'lib/kinokero/printer.rb', line 19

def gcp_printer_control
  @gcp_printer_control
end

#modelObject (readonly)

Returns the value of attribute model.



19
20
21
# File 'lib/kinokero/printer.rb', line 19

def model
  @model
end

#poll_threadObject

cloudprint object for this device



20
21
22
# File 'lib/kinokero/printer.rb', line 20

def poll_thread
  @poll_thread
end

#was_stateObject (readonly)

Returns the value of attribute was_state.



19
20
21
# File 'lib/kinokero/printer.rb', line 19

def was_state
  @was_state
end

Instance Method Details

#cups_reason(reason) ⇒ Object



147
148
149
# File 'lib/kinokero/printer.rb', line 147

def cups_reason( reason )
  return ( reason == 'none' ?  ''  :  reason )
end

#cups_state_to_sym(state) ⇒ Object



137
138
139
140
141
142
143
144
145
# 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?Boolean


Returns:

  • (Boolean)


171
172
173
174
# 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

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



156
157
158
159
160
161
162
163
164
165
166
167
# 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(gcp_info) ⇒ Object

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



78
79
80
81
82
83
# 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) ⇒ Object

setup_model info

  • Args :

    • model_info - some type of model object meaningful to calling appliance

  • Returns : -

  • Raises : -



62
63
64
# File 'lib/kinokero/printer.rb', line 62

def setup_model( model_info )
  @model = model_info
end

#start_poll_threadObject




90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_threadObject



129
130
131
132
# File 'lib/kinokero/printer.rb', line 129

def stop_poll_thread()
  @poll_thread.kill  unless @poll_thread.nil?
  @poll_thread = nil
end