So I have logic handling a queue of print jobs (Which is how all of our other printer integrations are handled) that calls specific functions for each printer (in this case, the sendCommand function) that does the print job, then reports back success / fail so we can properly handle retrying or removing the print job document.
func sendCommand(_ commands: Data, portName: String, portSettings: String, timeoutMilis: uint_least32_t) -> Bool { doPrinterSetup() let succPrint = cutAndCompletePrintJob(commands) if (connectedPrinter != nil) { disconnectPrinter() } while (!hasCompleted) { //Awful solution? sleep(1) } return printSuccess }
Inside the printer’s specific logic, I establish a connection, build the print data, ect… then send it. The response here I get is that the command has sent successfully.
func printData(_ data: Data) -> Bool { if connectedPrinter == nil { return false } connectedPrinter!.addCommand(data) //Add print information connectedPrinter!.addCut(EPOS2_CUT_FEED.rawValue) //Add cut connectedPrinter!.beginTransaction() connectedPrinter!.sendData(Int(EPOS2_PARAM_DEFAULT)) return true }
The EPOS framework has a separate delegate function, onPtrReceive that is called once the print job has actually finished printing, seen below, which actually reports the success or failure of printing the job:
//EPOS Delegate func func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) { hasCompleted = true print("đđđđStatus in onPtrReceive (makeErrorMessage(status))") dispPrinterWarnings(status) DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: { self.connectedPrinter!.endTransaction() self.connectedPrinter!.disconnect() self.connectedPrinter!.clearCommandBuffer() self.connectedPrinter!.setReceiveEventDelegate(nil) }) }
Now, the problem is that I have to wait for the response to sendCommand before responding to my parent class handling the print jobs, but I am at a loss on how to actually pass a completion handler to the framework delegate function and do it the proper way. Currently I have the global variable hasCompleted which I have a while loop stopping it from responding until onPtrReceive sets it to true, however I am under the impression this is an awful solution. Any suggestions on how to handle this the proper way or is this an acceptable strategy?
Recent Comments