require "io/console" module Sapos module Print include Thor::Actions class CLI < Thor desc "setup", "Setup Wizard" def setup info config = {} url = ask("URL de Configuración: ") if url.eql?('manual') data = {} data["channel"] = ask("Canal:") data["key"] = ask("Llave de la Cola:") else response = RestClient.get(url) data = JSON.parse(response) end config_file = "#{Sapos::Print.app_directory}/config.yml" user_id = nil if File.exist?(config_file) template = ERB.new(File.read(config_file)) result = YAML.load(template.result(binding)) user_id = result[:user_id] end user_id = SecureRandom.uuid if user_id.nil? config[:q] = data["channel"] config[:key] = data["key"] config[:printer] = ask("Nombre de la impresora default: ") config[:adapter] = ask("Adaptador: ", limited_to: ["console", "cups", "windows"]) config[:interface] = ask("Interface", limited_to: ["text", "escp"]) config[:user_id] = user_id config[:duplicate_control] = ask("¿Control Duplicados por MSGID?", limited_to: ["si", "no"]) config[:printers] = [config[:printer]] config[:interfaces] = [config[:interface]] while yes?("¿Tiene impresoras adicionales?") name = ask("Nombre de la impresora adicional: ") interface = ask("Interfaz de la impresora adicional: ", limited_to: ["text", "escp"]) config[:printers] << name config[:interfaces] << interface end if yes?("¿Esta seguro que desea guardar la configuración?") Sapos::Print::Configuration.write(config) ap config say "La configuración ha sido guardada!" end end desc "start", "run service" option :verbose, type: :boolean def start info config = Sapos::Print.config puts ["Q=#{config.q.to_s}", "Printer=#{config.printer.to_s}", "Adapter=#{config.adapter.to_s}", "Interface=#{config.interface}", "DC=#{config.duplicate_control?}"].join(";") print_dir = File.join(Sapos::Print.app_directory, 'print') if File.exists?(print_dir) Dir.glob("#{print_dir}/*.print").each do |x| FileUtils.rm_rf(x) if File.ctime(x) < Time.now - 3600 end end config.verbose = options[:verbose] Sapos::Print::QReader.printer_config = config Sapos::Print::QReader.verbose = options[:verbose] Sapos::Print::QReader.run! end desc "print", "demo print" option :printer_name, type: :string def print info config = Sapos::Print.config printer_name = options[:printer_name] if printer_name.present? if config.printers.any? && config.printers.include?(printer_name) config.printer = printer_name end end puts "Printing to: #{config.printer}" printer = Printer.new(config) printer.print(document: "This is a test\nprint page\nwithno special commands", document_number: "test", print_control: true) end desc "windows_test_print", "test printer" def windows_test_print info filename = "test.print" dir = File.join(Dir.home,".sapos-print") FileUtils.mkdir_p dir unless File.exists?(dir) dir print_dir = File.join(dir, 'print') FileUtils.mkdir_p print_dir unless File.exists?(print_dir) filename = File.join(print_dir, filename) puts "Test file Path: #{filename}" File.open(filename, 'w'){|f| f.write("Test\nTest\nTest")} puts "Execute COPY Command" command = "COPY "+"'"+filename.gsub("/","\\")+"'"+'"\\\\%COMPUTERNAME%\\'+"TM" if !system(command) puts "ERROR" else puts "OK" end end desc "test_q", "q reader test" def test_q config = Sapos::Print.config puts ["Q=#{config.q.to_s}", "Printer=#{config.printer.to_s}", "Adapter=#{config.adapter.to_s}", "Interface=#{config.interface}", "DC=#{config.duplicate_control?}"].join(";") Sapos::Print::QReader.printer_config = config reader = QReader.new puts reader.printer message = {:document => "doc", :printer => "Daniel"} ap reader.printers Sapos::Print.config.printers.each do |p| printer = reader.printers[p] puts "Printing to: #{printer.config.printer}" message[:document] = "Docu. #{p}" printer.print(message) end end no_tasks do def info if @printed.nil? @printed ||= begin print_wrapped("=+" * 40) say("SaposPrint v#{Sapos::Print::VERSION}") say "... by Agile Software Development [Copyright © 2016-#{Time.now.year}]" print_wrapped("=+" * 40) puts "\n" end end end end end end end