Search in sources :

Example 1 with XBeeException

use of com.rapplogic.xbee.api.XBeeException in project openhab1-addons by openhab.

the class DiyOnXBeeBinding method internalReceiveCommand.

/**
	 * 
	 * @return if the command was sent successfully
	 */
private boolean internalReceiveCommand(DiyOnXBeeBindingProvider provider, String itemName, Command command) {
    final String remote = provider.getRemote(itemName);
    final int[] remoteAddress = FormatUtil.fromReadableAddress(remote);
    Item item;
    try {
        item = itemRegistry.getItem(itemName);
    } catch (ItemNotFoundException e1) {
        logger.error("unable to get item {}", itemName, e1);
        return false;
    }
    final String commandValue = createCommand(item, command);
    if (commandValue == null) {
        logger.warn("unable to create command {} for item {}", commandValue, itemName);
        return false;
    } else {
        logger.debug("created command {} for item {}", commandValue, itemName);
    }
    final String commandString = new StringBuilder().append(provider.getId(itemName)).append('=').append(commandValue).append('\n').toString();
    final ZNetTxRequest request = new ZNetTxRequest(new XBeeAddress64(remoteAddress), createPayload(commandString));
    xbeeUsageLock.lock();
    try {
        if (xbee == null) {
            logger.error("cannot send command to {}  as the XBee module isn't initialized", itemName);
            return false;
        } else {
            // TODO:
            final XBeeResponse response = xbee.sendSynchronous(request);
            // response?
            return true;
        }
    } catch (XBeeTimeoutException e) {
        logger.error("failed sending {} to {}", command, itemName, e);
    } catch (XBeeException e) {
        logger.error("failed sending {} to {}", command, itemName, e);
    } finally {
        xbeeUsageLock.unlock();
    }
    return false;
}
Also used : Item(org.openhab.core.items.Item) ZNetTxRequest(com.rapplogic.xbee.api.zigbee.ZNetTxRequest) XBeeAddress64(com.rapplogic.xbee.api.XBeeAddress64) XBeeException(com.rapplogic.xbee.api.XBeeException) XBeeResponse(com.rapplogic.xbee.api.XBeeResponse) XBeeTimeoutException(com.rapplogic.xbee.api.XBeeTimeoutException) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 2 with XBeeException

use of com.rapplogic.xbee.api.XBeeException in project openhab1-addons by openhab.

the class DiyOnXBeeBinding method updated.

@Override
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    {
        final String serialPort = (String) properties.get("serialPort");
        if (StringUtils.isNotBlank(serialPort)) {
            this.serialPort = serialPort;
        }
    }
    {
        final String baudRate = (String) properties.get("baudRate");
        if (StringUtils.isNotBlank(baudRate)) {
            this.baudRate = Integer.parseInt(baudRate);
        }
    }
    String canonical = serialPort;
    try {
        // This code below enables to use a device name in
        // /dev/serial/by-id/... on linux
        File device = new File(serialPort);
        if (device.canRead()) {
            canonical = device.getCanonicalPath();
        }
    } catch (IOException e1) {
        logger.info("unable to get canonical path for '{}'", serialPort);
        canonical = serialPort;
    }
    xbeeSetupLock.lock();
    try {
        if (xbee != null) {
            xbee.removePacketListener(this);
            xbee.close();
            xbee = null;
        }
        xbee = new XBee();
        try {
            logger.info("opening XBee communication on '{}'", canonical);
            xbee.open(canonical, baudRate);
        } catch (XBeeException e) {
            logger.error("failed to open connection to XBee module", e);
            xbee = null;
            return;
        }
    } finally {
        xbeeSetupLock.unlock();
    }
    xbee.addPacketListener(this);
}
Also used : XBee(com.rapplogic.xbee.api.XBee) XBeeException(com.rapplogic.xbee.api.XBeeException) IOException(java.io.IOException) File(java.io.File)

Aggregations

XBeeException (com.rapplogic.xbee.api.XBeeException)2 XBee (com.rapplogic.xbee.api.XBee)1 XBeeAddress64 (com.rapplogic.xbee.api.XBeeAddress64)1 XBeeResponse (com.rapplogic.xbee.api.XBeeResponse)1 XBeeTimeoutException (com.rapplogic.xbee.api.XBeeTimeoutException)1 ZNetTxRequest (com.rapplogic.xbee.api.zigbee.ZNetTxRequest)1 File (java.io.File)1 IOException (java.io.IOException)1 Item (org.openhab.core.items.Item)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1