Add a printer job queue via the printer plugin
This commit is contained in:
parent
63e1541aa3
commit
c955a6ee40
24 changed files with 561 additions and 1 deletions
58
plugins/printer/app/controllers/printer_controller.rb
Normal file
58
plugins/printer/app/controllers/printer_controller.rb
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
class PrinterController < ApplicationController
|
||||
include Concerns::SendOrderPdf
|
||||
include Tubesock::Hijack
|
||||
|
||||
skip_before_filter :authenticate
|
||||
before_filter :authenticate_printer
|
||||
before_filter -> { require_plugin_enabled FoodsoftPrinter }
|
||||
|
||||
def socket
|
||||
hijack do |tubesock|
|
||||
tubesock.onopen do
|
||||
tubesock.send_data unfinished_jobs
|
||||
end
|
||||
|
||||
tubesock.onmessage do |data|
|
||||
update_job data
|
||||
tubesock.send_data unfinished_jobs
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
job = PrinterJob.find(params[:id])
|
||||
send_order_pdf job.order, job.document
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unfinished_jobs
|
||||
{
|
||||
unfinished_jobs: PrinterJob.pending.map(&:id)
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def update_job(data)
|
||||
json = JSON.parse data, symbolize_names: true
|
||||
job = PrinterJob.unfinished.find_by_id(json[:id])
|
||||
return unless job
|
||||
if json[:state]
|
||||
job.add_update! json[:state], json[:message]
|
||||
end
|
||||
job.finish! if json[:finish]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def bearer_token
|
||||
pattern = /^Bearer /
|
||||
header = request.headers['Authorization']
|
||||
header.gsub(pattern, '') if header && header.match(pattern)
|
||||
end
|
||||
|
||||
def authenticate_printer
|
||||
return head(:unauthorized) unless bearer_token
|
||||
return head(:forbidden) if bearer_token != FoodsoftConfig[:printer_token]
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue