use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class NikobusCommandProvider method _nikobus.
/**
* Nikobus command implementation.
*
* @param intp
* commandInterpreter
*
* @return null
*/
public Object _nikobus(CommandInterpreter intp) {
try {
String cmd = intp.nextArgument();
if (cmd.equals("help")) {
intp.println(getHelp());
return null;
}
if (cmd.equals("connect")) {
binding.connect();
return null;
}
if (cmd.equals("send")) {
String data = intp.nextArgument();
if (data != null && data.length() > 0) {
binding.sendCommand(new NikobusCommand(data));
} else {
intp.println("Missing command argument. Enclose command in single quotes. E.g. nikobus send '#N0D4CE6'");
intp.print(getHelp());
}
return null;
}
if (cmd.equals("status")) {
intp.println(binding.getConnectionStatus());
return null;
}
String address = intp.nextArgument();
if (address == null || address.length() != 4) {
intp.println(getHelp());
return null;
} else {
address = address.toUpperCase();
}
return null;
} catch (Exception e) {
intp.print(getHelp());
}
return null;
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroup method publishStateToNikobus.
/**
* Push the state of all channels to the Nikobus.
*
* @param moduleChannel
*/
public void publishStateToNikobus(ModuleChannel moduleChannel, NikobusBinding binding) {
log.trace("Publishing group {}-{} status to eventbus and nikobus", address, group);
// update the channel on the event bus..
binding.postUpdate(moduleChannel.getName(), moduleChannel.getState());
StringBuilder command = new StringBuilder();
command.append(statusUpdateGroup);
command.append(address);
for (int i = 0; i < 6; i++) {
if (channels[i] == null) {
// no channel defined
command.append(LOW_BYTE);
continue;
}
State channelState = channels[i].getState();
if (channelState == null || channelState.equals(OnOffType.OFF) || channelState.equals(PercentType.ZERO)) {
command.append(LOW_BYTE);
} else if (channelState.equals(UpDownType.UP)) {
command.append(UP_BYTE);
} else if (channelState.equals(UpDownType.DOWN)) {
command.append(DOWN_BYTE);
} else if (channelState instanceof PercentType) {
// calculate dimmer value...
PercentType currentState = (PercentType) channelState;
int value = BigDecimal.valueOf(255).multiply(currentState.toBigDecimal()).divide(BigDecimal.valueOf(100), 0, BigDecimal.ROUND_UP).intValue();
command.append(StringUtils.leftPad(Integer.toHexString(value), 2, "0").toUpperCase());
} else {
command.append(HIGH_BYTE);
}
}
command.append(HIGH_BYTE);
NikobusCommand cmd = new NikobusCommand(CRCUtil.appendCRC2(STATUS_CHANGE_CMD + CRCUtil.appendCRC(command.toString())));
try {
binding.sendCommand(cmd);
} catch (Exception e) {
log.error("Error sending command.", e);
}
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canSendGroup1DimmerUpdate.
@Test
public void canSendGroup1DimmerUpdate() throws Exception {
ModuleChannel item = group1.addChannel("test4", 4, new ArrayList<Class<? extends Command>>());
item.setState(new PercentType(25));
group1.publishStateToNikobus(item, binding);
Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
NikobusCommand cmd = command.getAllValues().get(0);
assertEquals("$1E156C94000000400000FF45DE7B", cmd.getCommand());
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canRequestGroup2Status.
@Test
public void canRequestGroup2Status() throws Exception {
NikobusCommand cmd = group2.getStatusRequestCommand();
assertEquals("$10176C948715BB", cmd.getCommand());
assertEquals("$1C6C94", cmd.getAck());
}
use of org.openhab.binding.nikobus.internal.core.NikobusCommand in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canProcessGroup1StatusUpdate.
@Test
public void canProcessGroup1StatusUpdate() {
ModuleChannel item = group1.addChannel("test5", 5, new ArrayList<Class<? extends Command>>());
item.setState(OnOffType.OFF);
group1.processNikobusCommand(new NikobusCommand("$0512"), binding);
group1.processNikobusCommand(new NikobusCommand("$1C6C940000000000FF00557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test5", OnOffType.ON);
group1.processNikobusCommand(new NikobusCommand("$0512"), binding);
group1.processNikobusCommand(new NikobusCommand("$1C6C9400000000FF00FF557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test5", OnOffType.OFF);
}
Aggregations