Search in sources :

Example 31 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class LcnGenericBindingProvider method processBindingConfiguration.

/**
     * Item processing for the LCN bindings.
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    Matcher matcher = PATTERN_BINDING_GENERAL.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException(bindingConfig + "' contains no valid binding!");
    }
    matcher.reset();
    LcnBindingConfig bc = new LcnBindingConfig(item);
    while (matcher.find()) {
        String binding = matcher.group(1);
        if (binding != null && !binding.trim().isEmpty()) {
            String openHabCmd = null;
            String connId, lcnTarget;
            Matcher openHabMatcher = PATTERN_BINDING_WITH_OPENHAB.matcher(binding);
            Matcher pureMatcher = PATTERN_BINDING_PURE.matcher(binding);
            if (openHabMatcher.matches()) {
                openHabCmd = openHabMatcher.group(1);
                connId = openHabMatcher.group(2);
                lcnTarget = openHabMatcher.group(3);
            } else if (pureMatcher.matches()) {
                connId = pureMatcher.group(1);
                lcnTarget = pureMatcher.group(2);
            } else {
                throw new BindingConfigParseException("Invalid binding configuration for " + binding + "!");
            }
            String lcnShort = resolveMappings(lcnTarget, openHabCmd);
            if (lcnShort == null || lcnShort.equals(openHabCmd)) {
                lcnShort = lcnTarget;
            }
            Command cmd = openHabCmd == null ? TypeParser.parseCommand(new StringItem("").getAcceptedCommandTypes(), "") : openHabCmd.equals("%i") ? new StringType("%i") : TypeParser.parseCommand(item.getAcceptedCommandTypes(), openHabCmd);
            bc.add(new LcnBindingConfig.Mapping(cmd, connId, lcnShort));
        }
    }
    // Finished
    this.addBindingConfig(item, bc);
    for (LcnAddrMod addr : bc.getRelatedModules()) {
        HashSet<String> l = this.itemNamesByModulCache.get(addr);
        if (l == null) {
            l = new HashSet<String>();
            this.itemNamesByModulCache.put(addr, l);
        }
        l.add(item.getName());
    }
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) StringItem(org.openhab.core.library.items.StringItem) LcnAddrMod(org.openhab.binding.lcn.common.LcnAddrMod)

Example 32 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class MochadX10Binding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    MochadX10BindingConfig deviceConfig = getConfigForItemName(itemName);
    if (deviceConfig == null) {
        return;
    }
    String address = deviceConfig.getAddress();
    String tm = deviceConfig.getTransmitMethod();
    String commandStr = "none";
    Command previousCommand = lastIssuedCommand.get(address);
    int level = -1;
    if (command instanceof OnOffType) {
        commandStr = OnOffType.ON.equals(command) ? "on" : "off";
        level = OnOffType.ON.equals(command) ? 100 : 0;
    } else if (command instanceof UpDownType) {
        commandStr = UpDownType.UP.equals(command) ? "bright" : "dim";
        level = UpDownType.UP.equals(command) ? 100 : 0;
    } else if (command instanceof StopMoveType) {
        if (StopMoveType.STOP.equals(command)) {
            commandStr = UpDownType.UP.equals(previousCommand) ? "dim" : "bright";
        } else {
            // Move not supported yet
            commandStr = "none";
        }
    } else if (command instanceof PercentType) {
        if (deviceConfig.getItemType() == DimmerItem.class) {
            level = ((PercentType) command).intValue();
            if (((PercentType) command).intValue() == 0) {
                // If percent value equals 0 the x10 "off" command is used instead of the dim command
                commandStr = "off";
            } else {
                long dim_value = 0;
                if (deviceConfig.getDimMethod().equals("xdim")) {
                    // 100% maps to value (XDIM_LEVELS - 1) so we need to do scaling
                    dim_value = Math.round(((PercentType) command).doubleValue() * (MochadX10Command.XDIM_LEVELS - 1) / 100);
                    commandStr = "xdim " + dim_value;
                } else {
                    // 100% maps to value (DIM_LEVELS - 1) so we need to do scaling
                    Integer currentValue = currentLevel.get(address);
                    if (currentValue == null) {
                        currentValue = 0;
                    }
                    logger.debug("Address " + address + " current level " + currentValue);
                    int newValue = ((PercentType) command).intValue();
                    int relativeValue;
                    if (newValue > currentValue) {
                        relativeValue = (int) Math.round((newValue - currentValue) * ((MochadX10Command.DIM_LEVELS - 1) * 1.0 / 100));
                        commandStr = "bright " + relativeValue;
                    } else if (currentValue > newValue) {
                        relativeValue = (int) Math.round((currentValue - newValue) * ((MochadX10Command.DIM_LEVELS - 1) * 1.0 / 100));
                        commandStr = "dim " + relativeValue;
                    } else {
                        // If there is no change in state, do nothing
                        commandStr = "none";
                    }
                }
            }
        } else if (deviceConfig.getItemType() == RollershutterItem.class) {
            level = ((PercentType) command).intValue();
            Double invert_level = 100 - ((PercentType) command).doubleValue();
            long newlevel = Math.round(invert_level * 25.0 / 100);
            commandStr = "extended_code_1 0 1 " + newlevel;
        }
    } else if (command instanceof IncreaseDecreaseType) {
        // Increase decrease not yet supported
        commandStr = "none";
    }
    try {
        if (!commandStr.equals("none")) {
            out.writeBytes(tm + " " + address + " " + commandStr + "\n");
            logger.debug(tm + " " + address + " " + commandStr);
            out.flush();
            previousX10Address.setAddress(address);
            logger.debug("Previous X10 address set to " + previousX10Address.toString());
            if (level != -1) {
                currentLevel.put(address, level);
                logger.debug("Address " + address + " level set to " + level);
            }
        }
    } catch (IOException e) {
        reconnectToMochadX10Server();
        logger.error("IOException: " + e.getMessage() + " while trying to send a command to Mochad X10 host: " + hostIp + ":" + hostPort);
    }
    lastIssuedCommand.put(address, command);
}
Also used : UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) StopMoveType(org.openhab.core.library.types.StopMoveType) Command(org.openhab.core.types.Command) MochadX10Command(org.openhab.binding.mochadx10.commands.MochadX10Command) OnOffType(org.openhab.core.library.types.OnOffType) DimmerItem(org.openhab.core.library.items.DimmerItem) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType)

Example 33 with Command

use of org.openhab.core.types.Command 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 34 with Command

use of org.openhab.core.types.Command 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)

Example 35 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class ModuleChannelGroupTest method canProcessGroup2StatusUpdate.

@Test
public void canProcessGroup2StatusUpdate() {
    ModuleChannel item = group2.addChannel("test11", 11, new ArrayList<Class<? extends Command>>());
    item.setState(OnOffType.OFF);
    group2.processNikobusCommand(new NikobusCommand("$0517"), binding);
    group2.processNikobusCommand(new NikobusCommand("$1C6C940000000000FF00557CF8"), binding);
    Mockito.verify(binding, Mockito.times(1)).postUpdate("test11", OnOffType.ON);
    group2.processNikobusCommand(new NikobusCommand("$0517"), binding);
    group2.processNikobusCommand(new NikobusCommand("$1C6C9400000000FF00FF557CF8"), binding);
    Mockito.verify(binding, Mockito.times(1)).postUpdate("test11", 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

Command (org.openhab.core.types.Command)61 ArrayList (java.util.ArrayList)20 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)14 State (org.openhab.core.types.State)12 Matcher (java.util.regex.Matcher)10 DecimalType (org.openhab.core.library.types.DecimalType)8 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 NikobusCommand (org.openhab.binding.nikobus.internal.core.NikobusCommand)6 SchedulerException (org.quartz.SchedulerException)6 StringType (org.openhab.core.library.types.StringType)5 JobDataMap (org.quartz.JobDataMap)5 JobDetail (org.quartz.JobDetail)5 Scheduler (org.quartz.Scheduler)5 Trigger (org.quartz.Trigger)5 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 IOException (java.io.IOException)4 SocketChannel (java.nio.channels.SocketChannel)4 IllegalClassException (org.apache.commons.lang.IllegalClassException)4 PercentType (org.openhab.core.library.types.PercentType)4