Search in sources :

Example 6 with NikobusCommand

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;
}
Also used : NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand)

Example 7 with NikobusCommand

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);
    }
}
Also used : State(org.openhab.core.types.State) PercentType(org.openhab.core.library.types.PercentType) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand)

Example 8 with NikobusCommand

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());
}
Also used : Command(org.openhab.core.types.Command) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) PercentType(org.openhab.core.library.types.PercentType) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) Test(org.junit.Test)

Example 9 with NikobusCommand

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());
}
Also used : NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) Test(org.junit.Test)

Example 10 with NikobusCommand

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);
}
Also used : Command(org.openhab.core.types.Command) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) NikobusCommand(org.openhab.binding.nikobus.internal.core.NikobusCommand) Test(org.junit.Test)

Aggregations

NikobusCommand (org.openhab.binding.nikobus.internal.core.NikobusCommand)13 Test (org.junit.Test)8 Command (org.openhab.core.types.Command)6 NikobusModule (org.openhab.binding.nikobus.internal.core.NikobusModule)2 PercentType (org.openhab.core.library.types.PercentType)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 ArrayList (java.util.ArrayList)1 State (org.openhab.core.types.State)1