private void newItem(GPIOBindingProvider provider, String itemName) {
try {
int direction;
GPIOPin gpioPin = gpio.reservePin(provider.getPinNumber(itemName));
gpioPin.setActiveLow(provider.getActiveLow(itemName));
direction = provider.getDirection(itemName);
gpioPin.setDirection(direction);
/* Edge detection and debouncing are valid only for input pins */
if (direction == GPIOPin.DIRECTION_IN) {
long debounceInterval = provider.getDebounceInterval(itemName);
gpioPin.setEdgeDetection(GPIOPin.EDGEDETECTION_BOTH);
/* If debounceInterval isn't configured its value is GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED */
if (debounceInterval != GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED) {
gpioPin.setDebounceInterval(debounceInterval);
}
}
/* Register the pin */
try {
if (registryLock.writeLock().tryLock(REGISTRYLOCK_TIMEOUT, REGISTRYLOCK_TIMEOUT_UNITS)) {
try {
registry.put(itemName, gpioPin);
} finally {
registryLock.writeLock().unlock();
}
} else {
logger.error("Item " + itemName + " hasn't been inserted into the registry, timeout expired while waiting for registry lock");
return;
}
} catch (InterruptedException e) {
logger.error("Item " + itemName + " hasn't been inserted into the registry, thread was interrupted while waiting for registry lock");
return;
}
/* Set initial item state */
if (direction == GPIOPin.DIRECTION_IN) {
/* Item type 'Contact' */
if (gpioPin.getValue() == GPIOPin.VALUE_HIGH) {
eventPublisher.postUpdate(itemName, OpenClosedType.OPEN);
} else {
eventPublisher.postUpdate(itemName, OpenClosedType.CLOSED);
}
} else {
/* Item type 'Switch' */
if (gpioPin.getValue() == GPIOPin.VALUE_HIGH) {
eventPublisher.postUpdate(itemName, OnOffType.ON);
} else {
eventPublisher.postUpdate(itemName, OnOffType.OFF);
}
}
/* The item is of type 'Contact', register for state change notifications */
if (direction == GPIOPin.DIRECTION_IN) {
gpioPin.addEventHandler(this);
}
} catch (Exception e) {
logger.error("Error occured while creating backend object for item " +itemName + ", exception: "+ e.getMessage());
}
}