Class: PrintWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/print_worker.rb

Instance Method Summary collapse

Instance Method Details

#check_status(printer) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/workers/print_worker.rb', line 40

def check_status printer
  begin
    s = TCPSocket.new(printer, 9100)
    # Must create intepolation between item and template
    # Printer.template può essere anche
    # una parola di comando epr chiedere lo stato della stampante, solo nel caso sia ok,
    # Allora mando la stampa
    s.puts("~HS")
    # Attende per la risposta (si mette in wait)
    response = []
    while (response_text = s.gets)
      response << response_text
      break if response.count == 3
    end
    s.close
    Rails.logger.info "PrintIt: RESPONSE: #{response.inspect}"
    first = response[0].split(",")
    second = response[1].split(",")
    return "HEAD UP" if second[2].to_i == 1
    return "RIBBON OUT" if second[3].to_i == 1
    return "PAPER OUT" if first[1].to_i == 1
    return "PAUSE" if first[2].to_i == 1
    return "OK"
  rescue
    Rails.logger.info "PrintIt: STATUS: UNREACHABLE"
    return "UNREACHABLE"
  end
end

#perform(printer, text) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/workers/print_worker.rb', line 4

def perform  printer, text
  # Checking status only for telnet printers, samba ones cannot be checked
  is_telnet_printer = IPAddr.new(printer).ipv4? rescue false
  status = if is_telnet_printer
      check_status(printer)
  else
      "OK"
  end 
  if status == "OK"
      begin
          if is_telnet_printer
              # Use TCPSocket
              TCPSocket.new(printer, 9100) do |s|
                  # Rails.logger.debug "TEMPERATURE: #{text}"
                  s.puts(text)
              end
              # @printed += 1
              return true
          else
              # Use File.open to print on a windows share
              # Must be in this format: "\\\\host\\share"
              # remember to escape \
              File.open(printer) do |f|
                  f.print(text)
              end
          end
      rescue
          @pjob.update(iserror: true, description: "PRINTER ERROR: TIMEOUT")
          return false
      end
  else
    @pjob.update(iserror: true, description: "PRINTER ERROR: #{status}")
    return false
  end
end