Search in sources :

Example 1 with CupsBindingProvider

use of org.openhab.binding.cups.CupsBindingProvider in project openhab1-addons by openhab.

the class CupsBinding method execute.

/**
     * @{inheritDoc}
     */
@Override
public void execute() {
    if (client == null) {
        logger.warn("CupsClient is null => refresh cycle aborted!");
        return;
    }
    for (CupsBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            String printerName = provider.getPrinterName(itemName);
            WhichJobsEnum whichJobs = provider.getWhichJobs(itemName);
            if (printerName == null) {
                logger.warn("printerName isn't defined for itemName '{}'" + " => querying bus for values aborted!", itemName);
                continue;
            }
            State value = UnDefType.UNDEF;
            try {
                URL printerUrl = null;
                CupsPrinter printer = null;
                try {
                    printerUrl = new URL(printerName);
                } catch (MalformedURLException e) {
                    try {
                        printerUrl = new URL("http://" + host + ":" + port + "/printers/" + printerName);
                    } catch (MalformedURLException e1) {
                        logger.warn("Failed to construct URL for printer name: {}", printerName);
                    }
                }
                if (printerUrl == null) {
                    // try to find printer by name
                    for (CupsPrinter pr : client.getPrinters()) {
                        if (pr.getName().equalsIgnoreCase(printerName)) {
                            printer = pr;
                            break;
                        }
                    }
                } else {
                    printer = client.getPrinter(printerUrl);
                }
                if (printer != null) {
                    value = new DecimalType(client.getJobs(printer, whichJobs, "", false).size());
                    logger.debug("Found printer {}#{} with value {}", printerUrl, whichJobs, value);
                } else {
                    logger.info("There is no printer for path {}", printerUrl);
                }
            } catch (IOException ioe) {
                logger.warn("Couldn't establish network connection for printer '{}'", printerName, ioe);
            } catch (Exception e) {
                logger.error("Couldn't get printer '{}' from cups server", printerName, e);
            } finally {
                eventPublisher.postUpdate(itemName, value);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) State(org.openhab.core.types.State) CupsBindingProvider(org.openhab.binding.cups.CupsBindingProvider) DecimalType(org.openhab.core.library.types.DecimalType) WhichJobsEnum(org.cups4j.WhichJobsEnum) IOException(java.io.IOException) CupsPrinter(org.cups4j.CupsPrinter) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 CupsPrinter (org.cups4j.CupsPrinter)1 WhichJobsEnum (org.cups4j.WhichJobsEnum)1 CupsBindingProvider (org.openhab.binding.cups.CupsBindingProvider)1 DecimalType (org.openhab.core.library.types.DecimalType)1 State (org.openhab.core.types.State)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1