use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class Button method processCommand.
/**
* {@inheritDoc}
*
* When an ON command is received, a simulated button press is sent to the
* nikobus and a status refresh request is triggered for any modules linked
* to the button.
*/
@Override
public void processCommand(Command command, NikobusBinding binding) throws Exception {
log.trace("Processing command {}", command.toString());
if (command == OnOffType.ON && getAddress().length() == 8) {
// Whenever the button receives an ON command,
// we send a simulated button press to the Nikobus
int times = (type == PressType.LONG) ? NikobusCommand.MAX_REPEAT : 1;
binding.sendCommand(new NikobusCommand(getAddress(), times));
binding.sendCommand(new NikobusCommand(END_OF_TRANSMISSION));
}
notifyModulesChanged(false, binding);
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canSendGroup1Update.
@Test
public void canSendGroup1Update() throws Exception {
ModuleChannel item = group1.addChannel("test4", 4, new ArrayList<Class<? extends Command>>());
item.setState(OnOffType.ON);
group1.publishStateToNikobus(item, binding);
Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
NikobusCommand cmd = command.getAllValues().get(0);
assertEquals("$1E156C94000000FF0000FF60E149", cmd.getCommand());
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canSendGroup2Update.
@Test
public void canSendGroup2Update() throws Exception {
ModuleChannel item2 = group2.addChannel("test12", 12, new ArrayList<Class<? extends Command>>());
item2.setState(OnOffType.ON);
group2.publishStateToNikobus(item2, binding);
Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
NikobusCommand cmd = command.getAllValues().get(0);
assertEquals("$1E166C940000000000FFFF997295", cmd.getCommand());
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand 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.NikobusCommand 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