lib/escper/printer.rb
# Opens all printers as passed to +new+
Opens all printers as passed to +new+
Opens all printers as passed to + new +
def open
      Escper.log "[open] ============"
      @vendor_printers.size.times do |i|
        p = @vendor_printers[i]
        name = p.name
        path = p.path
        codepage = p.codepage
        baudrate = p.baudrate
        if Escper.use_safe_device_path == true
          sanitized_path = path.gsub(/[\/\s'"\&\^\$\#\!;\*]/,'_').gsub(/[^\w\/\.\-@]/,'')
          path = File.join(Escper.safe_device_path, @subdirectory, "escpos", "#{sanitized_path}.bill")
          FileUtils.mkdir_p(File.dirname(path)) unless File.exists?(File.dirname(path))
          @file_mode = 'ab'
        end
        Escper.log "[open] Trying to open #{ name }@#{ path }@#{ baudrate }bps ..."
        pid = p.id ? p.id : i 
        if @mode == "local"
          if /\d+\.\d+\.\d+\.\d+:\d+/.match(path)
            ip_addr = /(\d+\.\d+\.\d+\.\d+)/.match(path)[1]
            port = /\d+\.\d+\.\d+\.\d+:(\d+)/.match(path)[1]
            Escper.log "[open]   Parsed IP #{ ip_addr} on port #{ port }"
            begin
              printer = nil
              Escper.log "[open]   Attempting to open TCPSocket at #{ ip_addr} on port #{ port } ... "
              Timeout.timeout(2) do
                printer = TCPSocket.new ip_addr, port
              end
              Escper.log "[open]   Success for TCPSocket: #{ printer.inspect }"
              @open_printers.merge! pid => {
                :name => name,
                :path => path,
                :copies => p.copies,
                :device => printer,
                :codepage => codepage
              }
              next
            rescue Errno::ECONNREFUSED
              Escper.log "[open]   TCPSocket ERROR: Connection refused at IP #{ ip_addr} on port #{ port }. Skipping this printer."
              next
            rescue Timeout::Error
              Escper.log "[open]   TCPSocket ERROR: Timeout #{ ip_addr} on port #{ port }. Skipping this printer."
              next
            rescue => e
              Escper.log "[open]   TCPSocket ERROR: Failed to open: #{ e.inspect }"
              next
            end
          else
            Escper.log "[open]   Path #{ path } is not in IP:port format. Not trying to open printer as TCPSocket."
          end
        else
          Escper.log "[open]   Mode is #{ @mode }. Not trying to open printer as TCPSocket."
        end
        begin
          printer = SerialPort.new path, baudrate
          @open_printers.merge! pid => {
            :name => name,
            :path => path,
            :copies => p.copies,
            :device => printer,
            :codepage => codepage
          }
          Escper.log "[open]   Success for SerialPort: #{ printer.inspect }"
          next
        rescue => e
          Escper.log "[open]   Failed to open as SerialPort: #{ e.inspect }"
        end
        begin
          printer = File.open path, @file_mode
          @open_printers.merge! pid => {
            :name => name,
            :path => path,
            :copies => p.copies,
            :device => printer,
            :codepage => codepage
          }
          Escper.log "[open]   Success for File: #{ printer.inspect }"
          next
        rescue Errno::EBUSY
          Escper.log "[open]   The File #{ path } is already open."
          Escper.log "[open]      Trying to reuse already opened printers."
          previously_opened_printers = @open_printers.clone
          previously_opened_printers.each do |key, val|
            Escper.log "[open]      Trying to reuse already opened File #{ key }: #{ val.inspect }"
            if val[:path] == p[:path] and val[:device].class == File
              Escper.log "[open]      Reused."
              @open_printers.merge! pid => {
                :name => name,
                :path => path,
                :copies => p.copies,
                :device => val[:device],
                :codepage => codepage
              }
              break
            end
          end
          unless @open_printers.has_key? p.id
            path = File.join(@fallback_root_path, 'tmp')
            printer = File.open(File.join(path, "#{ p.id }-#{ name }-fallback-busy.salor"), @file_mode)
            @open_printers.merge! pid => {
              :name => name,
              :path => path,
              :copies => p.copies,
              :device => printer,
              :codepage => codepage
            }
            Escper.log "[open]      Failed to open as either SerialPort or USB File and resource IS busy. This should not have happened. Created #{ printer.inspect } instead."
          end
          next
        rescue => e
          path = File.join(@fallback_root_path, 'tmp')
          printer = File.open(File.join(path, "#{ p.id }-#{ name }-fallback-notbusy.salor"), @file_mode)
          @open_printers.merge! pid => {
            :name => name,
            :path => path,
            :copies => p.copies,
            :device => printer,
            :codepage => codepage
          }
          Escper.log "[open]    Failed to open as either SerialPort or USB File and resource is NOT busy. Created #{ printer.inspect } instead."
        end
      end
    end
def open Escper . log "[open] ============" @vendor_printers . size . times do | i | p = @vendor_printers [ i ] name = p . name path = p . path codepage = p . codepage baudrate = p . baudrate if Escper . use_safe_device_path == true sanitized_path = path . gsub ( / [ \/ \s '" \& \^ \$ \# \! ; \* ] / , '_' ) . gsub ( / [^ \w \/ \. \- @] / , '' ) path = File . join ( Escper . safe_device_path , @subdirectory , "escpos" , "#{sanitized_path}.bill" ) FileUtils . mkdir_p ( File . dirname ( path ) ) unless File . exists? ( File . dirname ( path ) ) @file_mode = 'ab' end Escper . log "[open] Trying to open #{ name }@#{ path }@#{ baudrate }bps ..." pid = p . id ? p . id : i if @mode == "local" if / \d + \. \d + \. \d + \. \d +: \d + / . match ( path ) ip_addr = / ( \d + \. \d + \. \d + \. \d +) / . match ( path ) [ 1 ] port = / \d + \. \d + \. \d + \. \d +:( \d +) / . match ( path ) [ 1 ] Escper . log "[open]   Parsed IP #{ ip_addr} on port #{ port }" begin printer = nil Escper . log "[open]   Attempting to open TCPSocket at #{ ip_addr} on port #{ port } ... " Timeout . timeout ( 2 ) do printer = TCPSocket . new ip_addr , port end Escper . log "[open]   Success for TCPSocket: #{ printer.inspect }" @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => printer , :codepage => codepage } next rescue Errno :: ECONNREFUSED Escper . log "[open]   TCPSocket ERROR: Connection refused at IP #{ ip_addr} on port #{ port }. Skipping this printer." next rescue Timeout :: Error Escper . log "[open]   TCPSocket ERROR: Timeout #{ ip_addr} on port #{ port }. Skipping this printer." next rescue => e Escper . log "[open]   TCPSocket ERROR: Failed to open: #{ e.inspect }" next end else Escper . log "[open]   Path #{ path } is not in IP:port format. Not trying to open printer as TCPSocket." end else Escper . log "[open]   Mode is #{ @mode }. Not trying to open printer as TCPSocket." end begin printer = SerialPort . new path , baudrate @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => printer , :codepage => codepage } Escper . log "[open]   Success for SerialPort: #{ printer.inspect }" next rescue => e Escper . log "[open]   Failed to open as SerialPort: #{ e.inspect }" end begin printer = File . open path , @file_mode @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => printer , :codepage => codepage } Escper . log "[open]   Success for File: #{ printer.inspect }" next rescue Errno :: EBUSY Escper . log "[open]   The File #{ path } is already open." Escper . log "[open]      Trying to reuse already opened printers." previously_opened_printers = @open_printers . clone previously_opened_printers . each do | key , val | Escper . log "[open]      Trying to reuse already opened File #{ key }: #{ val.inspect }" if val [ :path ] == p [ :path ] and val [ :device ] . class == File Escper . log "[open]      Reused." @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => val [ :device ] , :codepage => codepage } break end end unless @open_printers . has_key? p . id path = File . join ( @fallback_root_path , 'tmp' ) printer = File . open ( File . join ( path , "#{ p.id }-#{ name }-fallback-busy.salor" ) , @file_mode ) @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => printer , :codepage => codepage } Escper . log "[open]      Failed to open as either SerialPort or USB File and resource IS busy. This should not have happened. Created #{ printer.inspect } instead." end next rescue => e path = File . join ( @fallback_root_path , 'tmp' ) printer = File . open ( File . join ( path , "#{ p.id }-#{ name }-fallback-notbusy.salor" ) , @file_mode ) @open_printers . merge! pid => { :name => name , :path => path , :copies => p . copies , :device => printer , :codepage => codepage } Escper . log "[open]    Failed to open as either SerialPort or USB File and resource is NOT busy. Created #{ printer.inspect } instead." end end end
Opens all printers as passed to +new+
Opens all printers as passed to + new +
# assign incrementing id if none given # ================ TCPSOCKET PRINTERS ============================= # Writing to IP Addresses is only supported in local mode # ================ SERIALPORT PRINTERS ============================= # ================ FILE PRINTERS ============================= # This happens when there are 2 printes with the same path