0 Replies Latest reply on Sep 26, 2017 10:36 AM by Beau Tremblay

    Retrieving a response sending print command via Socket

    Beau Tremblay

      Hello, I'm creating a UI to print labels using the GK420t network printer.   I wrote a service in Groovy to send the print command to the the printer.  I'm printing many labels at once, so I did have to troubleshoot to learn I could only print 9 labels in one command.

       

      I was hoping there might be some response that came back from the socket's input stream, but if I try to read it (see commented lines) the printing hangs.  I'd like to display some kind of feedback to the user the printing worked.  What is the preferred way to do this?  Is there a property I can read to determine the result of the last command?

       

      class ZebraPrintService {

       

         static final PRINTER_CONFIG = "ZebraPrinterConfig"
         static int PRINTER_PORT = 9100
         static ZebraCommandBuilder commandBuilder = new ZebraCommandBuilder();

         private String ip
         private Map formatMap = [:]

         private String lastCommand


         String getIp() {

         return ip ?: Lookups.findByField(PRINTER_CONFIG).value1

        }

       

        String sendPrintInstructions(List<PrintablePlate> plates) {

         final int MAX_PRINTING = 9

         def platesToPrint = plates

         while (platesToPrint.size() > 0) {

         def plateBatch = platesToPrint.take(MAX_PRINTING)

        platesToPrint = platesToPrint.drop(MAX_PRINTING)

       

         def socket = createCommandSocket()

         try {

         def cmdOut = new DataOutputStream(socket.getOutputStream())

         //def responseIn = new BufferedReader(new InputStreamReader(socket.getInputStream()))

         int msgNum = 1
         lastCommand = ""
         for (LabelPrintable p in plateBatch) {

        cmdOut.writeByte(msgNum++)

        String cmd = getPrintCommand(p);

        cmdOut.writeUTF(cmd)

        cmdOut.flush()

       

         // lastCommand << responseIn.readLine()
         }

        cmdOut.writeByte(-1)

        cmdOut.flush()

        } finally {

         if (socket != null)

        socket.close()

        }

        }

       

         return lastCommand
         }

       

        String getPrintCommand(LabelPrintable plate) {

         return commandBuilder.generatePrintCommand(getFormatForPlate(plate),plate)

        }

       

        String getFormatForPlate(LabelPrintable plate) {

         return formatMap.get(plate.plateTypeId,"DEFAULT.ZPL")

        }

       

         /**
        * Create a socket to the printer. This method is protected
        to enable mocking
        * @return a socket to the printer
        */
         protected Socket createCommandSocket() {

         return new Socket(getIp(),PRINTER_PORT)

        }

       

        String getLastCommand() {

         return lastCommand
         }

      }