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;
}
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);
}
Aggregations