thecore_print_commons 2.0.5 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/concerns/api/print_job.rb +30 -0
- data/app/models/concerns/api/printer.rb +3 -1
- data/app/models/concerns/rails_admin/print_job.rb +19 -0
- data/app/models/print_job.rb +10 -0
- data/app/models/printer.rb +2 -0
- data/app/workers/print_worker.rb +51 -3
- data/config/initializers/abilities_thecore_print_commons_concern.rb +11 -9
- data/config/locales/telnet_print.en.yml +2 -3
- data/config/locales/telnet_print.it.yml +2 -2
- data/db/migrate/20160407141529_create_print_jobs.rb +10 -0
- data/db/migrate/20160407152547_add_fields_to_print_job.rb +9 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a75ca48646863e62f33479174c453305b81aa9e907d3b5606e009fce8b7f4eb4
|
4
|
+
data.tar.gz: 34200d7c4a43265887eba4a02d46af283b61a258f29c6f0d1c9480fdc46722c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01744f41db8ab1352823a83c0e25565edd7252935fd8c50a783ad67187b5fbdcbd4903540e7b3da937d15efca05a4ff940c1dad6c94e2bb601305fb83b590eca
|
7
|
+
data.tar.gz: f8fb937db80a18c5a317258008c86784ddcc02372cf93a2b4014a10f19136f0509af01e4907c7e071fe4300e7a6da90f86e8c9b181d85848ae48a2b61cd8e82b
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Api::PrintJob
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
# Use @@json_attrs to drive json rendering for
|
6
|
+
# API model responses (index, show and update ones).
|
7
|
+
# For reference:
|
8
|
+
# https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
|
9
|
+
# The object passed accepts only these keys:
|
10
|
+
# - only: list [] of model fields to be shown in JSON serialization
|
11
|
+
# - except: exclude these fields from the JSON serialization, is a list []
|
12
|
+
# - methods: include the result of some method defined in the model
|
13
|
+
# - include: include associated models, it's an object {} which also accepts the keys described here
|
14
|
+
cattr_accessor :json_attrs
|
15
|
+
@@json_attrs = ::ModelDrivenApi.smart_merge (json_attrs || {}), {}
|
16
|
+
|
17
|
+
# Here you can add custom actions to be called from the API
|
18
|
+
# The action must return an serializable (JSON) object.
|
19
|
+
# Here you can find an example, in the API could be called like:
|
20
|
+
#
|
21
|
+
# GET /api/v2/:model/:id?do=test&custom_parameter=hello
|
22
|
+
#
|
23
|
+
# Please uncomment it to test with a REST client.
|
24
|
+
# Please take note on the fact that, if the do params is test, the custom
|
25
|
+
# action definition must be, like below self.custom_action_test.
|
26
|
+
# def self.custom_action_test id=nil, params=nil
|
27
|
+
# { test: [ :first, :second, :third ], id: id, params: params}
|
28
|
+
# end
|
29
|
+
end
|
30
|
+
end
|
@@ -27,7 +27,7 @@ module Api::Printer
|
|
27
27
|
# { test: [ :first, :second, :third ], id: id, params: params}
|
28
28
|
# end
|
29
29
|
|
30
|
-
def self.
|
30
|
+
def self.custom_action_print_single_barcode id, barcode
|
31
31
|
# Example Usage:
|
32
32
|
# item = ::Item.joins(:projects).where(projects: {id: params[:order_id].to_i}).first
|
33
33
|
# printer = ::Printer.where(supplier_id: current_user.supplier_id, default: true).first
|
@@ -36,6 +36,8 @@ module Api::Printer
|
|
36
36
|
# # Preso l'ordine mi recupero l'item e ne stampo la quantità richiesta
|
37
37
|
# ::PrintWorker.perform_async(printer.ip, text)
|
38
38
|
|
39
|
+
printer = Printer.find(id)
|
40
|
+
text = printer.print_template.template.gsub(printer.print_template.translation_matrix.lines.first, barcode)
|
39
41
|
::PrintWorker.perform_async(printer.ip, printer.port, text)
|
40
42
|
{ info: "Print job sent in background to #{printer.ip} on port #{printer.port}" }
|
41
43
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RailsAdmin::PrintJob
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included do
|
4
|
+
# Here You can define the RailsAdmin DSL
|
5
|
+
rails_admin do
|
6
|
+
# rails_admin do
|
7
|
+
navigation_label I18n.t("admin.settings.label")
|
8
|
+
navigation_icon 'fa fa-check-square'
|
9
|
+
parent Printer
|
10
|
+
|
11
|
+
field :printer
|
12
|
+
field :created_at
|
13
|
+
field :description
|
14
|
+
list do
|
15
|
+
field :printed_on_total
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/app/models/printer.rb
CHANGED
@@ -46,10 +46,12 @@ class Printer < ApplicationRecord
|
|
46
46
|
# end
|
47
47
|
|
48
48
|
belongs_to :print_template, inverse_of: :printers
|
49
|
+
has_many :print_jobs, dependent: :destroy, inverse_of: :printer
|
49
50
|
|
50
51
|
validates :name, presence: true
|
51
52
|
validates :ip, presence: true
|
52
53
|
validates :port, presence: true
|
54
|
+
validates :print_template, presence: true
|
53
55
|
|
54
56
|
|
55
57
|
# private
|
data/app/workers/print_worker.rb
CHANGED
@@ -6,9 +6,57 @@ class PrintWorker
|
|
6
6
|
|
7
7
|
# ZPL print
|
8
8
|
def perform ip, port, text
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
@pjob = PrintJob.create(printer_id: Printer.find_by(ip: ip), finished: false, iserror: false, total: 0, printed: 0)
|
10
|
+
status = check_status(ip)
|
11
|
+
print_job_status = false
|
12
|
+
if status == "OK"
|
13
|
+
begin
|
14
|
+
streamSock = TCPSocket.new ip, port
|
15
|
+
streamSock.send text, 0
|
16
|
+
streamSock.close
|
17
|
+
print_job_status = true
|
18
|
+
rescue
|
19
|
+
@pjob.update(iserror: true, description: "PRINTER ERROR: TIMEOUT")
|
20
|
+
print_job_status = false
|
21
|
+
end
|
22
|
+
else
|
23
|
+
@pjob.update(iserror: true, description: "PRINTER ERROR: #{status}")
|
24
|
+
print_job_status = false
|
25
|
+
end
|
26
|
+
# '~hs alla posizione 56 per 8 caratteri la quantità di etichette rimaste'
|
27
|
+
total = text.scan(/~PQ(\d+)/).last.first.to_i rescue 0
|
28
|
+
@pjob.update(printed: (print_job_status ? total : 0)) # Se risultato true, allora ha stampato tutto, altrimenti non ha stampato nulla
|
29
|
+
@pjob.update(total: total) # In realtà è inutile, ora manda tutto quello che può alla stampante, solo lei può andare storta
|
30
|
+
@pjob.update(finished: print_job_status)
|
31
|
+
end
|
32
|
+
|
33
|
+
def check_status printer
|
34
|
+
begin
|
35
|
+
s = TCPSocket.new(printer, 9100)
|
36
|
+
# Must create intepolation between item and template
|
37
|
+
# Printer.template può essere anche
|
38
|
+
# una parola di comando epr chiedere lo stato della stampante, solo nel caso sia ok,
|
39
|
+
# Allora mando la stampa
|
40
|
+
s.puts("~hs")
|
41
|
+
# Attende per la risposta (si mette in wait)
|
42
|
+
response = []
|
43
|
+
while (response_text = s.gets)
|
44
|
+
response << response_text
|
45
|
+
break if response.count == 3
|
46
|
+
end
|
47
|
+
s.close
|
48
|
+
Rails.logger.info "PrintIt: RESPONSE: #{response.inspect}"
|
49
|
+
first = response[0].split(",")
|
50
|
+
second = response[1].split(",")
|
51
|
+
return "HEAD UP" if second[2].to_i == 1
|
52
|
+
return "RIBBON OUT" if second[3].to_i == 1
|
53
|
+
return "PAPER OUT" if first[1].to_i == 1
|
54
|
+
return "PAUSE" if first[2].to_i == 1
|
55
|
+
return "OK"
|
56
|
+
rescue
|
57
|
+
Rails.logger.info "PrintIt: STATUS: UNREACHABLE"
|
58
|
+
return "UNREACHABLE"
|
59
|
+
end
|
12
60
|
end
|
13
61
|
|
14
62
|
# This is for cups only
|
@@ -2,16 +2,18 @@ module Abilities
|
|
2
2
|
class ThecorePrintCommon
|
3
3
|
include CanCan::Ability
|
4
4
|
def initialize user
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
cannot [:destroy, :create, :update, :show], [PrintJob]
|
6
|
+
# if user
|
7
|
+
# cannot :edi
|
8
|
+
# # if the user is logged in, it can do certain tasks regardless his role
|
9
|
+
# if user.admin?
|
10
|
+
# # if the user is an admin, it can do a lot of things, usually
|
11
|
+
# end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
13
|
+
# if user.has_role? :role
|
14
|
+
# # a specific role, brings specific powers
|
15
|
+
# end
|
16
|
+
# end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
en:
|
2
2
|
admin:
|
3
3
|
help:
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
print_template:
|
5
|
+
translation_matrix:
|
@@ -1,5 +1,5 @@
|
|
1
1
|
it:
|
2
2
|
admin:
|
3
3
|
help:
|
4
|
-
|
5
|
-
|
4
|
+
print_template:
|
5
|
+
translation_matrix:
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class AddFieldsToPrintJob < ActiveRecord::Migration[4.2]
|
2
|
+
def change
|
3
|
+
add_column :print_jobs, :iserror, :boolean, default: false
|
4
|
+
add_column :print_jobs, :description, :string
|
5
|
+
add_index :print_jobs, :description
|
6
|
+
add_column :print_jobs, :printed, :integer
|
7
|
+
add_column :print_jobs, :total, :integer
|
8
|
+
end
|
9
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thecore_print_commons
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriele Tassoni
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thecore_background_jobs
|
@@ -35,10 +35,13 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- Rakefile
|
37
37
|
- app/assets/config/thecore_print_commons_manifest.js
|
38
|
+
- app/models/concerns/api/print_job.rb
|
38
39
|
- app/models/concerns/api/print_template.rb
|
39
40
|
- app/models/concerns/api/printer.rb
|
41
|
+
- app/models/concerns/rails_admin/print_job.rb
|
40
42
|
- app/models/concerns/rails_admin/print_template.rb
|
41
43
|
- app/models/concerns/rails_admin/printer.rb
|
44
|
+
- app/models/print_job.rb
|
42
45
|
- app/models/print_template.rb
|
43
46
|
- app/models/printer.rb
|
44
47
|
- app/workers/print_worker.rb
|
@@ -50,6 +53,8 @@ files:
|
|
50
53
|
- config/locales/telnet_print.it.yml
|
51
54
|
- config/routes.rb
|
52
55
|
- db/migrate/20160323152041_create_printers.rb
|
56
|
+
- db/migrate/20160407141529_create_print_jobs.rb
|
57
|
+
- db/migrate/20160407152547_add_fields_to_print_job.rb
|
53
58
|
- db/migrate/20160413115407_add_temperature_to_printer.rb
|
54
59
|
- db/migrate/20160519093702_create_print_templates.rb
|
55
60
|
- db/migrate/20160519124051_add_print_template_id_to_printer.rb
|