kinokero 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +49 -0
  7. data/Gemfile_mock +8 -0
  8. data/LICENSE +20 -0
  9. data/README.md +955 -0
  10. data/Rakefile +1 -0
  11. data/console/.ruby-gemset +1 -0
  12. data/console/.ruby-version +1 -0
  13. data/console/Gemfile +6 -0
  14. data/console/Gemfile.lock +63 -0
  15. data/console/README.md +34 -0
  16. data/console/config/application_configuration.rb +39 -0
  17. data/console/config/gcp_seed.yml +73 -0
  18. data/console/config/kinokero_initializer_template.rb +108 -0
  19. data/console/console +2 -0
  20. data/console/irb_console +2 -0
  21. data/console/lib/appliance_common.rb +73 -0
  22. data/console/twiga.rb +579 -0
  23. data/hp-check.log +244 -0
  24. data/kinokero.gemspec +35 -0
  25. data/lib/kinokero.rb +183 -0
  26. data/lib/kinokero/blank.rb +105 -0
  27. data/lib/kinokero/cloudprint.rb +1159 -0
  28. data/lib/kinokero/device.rb +6 -0
  29. data/lib/kinokero/jingle.rb +176 -0
  30. data/lib/kinokero/log.rb +157 -0
  31. data/lib/kinokero/printer.rb +313 -0
  32. data/lib/kinokero/proxy.rb +341 -0
  33. data/lib/kinokero/ruby_extensions.rb +21 -0
  34. data/lib/kinokero/sasl_xoauth2.rb +164 -0
  35. data/lib/kinokero/version.rb +3 -0
  36. data/lib/proto/cloud_device_description.proto +18 -0
  37. data/lib/proto/cloud_device_state.proto +30 -0
  38. data/lib/proto/cloud_device_state_type.proto +20 -0
  39. data/lib/proto/cloud_device_ui_state.proto +42 -0
  40. data/lib/proto/cloud_device_ui_state_severity.proto +12 -0
  41. data/lib/proto/cloud_job_ticket.proto +18 -0
  42. data/lib/proto/collate.proto +4 -0
  43. data/lib/proto/collate_ticket_item.proto +5 -0
  44. data/lib/proto/color.proto +42 -0
  45. data/lib/proto/color_ticket_item.proto +12 -0
  46. data/lib/proto/copies.proto +6 -0
  47. data/lib/proto/copies_ticket_item.proto +5 -0
  48. data/lib/proto/cover.proto +31 -0
  49. data/lib/proto/cover_state.proto +25 -0
  50. data/lib/proto/device_action_cause.proto +19 -0
  51. data/lib/proto/dpi.proto +27 -0
  52. data/lib/proto/dpi_ticket_item.proto +13 -0
  53. data/lib/proto/duplex.proto +15 -0
  54. data/lib/proto/duplex_ticket_item.proto +8 -0
  55. data/lib/proto/fit_to_page.proto +20 -0
  56. data/lib/proto/fit_to_page_ticket_item.proto +8 -0
  57. data/lib/proto/input_tray_state.proto +31 -0
  58. data/lib/proto/input_tray_unit.proto +35 -0
  59. data/lib/proto/job_state.proto +143 -0
  60. data/lib/proto/local_settings.proto +36 -0
  61. data/lib/proto/localized_string.proto +119 -0
  62. data/lib/proto/margins.proto +33 -0
  63. data/lib/proto/margins_ticket_item.proto +14 -0
  64. data/lib/proto/marker.proto +62 -0
  65. data/lib/proto/marker_state.proto +31 -0
  66. data/lib/proto/media_path.proto +6 -0
  67. data/lib/proto/media_path_state.proto +25 -0
  68. data/lib/proto/media_size.proto +216 -0
  69. data/lib/proto/media_size_ticket_item.proto +17 -0
  70. data/lib/proto/output_bin_state.proto +31 -0
  71. data/lib/proto/output_bin_unit.proto +32 -0
  72. data/lib/proto/page_orientation.proto +16 -0
  73. data/lib/proto/page_orientation_ticket_item.proto +9 -0
  74. data/lib/proto/page_range.proto +15 -0
  75. data/lib/proto/page_range_ticket_item.proto +7 -0
  76. data/lib/proto/print_job_state.proto +18 -0
  77. data/lib/proto/print_job_state_diff.proto +13 -0
  78. data/lib/proto/print_job_ui_state.proto +24 -0
  79. data/lib/proto/print_ticket_section.proto +30 -0
  80. data/lib/proto/printer_description_section.proto +107 -0
  81. data/lib/proto/printer_state_section.proto +49 -0
  82. data/lib/proto/printer_ui_state_section.proto +39 -0
  83. data/lib/proto/printing_speed.proto +35 -0
  84. data/lib/proto/pwg_raster_config.proto +176 -0
  85. data/lib/proto/range_capability.proto +14 -0
  86. data/lib/proto/reverse_order.proto +5 -0
  87. data/lib/proto/reverse_order_ticket_item.proto +5 -0
  88. data/lib/proto/scanner_description_section.proto +5 -0
  89. data/lib/proto/scanner_state_section.proto +25 -0
  90. data/lib/proto/select_capability.proto +31 -0
  91. data/lib/proto/supported_content_type.proto +12 -0
  92. data/lib/proto/typed_value_capability.proto +15 -0
  93. data/lib/proto/vendor_capability.proto +40 -0
  94. data/lib/proto/vendor_state.proto +26 -0
  95. data/lib/proto/vendor_ticket_item.proto +9 -0
  96. data/lib/proto_lib/cloud_device_state.pb.rb +21 -0
  97. data/lib/proto_lib/cloud_device_state_type.pb.rb +16 -0
  98. data/lib/proto_lib/cloud_device_ui_state.pb.rb +22 -0
  99. data/lib/proto_lib/cloud_device_ui_state_severity.pb.rb +17 -0
  100. data/lib/proto_lib/collate.pb.rb +11 -0
  101. data/lib/proto_lib/color.pb.rb +31 -0
  102. data/lib/proto_lib/copies.pb.rb +12 -0
  103. data/lib/proto_lib/cover.pb.rb +21 -0
  104. data/lib/proto_lib/cover_state.pb.rb +27 -0
  105. data/lib/proto_lib/device_action_cause.pb.rb +18 -0
  106. data/lib/proto_lib/dpi.pb.rb +27 -0
  107. data/lib/proto_lib/duplex.pb.rb +26 -0
  108. data/lib/proto_lib/fit_to_page.pb.rb +28 -0
  109. data/lib/proto_lib/input_tray_state.pb.rb +30 -0
  110. data/lib/proto_lib/input_tray_unit.pb.rb +25 -0
  111. data/lib/proto_lib/job_state.pb.rb +99 -0
  112. data/lib/proto_lib/localized_string.pb.rb +118 -0
  113. data/lib/proto_lib/margins.pb.rb +30 -0
  114. data/lib/proto_lib/marker.pb.rb +45 -0
  115. data/lib/proto_lib/marker_state.pb.rb +30 -0
  116. data/lib/proto_lib/media_path.pb.rb +11 -0
  117. data/lib/proto_lib/media_path_state.pb.rb +27 -0
  118. data/lib/proto_lib/media_size.pb.rb +198 -0
  119. data/lib/proto_lib/output_bin_state.pb.rb +30 -0
  120. data/lib/proto_lib/output_bin_unit.pb.rb +22 -0
  121. data/lib/proto_lib/page_orientation.pb.rb +26 -0
  122. data/lib/proto_lib/page_range.pb.rb +20 -0
  123. data/lib/proto_lib/print_job_state_diff.pb.rb +12 -0
  124. data/lib/proto_lib/printer_description_section.pb.rb +30 -0
  125. data/lib/proto_lib/printer_state_section.pb.rb +23 -0
  126. data/lib/proto_lib/printer_ui_state_section.pb.rb +28 -0
  127. data/lib/proto_lib/printing_speed.pb.rb +21 -0
  128. data/lib/proto_lib/pwg_raster_config.pb.rb +103 -0
  129. data/lib/proto_lib/range_capability.pb.rb +19 -0
  130. data/lib/proto_lib/reverse_order.pb.rb +11 -0
  131. data/lib/proto_lib/scanner_description_section.pb.rb +10 -0
  132. data/lib/proto_lib/scanner_state_section.pb.rb +17 -0
  133. data/lib/proto_lib/select_capability.pb.rb +22 -0
  134. data/lib/proto_lib/supported_content_type.pb.rb +13 -0
  135. data/lib/proto_lib/typed_value_capability.pb.rb +19 -0
  136. data/lib/proto_lib/vendor_capability.pb.rb +23 -0
  137. data/lib/proto_lib/vendor_state.pb.rb +27 -0
  138. data/test/.ruby-gemset +1 -0
  139. data/test/.ruby-version +1 -0
  140. data/test/Gemfile +68 -0
  141. data/test/Gemfile.lock +269 -0
  142. data/test/README.md +2 -0
  143. data/test/Rakefile +6 -0
  144. data/test/app/assets/javascripts/application.js +16 -0
  145. data/test/app/assets/stylesheets/application.css +13 -0
  146. data/test/app/controllers/application_controller.rb +13 -0
  147. data/test/app/controllers/home_controller.rb +10 -0
  148. data/test/app/helpers/application_helper.rb +2 -0
  149. data/test/app/views/home/index.html.erb +2 -0
  150. data/test/app/views/home/show.html.erb +2 -0
  151. data/test/app/views/layouts/application.html.erb +14 -0
  152. data/test/bin/bundle +3 -0
  153. data/test/bin/rails +4 -0
  154. data/test/bin/rake +4 -0
  155. data/test/config/application.rb +29 -0
  156. data/test/config/boot.rb +4 -0
  157. data/test/config/database.yml +25 -0
  158. data/test/config/environment.rb +5 -0
  159. data/test/config/environments/development.rb +48 -0
  160. data/test/config/environments/production.rb +95 -0
  161. data/test/config/environments/test.rb +42 -0
  162. data/test/config/initializers/backtrace_silencers.rb +7 -0
  163. data/test/config/initializers/filter_parameter_logging.rb +4 -0
  164. data/test/config/initializers/inflections.rb +16 -0
  165. data/test/config/initializers/mime_types.rb +5 -0
  166. data/test/config/initializers/secret_token.rb +12 -0
  167. data/test/config/initializers/session_store.rb +3 -0
  168. data/test/config/initializers/wrap_parameters.rb +14 -0
  169. data/test/config/locales/en.yml +23 -0
  170. data/test/config/routes.rb +65 -0
  171. data/test/db/development.sqlite3 +0 -0
  172. data/test/db/migrate/20111012050200_add_sessions_table.rb +12 -0
  173. data/test/db/schema.rb +26 -0
  174. data/test/db/seeds.rb +7 -0
  175. data/test/db/test.sqlite3 +0 -0
  176. data/test/log/development.log +0 -0
  177. data/test/log/test.log +0 -0
  178. data/test/test/controllers/home_controller_test.rb +133 -0
  179. data/test/test/ctlr_test_helper.rb +7 -0
  180. data/test/test/fixtures/gcp_seed.yml +51 -0
  181. data/test/test/models/cloudprint_test.rb +186 -0
  182. data/test/test/models/jingle_test.rb +44 -0
  183. data/test/test/models/printer_test.rb +99 -0
  184. data/test/test/models/proxy_test.rb +102 -0
  185. data/test/test/test_helper.rb +31 -0
  186. data/test/test/test_kinokero.rb +234 -0
  187. metadata +462 -0
@@ -0,0 +1,6 @@
1
+ module Kinokero
2
+ module Device
3
+
4
+ end # Device
5
+
6
+ end # Kinokero
@@ -0,0 +1,176 @@
1
+ module Kinokero
2
+
3
+ # #########################################################################
4
+
5
+ class Jingle
6
+
7
+ # #########################################################################
8
+
9
+ include Jabber
10
+
11
+ # #########################################################################
12
+
13
+ attr_reader :gcp_channel, :gcp_control, :my_cloudprint, :is_connection
14
+
15
+ # #########################################################################
16
+
17
+ # -----------------------------------------------------------------------------
18
+ # ----- jabber initialization here -----------------------------------------
19
+ # -----------------------------------------------------------------------------
20
+ def initialize( my_cloudprint, gcp_control, verbose )
21
+ @my_cloudprint = my_cloudprint # cloudprint object
22
+ @gcp_control = gcp_control
23
+ @gcp_channel = ::Kinokero.gcp_channel
24
+ @client = nil
25
+ @is_connection = false # true if connection established
26
+ @in_callback = false # semaphore true during callback stanza
27
+
28
+ Jabber::debug = verbose
29
+ end
30
+
31
+ # -----------------------------------------------------------------------------
32
+
33
+ def self.set_verbose
34
+ Jabber::debug = true
35
+ end
36
+
37
+
38
+ def self.clear_verbose
39
+ Jabber::debug = false
40
+ end
41
+
42
+
43
+ # -----------------------------------------------------------------------------
44
+
45
+ def gtalk_start_connection( &block )
46
+
47
+ begin
48
+
49
+ begin_connection() # establishes client protocol
50
+
51
+ gtalk_start_subscription() # starts subscription to receive jobs
52
+
53
+ gtalk_notification_callback( &block ) # callback response to notifications
54
+
55
+ Jabber::debuglog("**************** protocol ended normally ******************")
56
+
57
+ rescue
58
+ Jabber::debuglog("\e[1;31m**************** protocol yielded exception: #{ $! } ******************\e[0m")
59
+ @is_connection = false
60
+ end # block for catch exceptions
61
+
62
+ end # end gtalk_start_connection
63
+
64
+ # -----------------------------------------------------------------------------
65
+
66
+ def begin_connection()
67
+ if @client.nil?
68
+ @sender_jid = Jabber::JID.new( @gcp_control[ :gcp_xmpp_jid ] )
69
+ @client = Jabber::Client.new(@sender_jid)
70
+ @client.jid.resource = @gcp_control[ :gcp_printer_name ]
71
+ @conn = @client.connect(::Kinokero.xmpp_server)
72
+ end
73
+ end
74
+
75
+ # -----------------------------------------------------------------------------
76
+
77
+ # setup callback for subscription, which then triggers kinokero
78
+ # <message from="cloudprint.google.com" to=”{Full JID}”>
79
+ # <push:push channel="cloudprint.google.com" xmlns:push="google:push">
80
+ # <push:recipient to="{Bare JID}"></push:recipient>
81
+ # <push:data>{Base-64 encoded printer id}</push:data>
82
+ # </push:push>
83
+ # </message>
84
+ def gtalk_notification_callback( &block )
85
+ @client.add_message_callback do |m|
86
+
87
+ @in_callback = true # shows we're within callback now
88
+
89
+ if m.from == @gcp_channel
90
+ # grab the "push:data" snippet from within the "push:push" snippet
91
+ # from within the current stanza m
92
+ # for better understanding this, see issue & comments at:
93
+ # https://github.com/xmpp4r/xmpp4r/issues/29
94
+ encoded_printerid = m.first_element("push:push").first_element_text("push:data")
95
+
96
+ if encoded_printerid.nil? # is it invalid?
97
+ Jabber::debuglog("GCP CALLBACK printer_id nil ERROR ???????????????")
98
+
99
+ else
100
+ # decode it
101
+ printerid = Base64::strict_decode64(encoded_printerid)
102
+
103
+ if printerid.nil? # is that invalid?
104
+ Jabber::debuglog("GCP CALLBACK decoded printer_id nil ERROR #{encoded_printerid} ???????????????")
105
+
106
+ else
107
+
108
+ Jabber::debuglog("GCP CALLBACK: printer_id: #{printerid}")
109
+
110
+ # go into appliance and let it know we need the queue for this printer
111
+ yield( printerid )
112
+
113
+ end # decoded printerid nil if..then..else
114
+ end # printerid useless if..then..else
115
+ end # if channel correct
116
+
117
+ @in_callback = false # shows we're not within callback now
118
+
119
+ end # callback block
120
+ end
121
+
122
+ # -----------------------------------------------------------------------------
123
+
124
+ def gtalk_start_subscription()
125
+ unless @client.nil?
126
+
127
+ # prep the Google Talk for GCP subscribe stanza
128
+ iq_subscribe = Jabber::Iq.new( :set, @gcp_control[ :gcp_xmpp_jid ])
129
+ sub_el = iq_subscribe.add_element( 'subscribe', 'xmlns' => ::Kinokero.ns_google_push )
130
+ sub_el.add_element( 'item', 'channel' => @gcp_channel, 'from' => @gcp_channel )
131
+
132
+ @client.auth( @my_cloudprint.gcp_form_jingle_auth_token )
133
+
134
+ @client.send( iq_subscribe )
135
+
136
+ @is_connection = true
137
+
138
+ end # if a client exists
139
+
140
+ end
141
+
142
+ # -----------------------------------------------------------------------------
143
+
144
+ def gtalk_close_connection()
145
+ unless @client.nil? || @in_callback
146
+ # cease responding to message callbacks
147
+ @client.delete_message_callback( nil )
148
+
149
+ # prep the Google Talk for GCP unsubscribe stanza
150
+ iq_unsubscribe = Jabber::Iq.new( :set, @gcp_control[ :gcp_xmpp_jid ])
151
+ sub_el = iq_unsubscribe.add_element( 'unsubscribe', 'xmlns' => ::Kinokero.ns_google_push )
152
+ sub_el.add_element( 'item', 'channel' => @gcp_channel, 'from' => @gcp_channel )
153
+
154
+ # attempt to unsubscribe (currently GCP returns 501: feature-not-implemented error
155
+ @client.send( iq_unsubscribe )
156
+
157
+ # now seal off the connection
158
+ @client.close!
159
+
160
+ @client = nil # and let it vanish away
161
+
162
+ @is_connection = false
163
+
164
+ end # if a client exists
165
+
166
+ end
167
+
168
+ # -----------------------------------------------------------------------------
169
+
170
+ # #########################################################################
171
+
172
+ end # class Jingle
173
+
174
+ # #########################################################################
175
+
176
+ end # module
@@ -0,0 +1,157 @@
1
+ module Kinokero
2
+
3
+ # #########################################################################
4
+
5
+ class Log
6
+
7
+ # #########################################################################
8
+
9
+ # logger must be accessible as class-method level for register
10
+ @@logger = ::Logger.new(STDOUT) # in case we need error logging
11
+
12
+ # def_delegators :@@logger, :debug, :info, :warn, :error, :fatal
13
+ # #########################################################################
14
+
15
+ # ------------------------------------------------------------------------------
16
+
17
+ def self.debug(msg, &block)
18
+ @@logger.debug( say_debug(msg), &block )
19
+ end
20
+
21
+ # ------------------------------------------------------------------------------
22
+
23
+ def self.verbose_debug(msg, verbose=nil )
24
+ if verbose || ( verbose.nil? && Kinokero.verbose )
25
+ @@logger.debug( say_debug(msg) )
26
+ end
27
+ end
28
+
29
+ # ------------------------------------------------------------------------------
30
+
31
+ def self.info(msg, &block)
32
+ @@logger.info( say_info(msg), &block )
33
+ end
34
+
35
+ # ------------------------------------------------------------------------------
36
+
37
+ def self.verbose_info(msg, verbose=nil )
38
+ if verbose || ( verbose.nil? && Kinokero.verbose )
39
+ @@logger.info( say_info(msg) )
40
+ end
41
+ end
42
+
43
+ # ------------------------------------------------------------------------------
44
+
45
+ def self.warn(msg, &block)
46
+ @@logger.warn( say_warn(msg), &block )
47
+ end
48
+
49
+ # ------------------------------------------------------------------------------
50
+
51
+ def self.error(msg, &block)
52
+ @@logger.error( say_error(msg), &block )
53
+ end
54
+
55
+ # ------------------------------------------------------------------------------
56
+
57
+ def self.fatal(msg, &block)
58
+ @@logger.fatal( say_fatal(msg), &block )
59
+ end
60
+
61
+ # ------------------------------------------------------------------------------
62
+
63
+
64
+
65
+ # #########################################################################
66
+
67
+ # ------------------------------------------------------------------------------
68
+
69
+ #
70
+ # log_request -- will log the farady request params if verbose setting
71
+ #
72
+ # * *Args* :
73
+ # - +msg+ - string to identify position in protocol sequence
74
+ # - +req+ - gcp request hash
75
+ # * *Returns* :
76
+ # -
77
+ # * *Raises* :
78
+ # -
79
+ #
80
+ def self.log_request( msg, req, verbose = nil )
81
+ if verbose || ( verbose.nil? && Kinokero.verbose )
82
+ body = ( req.body.nil? ? req : req.body )
83
+ puts "\n---------- REQUEST ------------ #{body.class.name} --------------"
84
+ @@logger.debug( msg ) { body.inspect }
85
+ puts "----------" * 4
86
+ end # if verbose
87
+ end
88
+
89
+ # ------------------------------------------------------------------------------
90
+
91
+ # log the GCP response
92
+ #
93
+ # * *Args* :
94
+ # - +msg+ - string to identify position in protocol sequence
95
+ # - +response+ - gcp response hash
96
+ # * *Returns* :
97
+ # -
98
+ # * *Raises* :
99
+ # -
100
+ #
101
+ def self.log_response( msg, response, verbose = nil )
102
+ if verbose || ( verbose.nil? && Kinokero.verbose )
103
+ body = ( response.body.nil? ? response : response.body )
104
+ puts "\n---------- RESPONSE ------------ #{body.class.name} --------------"
105
+ @@logger.debug( msg ) { body.inspect[0, ::Kinokero.truncate_log] }
106
+ puts "----------" * 4
107
+ end # if verbose
108
+ end
109
+
110
+ # #########################################################################
111
+
112
+ # private
113
+
114
+ # #########################################################################
115
+
116
+ # -----------------------------------------------------------------------------
117
+
118
+ # wraps a message in color coding for terminals
119
+ #
120
+ def self.say_debug(msg)
121
+ return msg # white
122
+ end
123
+
124
+ # -----------------------------------------------------------------------------
125
+
126
+ def self.say_info(msg)
127
+ return "\e[1;34m" + msg + "\e[0m" # blue
128
+ end
129
+
130
+ # -----------------------------------------------------------------------------
131
+
132
+ def self.say_warn(msg)
133
+ return "\e[1;33m" + msg + "\e[0m" # orange
134
+ end
135
+
136
+ # -----------------------------------------------------------------------------
137
+
138
+ def self.say_error(msg)
139
+ return "\e[1;31m" + msg + "\e[0m" # red
140
+ end
141
+
142
+ # -----------------------------------------------------------------------------
143
+
144
+ def self.say_fatal(msg)
145
+ return "\e[1;31m" + msg + "\e[0m" # red
146
+ end
147
+
148
+
149
+
150
+ # #########################################################################
151
+
152
+ end # Log
153
+
154
+ # #########################################################################
155
+
156
+
157
+ end # Kinokero
@@ -0,0 +1,313 @@
1
+ module Kinokero
2
+
3
+ # ****************************************************************************
4
+
5
+ # all printer-specific information & handling
6
+ # mixes in Device for any superset device common stuff
7
+ #
8
+ # == data structure
9
+ # relates together:
10
+ # higher-level linkages (such as to a persistance/recall mechanism)
11
+ # GCP-specific info (such as printer_id, access_tokens, etc)
12
+ # GCP-requesting info for device
13
+ #
14
+ class Printer
15
+
16
+ include Device
17
+ require 'cups'
18
+
19
+ attr_reader :model, :gcp_printer_control, :was_state
20
+ attr_accessor :cloudprint, :poll_thread # cloudprint object for this device
21
+
22
+ # -----------------------------------------------------------------------------
23
+
24
+ # new object constructor; any/all args can be missing
25
+ #
26
+ # * *Args* :
27
+ # - +gcp_info+ - hash of on-going gcp required information
28
+ # - +request_info+ - hash of info needed for a request
29
+ # - +model_info+ - nil or higher-level object itself
30
+ # * *Returns* :
31
+ # - Printer object
32
+ # * *Raises* :
33
+ # -
34
+ #
35
+ def initialize( gcp_info={}, model_info=nil )
36
+ # TODO: uncomment when Device mix-in is fleshed out
37
+ # super
38
+
39
+ @model = nil
40
+ @poll_thread = nil # will be the polling thread object
41
+ @cloudprint = nil # Proxy fills this in later
42
+ @was_state = false # previous state, printer not ready
43
+ @gcp_printer_control = nil # default if empty
44
+
45
+ setup_model( model_info )
46
+ setup_gcp( gcp_info )
47
+
48
+
49
+ end
50
+
51
+ # -----------------------------------------------------------------------------
52
+
53
+ # setup_model info
54
+ #
55
+ # * *Args* :
56
+ # - +model_info+ - some type of model object meaningful to calling appliance
57
+ # * *Returns* :
58
+ # -
59
+ # * *Raises* :
60
+ # -
61
+ #
62
+ def setup_model( model_info )
63
+ @model = model_info
64
+ end
65
+
66
+
67
+ # -----------------------------------------------------------------------------
68
+
69
+ # setup_gcp info from hash (possibly empty)
70
+ #
71
+ # * *Args* :
72
+ # - +gcp_info+ - hash of on-going gcp required information
73
+ # * *Returns* :
74
+ # -
75
+ # * *Raises* :
76
+ # - ArgumentError upon invalid values or keys for gcp_info
77
+ #
78
+ def setup_gcp( gcp_info )
79
+ unless gcp_info.empty?
80
+ validate_gcp_options( gcp_info )
81
+ @gcp_printer_control = gcp_info # persist the hash
82
+ end
83
+ end
84
+
85
+ # -----------------------------------------------------------------------------
86
+
87
+
88
+ # -----------------------------------------------------------------------------
89
+
90
+ def start_poll_thread()
91
+
92
+ if @poll_thread.nil? && @gcp_printer_control[:is_active]
93
+
94
+ @poll_thread = Thread.new do
95
+
96
+ while true # LOOP indefinitely
97
+
98
+ # get current device cups state
99
+ state_hash = Cups.options_for( @gcp_printer_control[:cups_alias] )
100
+
101
+ # convert string to boolean for is_accepting jobs
102
+ is_accepting = (state_hash['printer-is-accepting-jobs'] == 'true')
103
+
104
+ # if different from before
105
+ if @was_state ^ is_accepting
106
+ # remember the changed state
107
+ @was_state = is_accepting
108
+ # then tell GCP about the change
109
+ @cloudprint.gcp_ready_state_changed(
110
+ @was_state,
111
+ cups_state_to_sym( state_hash['printer-state'] ),
112
+ cups_reason( state_hash['printer-state-reasons'] )
113
+ )
114
+ end
115
+
116
+ # go back to sleep again
117
+ sleep Kinokero.printer_poll_cycle # <<<< SLEEPING HERE <<<<<
118
+ end
119
+
120
+ end # polling thread; only ends when killed
121
+
122
+ # force abort of everything if exception in thread
123
+ @poll_thread.abort_on_exception = true
124
+
125
+ end # if no existing poll_thread and this is an active printer
126
+
127
+ end
128
+
129
+ def stop_poll_thread()
130
+ @poll_thread.kill unless @poll_thread.nil?
131
+ @poll_thread = nil
132
+ end
133
+
134
+ # -----------------------------------------------------------------------------
135
+
136
+
137
+ def cups_state_to_sym( state )
138
+ case ( state )
139
+ when '3' then :idle
140
+ when '4' then :processing
141
+ when '5' then :stopped
142
+ else
143
+ state.to_sym
144
+ end # case
145
+ end
146
+
147
+ def cups_reason( reason )
148
+ return ( reason == 'none' ? '' : reason )
149
+ end
150
+
151
+
152
+ # ****************************************************************************
153
+ # PRINTER CONTROL
154
+ # ****************************************************************************
155
+
156
+ def print_file( file )
157
+ printer_command = "lp -d #{@gcp_printer_control[:cups_alias]} #{file} "
158
+ Kinokero::Log.verbose_debug "#{@gcp_printer_control[:gcp_printer_name]}: " +
159
+ printer_command + "\n"
160
+
161
+ status = system( "#{printer_command}" )
162
+
163
+ # TODO: figure out what the status means? or get printer status
164
+ # so we know when the job has successfully printed
165
+
166
+ return status
167
+ end
168
+
169
+ # -----------------------------------------------------------------------------
170
+
171
+ def is_printer_ready?()
172
+ state_hash = Cups.options_for( @gcp_printer_control[:cups_alias] )
173
+ return state_hash['printer-is-accepting-jobs']
174
+ end
175
+
176
+
177
+ # -----------------------------------------------------------------------------
178
+ # -----------------------------------------------------------------------------
179
+
180
+ protected
181
+
182
+ # -----------------------------------------------------------------------------
183
+ # -----------------------------------------------------------------------------
184
+
185
+ # SAMPLE_GCP_OPTIONS are indicative of the data furnished by GCP after
186
+ # printer registration; the data here has been sanitized
187
+ SAMPLE_GCP_OPTIONS = {
188
+ gcp_xmpp_jid: "123456789cbce123456789abcdef123a@cloudprint.googleusercontent.com" ,
189
+ gcp_confirmation_url: "https://www.google.com/cloudprint/regconfirmpage?printername=my_printer_name&email=my_user@gmail.com&dpi=300&pagesize=215900,279400" ,
190
+ gcp_owner_email: "my_user@gmail.com" ,
191
+
192
+ gcp_printer_name: "my_printer_name",
193
+ gcp_printerid: "bd9234a9-abc6-1a34-012def1234ab63f03" ,
194
+
195
+ gcp_access_token: "ya29.LgDrBsgEaZ7b-ridicuously-long-encrypted-key-u2TA" ,
196
+ gcp_refresh_token: "1/nm0_another_encrypted_key_xxxxxxxxxxxxxe730" ,
197
+ gcp_token_type: "Bearer" ,
198
+ gcp_token_expiry_time: Time.new(2014,6,13,19,31,0),
199
+
200
+ printer_id: 1,
201
+ item: 'test',
202
+ cups_alias: 'cups_printer_name',
203
+ virgin_access: false, # true for initial oauth2 token
204
+ is_active: false, # set to true after successful registration
205
+
206
+ capability_ppd: '/etc/cups/ppd/my_printer.ppd',
207
+ capability_cdd: '/etc/cups/cdd/laserjet_1102w.cdd',
208
+
209
+ gcp_uuid: 'VND3R11877',
210
+ gcp_manufacturer: 'Hewlett-Packard',
211
+ gcp_model: 'LaserJet P1102w',
212
+ gcp_setup_url: 'http://www8.hp.com/us/en/campaigns/wireless-printing-center/printer-setup-help.html',
213
+ gcp_support_url: 'http://h10025.www1.hp.com/ewfrf/wc/product?product=4110396&lc=en&cc=us&dlc=en&lang=en&cc=us',
214
+ gcp_update_url: 'http://h10025.www1.hp.com/ewfrf/wc/product?product=4110396&lc=en&cc=us&dlc=en&lang=en&cc=us',
215
+ gcp_firmware: '20130703',
216
+ }
217
+
218
+ # VALID_GCP_OPTIONS is used to determine if user options valid
219
+ # if (in future) any default options were to be off-limits,
220
+ # then a specific sets of keys will have to be enumerated below
221
+ VALID_GCP_OPTIONS = SAMPLE_GCP_OPTIONS.keys
222
+
223
+ # -----------------------------------------------------------------------------
224
+
225
+ # validates gcp options
226
+ #
227
+ # * *Args* :
228
+ # - +options+ - gcp_control as a hash
229
+ # * *Returns* :
230
+ # -
231
+ # * *Raises* :
232
+ # - ArgumentError if invalid option present
233
+ #
234
+ def validate_gcp_options(options)
235
+
236
+ validate_hash(
237
+ 'VALIDATE gcp_control',
238
+ VALID_GCP_OPTIONS,
239
+ SAMPLE_GCP_OPTIONS,
240
+ options
241
+ )
242
+
243
+ end
244
+
245
+ # -----------------------------------------------------------------------------
246
+
247
+
248
+ # -----------------------------------------------------------------------------
249
+
250
+ # validates any hash options against a standard
251
+ #
252
+ # * *Args* :
253
+ # - +msg+ - string to display if exception occurs
254
+ # - +valid_keys+ - list of expected hash keys (to look for invalid keys)
255
+ # - +sample+ - hash of sample values for all those keys, used to validate options hash values
256
+ # - +options+ - hash of values to be used
257
+ # * *Returns* :
258
+ # -
259
+ # * *Raises* :
260
+ # - ArgumentError if invalid option present
261
+ #
262
+
263
+ def validate_hash(msg, valid_keys, sample, options)
264
+
265
+ # options validations: check for invalid keys
266
+ options.assert_valid_keys(valid_keys)
267
+
268
+ # options validations: check for invalid values
269
+ valid_keys.each do |key|
270
+
271
+ if options[key].nil? || !similar_values?(options[key], sample[key])
272
+ err_msg = "[#{msg}] value for key #{key} should be similar to <#{sample[key]}>, a #{sample[key].class}; " +
273
+ "instead it was <#{options[key]}>"
274
+ raise ArgumentError, err_msg
275
+ end
276
+
277
+ end # do validate each key
278
+
279
+ end
280
+
281
+ # -----------------------------------------------------------------------------
282
+
283
+ def similar_values?(ref, x)
284
+ return true if ref.kind_of?( x.class ) || ( (x == true) ^ (x == false) )
285
+ return false
286
+ end
287
+
288
+ # ****************************************************************************
289
+
290
+ # -----------------------------------------------------------------------------
291
+ def self.print_gcp_registration_info( cups_printer, msg )
292
+
293
+ # display in log or the SYSOUT
294
+ Kinokero::Log.debug ("\n------------------------------------------------------------------\n")
295
+ Kinokero::Log.info( msg )
296
+ Kinokero::Log.debug ("\n------------------------------------------------------------------\n")
297
+
298
+ # print out on the new printer
299
+ command = ( system("which enscript") ? 'enscript -f Helvetica12' : 'lp' )
300
+
301
+ system("echo '#{msg}' | #{command} -d #{cups_printer}")
302
+ end
303
+
304
+
305
+
306
+
307
+ # ****************************************************************************
308
+
309
+ end # Printer
310
+
311
+ # ****************************************************************************
312
+
313
+ end # Kinokero