Class: Sapos::Print::Printer
- Inherits:
-
Object
- Object
- Sapos::Print::Printer
- Defined in:
- lib/sapos/print/printer.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #data(raw_message) ⇒ Object
-
#initialize(config) ⇒ Printer
constructor
A new instance of Printer.
- #print(args = {}) ⇒ Object
- #send_to_printer(opts = {}) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(config) ⇒ Printer
Returns a new instance of Printer.
8 9 10 |
# File 'lib/sapos/print/printer.rb', line 8 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/sapos/print/printer.rb', line 5 def config @config end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/sapos/print/printer.rb', line 6 def name @name end |
Instance Method Details
#data(raw_message) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sapos/print/printer.rb', line 72 def data() case @config.interface when 'text' return when 'escp' esc = "\x1B" #ESC byte in hex notation newLine = "\x0A" #LF byte in hex notation cmds = esc + "@" cmds += esc + "!"+"\00" cmds += cmds += newLine + newLine cmds += "\x1D\x56\x42\x03" # Cut Paper cmds += "\x1B\x70\x00\x19\xFA" # Open drawer return cmds else raise "Unknown interface: #{@config.interface}" end end |
#print(args = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 68 69 70 |
# File 'lib/sapos/print/printer.rb', line 16 def print(args = {}) = args[:document] log_text = "id=#{args[:id]},doc=#{args[:document_number]},control=#{args[:print_control] ? "SI" : "NO"},cache=0\t" log_text += "printer=#{args[:printer]}\t" if args[:printer] filename = "#{args[:document_number]}.print" if args[:document_number] filename = SecureRandom.hex if filename.nil? result = false raw = .force_encoding("UTF-8") msgid = args[:id] if @config.duplicate_control? && @config.cache.include?(msgid) log_text += " => DUPLICADO (DC)" log_text.gsub!("cache=0", "cache=#{@config.cache.size}") puts log_text return true end begin if args[:print_control] && args[:document_number] print_dir = File.join(Sapos::Print.app_directory, 'print') FileUtils.mkdir_p print_dir unless File.exists?(print_dir) filename = File.join(print_dir, filename) if !File.exists?(filename) printable = data() File.open(filename, 'w'){|f| f.write(printable)} result = send_to_printer({file: filename}) #puts "Result: #{result}" log_text += " => OK" else result = true log_text += " => DUPLICADO" end else printable = data() File.open(filename, 'w'){|f| f.write(printable)} result = send_to_printer({file: filename}) File.delete(filename) if File.exists?(filename) #puts "Result: #{result}" log_text += " => OK" end if @config.duplicate_control? @config.cache << msgid @config.cache.shift if @config.cache.size > 30 log_text.gsub!("cache=0", "cache=#{@config.cache.size}") end puts log_text rescue => e puts "#{e.message}" self.errors = e. result = false ensure #File.delete(filename) if File.exists?(filename) return result end end |
#send_to_printer(opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sapos/print/printer.rb', line 91 def send_to_printer(opts = {}) case @config.adapter when 'console' puts "=> CONSOLE: file: #{opts[:file]}<=\n\n" puts "=> Printer: #{@config.printer}" puts File.read(opts[:file]) puts "\n=> END CONSOLE =<" return true when 'cups' file = opts.delete(:file) = [] << "-d #{@printer_name}" << "-o raw" if opts[:raw].present? command = "lp #{command_options.join(" ")} #{file}" puts "CMD => #{command}" if @config.verbose if !system(command) puts "Not executed" return false end return true when 'windows' #command = "copy" + ' "' + filename + '" ' + '"\\\\%COMPUTERNAME%\\'+"TM" filename = opts[:file].to_s.gsub("/","\\") command = "COPY " + '"' + filename + '" ' + '"\\\\%COMPUTERNAME%\\' + @config.printer #command = "copy #{opts[:file]} "+'"\\\\%COMPUTERNAME%\\'[email protected] if !system(command) puts "Not executed" return false end return true end end |
#to_h ⇒ Object
12 13 14 |
# File 'lib/sapos/print/printer.rb', line 12 def to_h {config: config.printer} end |