Search in sources :

Example 1 with JobStatusInfo

use of com.zebra.sdk.common.card.containers.JobStatusInfo in project openmrs-module-mirebalais by PIH.

the class ZXPSeries3PrintHandler method pollJobStatus.

private boolean pollJobStatus(ZebraCardPrinter device, Integer actionID, Printer printer) throws ConnectionException, ZebraCardException {
    boolean success = false;
    long dropDeadTime = System.currentTimeMillis() + 40000;
    long pollInterval = 500;
    // Poll job status
    JobStatusInfo jStatus = null;
    if (actionID != null) {
        do {
            jStatus = device.getJobStatus(actionID.intValue());
            // TODO get rid of this eventually?
            log.info(String.format("Job %d, Status:%s, Card Position:%s, " + "ReadyForNextJob:%s, Mag Status:%s, Contact Status:%s, Contactless Status:%s, " + "Error Code:%d, Alarm Code:%d", actionID, jStatus.printStatus, jStatus.cardPosition, jStatus.readyForNextJob, jStatus.magneticEncoding, jStatus.contactSmartCard, jStatus.contactlessSmartCard, jStatus.errorInfo.value, jStatus.alarmInfo.value));
            if (jStatus.printStatus.contains("done_ok")) {
                success = true;
                break;
            } else if (jStatus.printStatus.contains("error") || jStatus.printStatus.contains("alarm_handling")) {
                String errorMessage = jStatus.errorInfo != null ? "Err Code: " + jStatus.errorInfo.value + ", " + jStatus.errorInfo.description : "";
                String alarmMessage = jStatus.alarmInfo != null ? "Alarm Message: " + jStatus.alarmInfo.description : "";
                log.error("Printer error detected with printer " + printer.getName() + ".  " + errorMessage + ", " + alarmMessage);
                success = false;
                break;
            } else if (jStatus.printStatus.contains("cancelled")) {
                success = false;
                break;
            }
            if (System.currentTimeMillis() > dropDeadTime) {
                success = false;
                break;
            }
            try {
                Thread.sleep(pollInterval);
            } catch (InterruptedException e) {
                log.error("Error while printing to printer " + printer.getName(), e);
            }
        } while (true);
    }
    return success;
}
Also used : JobStatusInfo(com.zebra.sdk.common.card.containers.JobStatusInfo)

Example 2 with JobStatusInfo

use of com.zebra.sdk.common.card.containers.JobStatusInfo 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 3 with JobStatusInfo

use of com.zebra.sdk.common.card.containers.JobStatusInfo 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)

Example 4 with JobStatusInfo

use of com.zebra.sdk.common.card.containers.JobStatusInfo in project openmrs-module-pihcore by PIH.

the class ZXPSeries3PrintHandler method pollJobStatus.

private boolean pollJobStatus(ZebraCardPrinter device, Integer actionID, Printer printer) throws ConnectionException, ZebraCardException {
    boolean success = false;
    long dropDeadTime = System.currentTimeMillis() + 40000;
    long pollInterval = 500;
    // Poll job status
    JobStatusInfo jStatus = null;
    if (actionID != null) {
        do {
            jStatus = device.getJobStatus(actionID.intValue());
            // TODO get rid of this eventually?
            log.info(String.format("Job %d, Status:%s, Card Position:%s, " + "ReadyForNextJob:%s, Mag Status:%s, Contact Status:%s, Contactless Status:%s, " + "Error Code:%d, Alarm Code:%d", actionID, jStatus.printStatus, jStatus.cardPosition, jStatus.readyForNextJob, jStatus.magneticEncoding, jStatus.contactSmartCard, jStatus.contactlessSmartCard, jStatus.errorInfo.value, jStatus.alarmInfo.value));
            if (jStatus.printStatus.contains("done_ok")) {
                success = true;
                break;
            } else if (jStatus.printStatus.contains("error") || jStatus.printStatus.contains("alarm_handling")) {
                String errorMessage = jStatus.errorInfo != null ? "Err Code: " + jStatus.errorInfo.value + ", " + jStatus.errorInfo.description : "";
                String alarmMessage = jStatus.alarmInfo != null ? "Alarm Message: " + jStatus.alarmInfo.description : "";
                log.error("Printer error detected with printer " + printer.getName() + ".  " + errorMessage + ", " + alarmMessage);
                success = false;
                break;
            } else if (jStatus.printStatus.contains("cancelled")) {
                success = false;
                break;
            }
            if (System.currentTimeMillis() > dropDeadTime) {
                success = false;
                break;
            }
            try {
                Thread.sleep(pollInterval);
            } catch (InterruptedException e) {
                log.error("Error while printing to printer " + printer.getName(), e);
            }
        } while (true);
    }
    return success;
}
Also used : JobStatusInfo(com.zebra.sdk.common.card.containers.JobStatusInfo)

Aggregations

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