use of org.openhab.binding.nikobus.internal.core.NikobusModule in project openhab1-addons by openhab.
the class NikobusBinding method activate.
/**
* Activate the binding provider. After initial connection;
*/
@Override
public void activate() {
log.trace("Activating binding");
startRefreshScheduler();
log.trace("Registering modules");
for (NikobusModule module : getBindingProvider().getAllModules()) {
register(module);
}
}
use of org.openhab.binding.nikobus.internal.core.NikobusModule in project openhab1-addons by openhab.
the class NikobusGenericBindingProvider method parseItem.
/**
* Parse an item from the provided configuration string.
*
* @param item.getName()
* item name
* @param config
* string to parse
* @return parsed item
* @throws BindingConfigParseException
* if no item could be created
*/
private AbstractNikobusItemConfig parseItem(Item item, String config) throws BindingConfigParseException {
if (config == null || config.trim().length() == 0) {
throw new BindingConfigParseException("Invalid config for item " + item.getName());
}
if (config.matches(BUTTON_CONFIG_PATTERN)) {
return new Button(item.getName(), config);
}
if (config.matches(MODULE_CHANNEL_PATTERN)) {
String address = config.split(":")[0];
int channelNum = Integer.parseInt(config.split(":")[1]);
int group = channelNum > 6 ? 2 : 1;
String moduleKey = address + "-" + group;
NikobusModule module = getModule(moduleKey);
if (module == null) {
log.trace("Creating channel group {}", moduleKey);
module = new ModuleChannelGroup(address, group);
allModules.add(module);
modules.put(moduleKey, module);
}
return ((ModuleChannelGroup) module).addChannel(item.getName(), channelNum, item.getAcceptedCommandTypes());
}
throw new BindingConfigParseException("Could not determine item type from config: " + config);
}
use of org.openhab.binding.nikobus.internal.core.NikobusModule in project openhab1-addons by openhab.
the class NikobusBinding method scheduleStatusUpdateRequest.
/**
* Request a status update for the module with the given address. This will
* trigger a read command being sent to the nikobus if none is already
* pending. The read command will be send using a separate thread.
*
* @param moduleAddress
* address of the module for which to request the status update.
* @param delayedSend
* when true, sending will wait for empty bus
*/
public void scheduleStatusUpdateRequest(String moduleAddress, boolean delayedSend) {
NikobusModule module = getBindingProvider().getModule(moduleAddress);
final NikobusCommand cmd = module.getStatusRequestCommand();
cmd.setWaitForSilence(delayedSend);
cmd.setMaxRetryCount(10);
if (commandSender.isCommandRedundant(cmd)) {
// no need to send, there is a similar command already pending
return;
}
Runnable updater = new Runnable() {
@Override
public void run() {
try {
sendCommand(cmd);
} catch (Exception e) {
log.error("Error occurred during status request.", e);
}
}
};
statusRequestService.submit(updater);
}
use of org.openhab.binding.nikobus.internal.core.NikobusModule in project openhab1-addons by openhab.
the class NikobusBinding method startRefreshScheduler.
/**
* Start an automatic refresh scheduler.
*/
private void startRefreshScheduler() {
// stop any running instance..
if (refreshService != null) {
refreshService.shutdownNow();
}
if (refreshInterval > 0) {
refreshService = Executors.newScheduledThreadPool(1);
Runnable refreshCommand = new Runnable() {
@Override
public void run() {
List<NikobusModule> allModules = getBindingProvider().getAllModules();
if (allModules == null || allModules.isEmpty()) {
log.trace("No modules available to refresh");
return;
}
if (nextModuleToRefresh >= allModules.size()) {
nextModuleToRefresh = 0;
}
NikobusModule m = allModules.get(nextModuleToRefresh);
log.trace("Requesting scheduled status update for {}", m.getAddress());
NikobusCommand cmd = m.getStatusRequestCommand();
cmd.setWaitForSilence(true);
try {
sendCommand(cmd);
} catch (Exception e) {
log.error("Error occurred during scheduled status refresh.", e);
}
nextModuleToRefresh++;
}
};
// don't start the scheduler too soon, otherwise the connection may
// not be available yet on slower systems like a pi
refreshService.scheduleAtFixedRate(refreshCommand, 600, refreshInterval, TimeUnit.SECONDS);
log.debug("Refresh service started. Will refresh a module every {} seconds.", refreshInterval);
}
}
Aggregations