Search in sources :

Example 1 with ConnectionException

use of com.zebra.sdk.comm.ConnectionException in project openmrs-module-mirebalais by PIH.

the class ZXPSeries3PrintHandler method cleanUp.

private void cleanUp(Connection connection, ZebraCardPrinter device, Integer jobId, ZebraGraphics graphics, Printer printer, boolean success) {
    // need to cancel any jobs before it will allow you to destroy the printer
    if (!success && device != null && jobId != null) {
        JobStatusInfo jStatus = null;
        try {
            long waitToCancelInterval = 500;
            long dropDeadTime = System.currentTimeMillis() + 5000;
            jStatus = device.getJobStatus(jobId.intValue());
            // TODO figure out the right way to determine if a job needs to be cancelled or not
            while (jStatus != null && !jStatus.printStatus.contains("cancelled") && System.currentTimeMillis() < dropDeadTime) {
                device.cancel(jobId.intValue());
                Thread.sleep(waitToCancelInterval);
                log.info("Cancelled job " + jobId);
            }
        } catch (ConnectionException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        } catch (ZebraCardException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        } catch (InterruptedException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        }
    }
    try {
        if (device != null) {
            device.destroy();
            log.info("Destroyed printer handle for job " + jobId + " " + device.toString());
        }
    } catch (ZebraCardException e) {
        log.error("Unable to destroy printer object for printer " + printer.getName(), e);
    }
    if (graphics != null) {
        graphics.close();
    }
    if (connection != null) {
        try {
            connection.close();
            log.info("Closed connection for job " + jobId + ", Connected:  " + connection.isConnected());
        } catch (ConnectionException e) {
            log.error("Unable to close connection with printer " + printer.getName(), e);
        }
    }
}
Also used : JobStatusInfo(com.zebra.sdk.common.card.containers.JobStatusInfo) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException)

Example 2 with ConnectionException

use of com.zebra.sdk.comm.ConnectionException in project openmrs-module-pihcore by PIH.

the class ZXPSeries3PrintHandler method cleanUp.

private void cleanUp(Connection connection, ZebraCardPrinter device, Integer jobId, ZebraGraphics graphics, Printer printer, boolean success) {
    // need to cancel any jobs before it will allow you to destroy the printer
    if (!success && device != null && jobId != null) {
        JobStatusInfo jStatus = null;
        try {
            long waitToCancelInterval = 500;
            long dropDeadTime = System.currentTimeMillis() + 5000;
            jStatus = device.getJobStatus(jobId.intValue());
            // TODO figure out the right way to determine if a job needs to be cancelled or not
            while (jStatus != null && !jStatus.printStatus.contains("cancelled") && System.currentTimeMillis() < dropDeadTime) {
                device.cancel(jobId.intValue());
                Thread.sleep(waitToCancelInterval);
                log.info("Cancelled job " + jobId);
            }
        } catch (ConnectionException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        } catch (ZebraCardException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        } catch (InterruptedException e) {
            log.error("Error while attempting to cancel job on printer " + printer.getName() + ", Status: " + (jStatus != null ? jStatus.printStatus : ""), e);
        }
    }
    try {
        if (device != null) {
            device.destroy();
            log.info("Destroyed printer handle for job " + jobId + " " + device.toString());
        }
    } catch (ZebraCardException e) {
        log.error("Unable to destroy printer object for printer " + printer.getName(), e);
    }
    if (graphics != null) {
        graphics.close();
    }
    if (connection != null) {
        try {
            connection.close();
            log.info("Closed connection for job " + jobId + ", Connected:  " + connection.isConnected());
        } catch (ConnectionException e) {
            log.error("Unable to close connection with printer " + printer.getName(), e);
        }
    }
}
Also used : JobStatusInfo(com.zebra.sdk.common.card.containers.JobStatusInfo) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException)

Aggregations

ConnectionException (com.zebra.sdk.comm.ConnectionException)2 JobStatusInfo (com.zebra.sdk.common.card.containers.JobStatusInfo)2 ZebraCardException (com.zebra.sdk.common.card.exceptions.ZebraCardException)2