thecore_print_commons 2.0.7 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/models/concerns/api/printer.rb +34 -2
- data/app/models/concerns/rails_admin/printer.rb +17 -0
- data/app/models/printer.rb +14 -12
- data/app/workers/print_worker.rb +5 -65
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3d14d2d7122107560e3483a45aaab2f69efb513fae2f2c4368f996e79b61067
|
4
|
+
data.tar.gz: 69c327df788227be6659c6a6dd7ddcfda0af077bc18b82fb177b20ae3046a8f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9841914519bfb9b77f1dd53c2f4ffcc344e411f083830a5104101be69b5ee4821636203d0c9a77cd09a7d39209b43a43098b4965236fa1f823ee6a365501c6f1
|
7
|
+
data.tar.gz: 826d5bd6e22ef43312e244b63ce864b6223b836f0f3ae4612516d555943b6760385b6839735f8445f96f90694bd5390c5980dbf8a42b0be87847b787aa7fc809
|
@@ -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.custom_action_print_single_barcode
|
30
|
+
def self.custom_action_print_single_barcode params
|
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,10 +36,42 @@ 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)
|
39
|
+
printer = Printer.find(params[:id])
|
40
40
|
text = printer.print_template.template.gsub(printer.print_template.translation_matrix.lines.first, params[:barcode])
|
41
41
|
::PrintWorker.perform_async(printer.ip, printer.port, text)
|
42
42
|
{ info: "Print job sent in background to #{printer.ip} on port #{printer.port}" }
|
43
43
|
end
|
44
|
+
|
45
|
+
def self.custom_action_printer_status params
|
46
|
+
printer = Printer.find(params[:id])
|
47
|
+
ip = printer.ip
|
48
|
+
port = printer.port
|
49
|
+
begin
|
50
|
+
s = Socket.tcp ip, port, connect_timeout: 0.5
|
51
|
+
# Must create intepolation between item and template
|
52
|
+
# Printer.template può essere anche
|
53
|
+
# una parola di comando epr chiedere lo stato della stampante, solo nel caso sia ok,
|
54
|
+
# Allora mando la stampa
|
55
|
+
s.puts("~hs")
|
56
|
+
# Attende per la risposta (si mette in wait)
|
57
|
+
response = []
|
58
|
+
while (response_text = s.gets)
|
59
|
+
response << response_text
|
60
|
+
break if response.count == 3
|
61
|
+
end
|
62
|
+
s.close
|
63
|
+
# Rails.logger.info "PrintIt: RESPONSE: #{response.inspect}"
|
64
|
+
first = response[0].split(",")
|
65
|
+
second = response[1].split(",")
|
66
|
+
return "HEAD UP" if second[2].to_i == 1
|
67
|
+
return "RIBBON OUT" if second[3].to_i == 1
|
68
|
+
return "PAPER OUT" if first[1].to_i == 1
|
69
|
+
return "PAUSE" if first[2].to_i == 1
|
70
|
+
return "OK"
|
71
|
+
rescue
|
72
|
+
Rails.logger.info "PrintIt: STATUS: UNREACHABLE"
|
73
|
+
return "UNREACHABLE"
|
74
|
+
end
|
75
|
+
end
|
44
76
|
end
|
45
77
|
end
|
@@ -17,11 +17,28 @@ module RailsAdmin::Printer
|
|
17
17
|
field :default, :toggle
|
18
18
|
field :temperature
|
19
19
|
field :description
|
20
|
+
field :online? do
|
21
|
+
read_only true
|
22
|
+
formatted_value do # used in form views
|
23
|
+
(value ? "<strong style='color:green'>👍</strong>" : "<strong style='color:red'>👎</strong>").html_safe
|
24
|
+
end
|
25
|
+
|
26
|
+
pretty_value do # used in list view columns and show views, defaults to formatted_value for non-association fields
|
27
|
+
(value ? "<strong style='color:green'>👍</strong>" : "<strong style='color:red'>👎</strong>").html_safe
|
28
|
+
end
|
29
|
+
|
30
|
+
export_value do
|
31
|
+
value ? "OK" : "KO" # used in exports, where no html/data is allowed
|
32
|
+
end
|
33
|
+
end
|
20
34
|
|
21
35
|
list do
|
22
36
|
configure :description do
|
23
37
|
visible false
|
24
38
|
end
|
39
|
+
configure :temperature do
|
40
|
+
visible false
|
41
|
+
end
|
25
42
|
end
|
26
43
|
end
|
27
44
|
end
|
data/app/models/printer.rb
CHANGED
@@ -2,11 +2,11 @@ class Printer < ApplicationRecord
|
|
2
2
|
include Api::Printer
|
3
3
|
include RailsAdmin::Printer
|
4
4
|
#serialize :translation, Hash
|
5
|
-
|
5
|
+
|
6
6
|
# before_save :check_if_unique_default
|
7
7
|
# validates :qty, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
8
8
|
# validates :translation, presence: true
|
9
|
-
|
9
|
+
|
10
10
|
# def template_enum
|
11
11
|
# PrintTemplate.descendants
|
12
12
|
# end
|
@@ -32,7 +32,7 @@ class Printer < ApplicationRecord
|
|
32
32
|
# def self.assigned_to section
|
33
33
|
# where(used_in: (USED.index(section.to_sym) + 1))
|
34
34
|
# end
|
35
|
-
|
35
|
+
|
36
36
|
# Usable with CUPS (STUB)
|
37
37
|
# def ip_enum
|
38
38
|
# # Getting from CUPS the list of configured printers
|
@@ -44,20 +44,22 @@ class Printer < ApplicationRecord
|
|
44
44
|
# CupsPrinter.get_all_printer_names hostname: Settings.ns(:printer_commons).cups_server
|
45
45
|
# end
|
46
46
|
# end
|
47
|
-
|
47
|
+
|
48
48
|
belongs_to :print_template, inverse_of: :printers
|
49
49
|
has_many :print_jobs, dependent: :destroy, inverse_of: :printer
|
50
|
-
|
50
|
+
|
51
51
|
validates :name, presence: true
|
52
52
|
validates :ip, presence: true
|
53
53
|
validates :port, presence: true
|
54
54
|
validates :print_template, presence: true
|
55
|
-
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
56
|
+
def online?
|
57
|
+
begin
|
58
|
+
Socket.tcp(self.ip, self.port, connect_timeout: 0.5).close
|
59
|
+
rescue
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
63
65
|
end
|
data/app/workers/print_worker.rb
CHANGED
@@ -7,73 +7,13 @@ class PrintWorker
|
|
7
7
|
# ZPL print
|
8
8
|
def perform ip, port, text
|
9
9
|
@pjob = PrintJob.create(printer_id: Printer.find_by(ip: ip), finished: false, iserror: false, total: 0, printed: 0)
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
10
|
+
streamSock = Socket.tcp ip, port, connect_timeout: 0.5
|
11
|
+
streamSock.send text, 0
|
12
|
+
streamSock.close
|
26
13
|
# '~hs alla posizione 56 per 8 caratteri la quantità di etichette rimaste'
|
27
14
|
total = text.scan(/~PQ(\d+)/).last.first.to_i rescue 0
|
28
|
-
@pjob.update(printed:
|
15
|
+
@pjob.update(printed: total) # Se risultato true, allora ha stampato tutto, altrimenti non ha stampato nulla
|
29
16
|
@pjob.update(total: total) # In realtà è inutile, ora manda tutto quello che può alla stampante, solo lei può andare storta
|
30
|
-
@pjob.update(finished:
|
17
|
+
@pjob.update(finished: true)
|
31
18
|
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
|
60
|
-
end
|
61
|
-
|
62
|
-
# This is for cups only
|
63
|
-
# def perform printer_cups_name, text
|
64
|
-
# # if Settings.ns(:printer_commons).cups_server.blank? || ['127.0.0.1', 'localhost'].contains(Settings.ns(:printer_commons).cups_server)
|
65
|
-
# # printer = CupsPrinter.new printer_cups_name
|
66
|
-
# # printer.print_data text, 'text/plain'
|
67
|
-
# # printer.close
|
68
|
-
# # else
|
69
|
-
# print_file = "/tmp/print-#{printer_cups_name}-#{Time.now.strftime '%Y%m%d%H%M%S%L'}"
|
70
|
-
# puts "Creating temp file: #{print_file}"
|
71
|
-
# IO.write(print_file, text)
|
72
|
-
# # CupsPrinter.new printer_cups_name, hostname: Settings.ns(:printer_commons).cups_server
|
73
|
-
# # printer.print_data text, 'text/plain', hostname: Settings.ns(:printer_commons).cups_server
|
74
|
-
# puts "Printing with lp command on #{printer_cups_name} of #{Settings.ns(:printer_commons).cups_server} "
|
75
|
-
# `cupsenable "#{printer_cups_name}";lp -d "#{printer_cups_name}" -h "#{Settings.ns(:printer_commons).cups_server}" -o raw "#{print_file}"`
|
76
|
-
# File.delete print_file
|
77
|
-
# # end
|
78
|
-
# end
|
79
19
|
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.
|
4
|
+
version: 2.1.3
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thecore_background_jobs
|