Search in sources :

Example 61 with PercentType

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

the class MochadX10BrightCommand method postCommand.

@Override
public void postCommand(String itemName, int currentLevel) {
    if (value < 0) {
        value = 1;
    }
    level = Math.min(100, currentLevel + value * 100 / (MochadX10Command.DIM_LEVELS - 1));
    eventPublisher.postCommand(itemName, new PercentType(level));
}
Also used : PercentType(org.openhab.core.library.types.PercentType)

Example 62 with PercentType

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

the class MochadX10DimCommand method postCommand.

@Override
public void postCommand(String itemName, int currentLevel) {
    if (value < 0) {
        value = 1;
    }
    level = Math.max(0, currentLevel - value * 100 / (MochadX10Command.DIM_LEVELS - 1));
    eventPublisher.postCommand(itemName, new PercentType(level));
}
Also used : PercentType(org.openhab.core.library.types.PercentType)

Example 63 with PercentType

use of org.openhab.core.library.types.PercentType 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 64 with PercentType

use of org.openhab.core.library.types.PercentType 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 65 with PercentType

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

the class ModuleChannelGroup method processNikobusCommand.

/**
     * {@inheritDoc}
     * 
     * The channel group can only process status update commands. These commands
     * are sent by the module in response to a status request and contain
     * the ON/OFF/Dimming status of the different channels.
     */
@Override
public void processNikobusCommand(NikobusCommand cmd, NikobusBinding binding) {
    String command = cmd.getCommand();
    // check if it was an ACK for a status request
    if (command.startsWith(STATUS_REQUEST_ACK)) {
        if (command.startsWith(STATUS_REQUEST_ACK + statusRequestGroup)) {
            nextStatusResponseIsForThisGroup = Boolean.TRUE;
        } else {
            nextStatusResponseIsForThisGroup = Boolean.FALSE;
        }
        return;
    }
    if (!command.startsWith(STATUS_RESPONSE)) {
        return;
    }
    if (!command.startsWith(STATUS_RESPONSE + address) || nextStatusResponseIsForThisGroup == null || nextStatusResponseIsForThisGroup.equals(Boolean.FALSE)) {
        nextStatusResponseIsForThisGroup = null;
        return;
    }
    log.debug("Processing nikobus command {} for module ({}-{})", new Object[] { cmd.getCommand(), address, Integer.toString(group) });
    lastUpdatedTime = System.currentTimeMillis();
    // for every channel, update the status if it was changed
    for (int i = 0; i < 6; i++) {
        if (channels[i] == null) {
            continue;
        }
        State currentState = channels[i].getState();
        String newValue = command.substring(9 + (i * 2), 11 + (i * 2));
        if (channels[i].supportsPercentType()) {
            PercentType value = getPercentTypeFromByteString(newValue);
            if (!currentState.equals(value)) {
                binding.postUpdate(channels[i].getName(), value);
                channels[i].setState(value);
            }
        } else {
            if (newValue.equals(LOW_BYTE)) {
                if (channels[i].getState().equals(OnOffType.ON)) {
                    binding.postUpdate(channels[i].getName(), OnOffType.OFF);
                    channels[i].setState(OnOffType.OFF);
                }
            } else {
                if (channels[i].getState().equals(OnOffType.OFF)) {
                    binding.postUpdate(channels[i].getName(), OnOffType.ON);
                    channels[i].setState(OnOffType.ON);
                }
            }
        }
    }
}
Also used : State(org.openhab.core.types.State) PercentType(org.openhab.core.library.types.PercentType)

Aggregations

PercentType (org.openhab.core.library.types.PercentType)105 DecimalType (org.openhab.core.library.types.DecimalType)43 State (org.openhab.core.types.State)29 StringType (org.openhab.core.library.types.StringType)27 DimmerItem (org.openhab.core.library.items.DimmerItem)22 Test (org.junit.Test)21 HSBType (org.openhab.core.library.types.HSBType)19 OnOffType (org.openhab.core.library.types.OnOffType)19 RollershutterItem (org.openhab.core.library.items.RollershutterItem)17 NumberItem (org.openhab.core.library.items.NumberItem)16 BigDecimal (java.math.BigDecimal)14 SwitchItem (org.openhab.core.library.items.SwitchItem)13 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)13 ColorItem (org.openhab.core.library.items.ColorItem)11 DateTimeType (org.openhab.core.library.types.DateTimeType)11 Calendar (java.util.Calendar)9 ContactItem (org.openhab.core.library.items.ContactItem)9 UpDownType (org.openhab.core.library.types.UpDownType)9 StopMoveType (org.openhab.core.library.types.StopMoveType)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6