Search in sources :

Example 81 with PercentType

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

the class IhcDataConverter method convertCommandToResourceValue.

/**
     * Convert openHAB data type to IHC data type.
     *
     * @param type
     *            openHAB data type
     * @param value
     *
     * @param enumValues
     *
     * @return IHC data type
     */
public static WSResourceValue convertCommandToResourceValue(Type type, WSResourceValue value, ArrayList<IhcEnumValue> enumValues) {
    if (type instanceof DecimalType) {
        if (value instanceof WSFloatingPointValue) {
            double newVal = ((DecimalType) type).doubleValue();
            double max = ((WSFloatingPointValue) value).getMaximumValue();
            double min = ((WSFloatingPointValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSFloatingPointValue) value).setFloatingPointValue(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(((DecimalType) type).intValue() > 0 ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = ((DecimalType) type).intValue();
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else if (value instanceof WSTimerValue) {
            ((WSTimerValue) value).setMilliseconds(((DecimalType) type).longValue());
        } else if (value instanceof WSWeekdayValue) {
            ((WSWeekdayValue) value).setWeekdayNumber(((DecimalType) type).intValue());
        } else {
            throw new NumberFormatException("Can't convert DecimalType to " + value.getClass());
        }
    } else if (type instanceof OnOffType) {
        if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(type == OnOffType.ON ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = type == OnOffType.ON ? 100 : 0;
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert OnOffType to " + value.getClass());
        }
    } else if (type instanceof OpenClosedType) {
        ((WSBooleanValue) value).setValue(type == OpenClosedType.OPEN ? true : false);
    } else if (type instanceof DateTimeType) {
        if (value instanceof WSDateValue) {
            Calendar c = ((DateTimeType) type).getCalendar();
            short year = (short) c.get(Calendar.YEAR);
            byte month = (byte) (c.get(Calendar.MONTH) + 1);
            byte day = (byte) c.get(Calendar.DAY_OF_MONTH);
            ((WSDateValue) value).setYear(year);
            ((WSDateValue) value).setMonth(month);
            ((WSDateValue) value).setDay(day);
        } else if (value instanceof WSTimeValue) {
            Calendar c = ((DateTimeType) type).getCalendar();
            int hours = c.get(Calendar.HOUR_OF_DAY);
            int minutes = c.get(Calendar.MINUTE);
            int seconds = c.get(Calendar.SECOND);
            ((WSTimeValue) value).setHours(hours);
            ((WSTimeValue) value).setMinutes(minutes);
            ((WSTimeValue) value).setSeconds(seconds);
        } else {
            throw new NumberFormatException("Can't convert DateTimeItem to " + value.getClass());
        }
    } else if (type instanceof StringType) {
        if (value instanceof WSEnumValue) {
            boolean found = false;
            for (IhcEnumValue item : enumValues) {
                if (item.name.equals(type.toString())) {
                    ((WSEnumValue) value).setEnumValueID(item.id);
                    ((WSEnumValue) value).setEnumName(type.toString());
                    found = true;
                    break;
                }
            }
            if (found == false) {
                throw new NumberFormatException("Can't find enum value for string " + type.toString());
            }
        } else {
            throw new NumberFormatException("Can't convert StringType to " + value.getClass());
        }
    } else if (type instanceof PercentType) {
        if (value instanceof WSIntegerValue) {
            int newVal = ((DecimalType) type).intValue();
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert PercentType to " + value.getClass());
        }
    } else if (type instanceof UpDownType) {
        if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(type == UpDownType.DOWN ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = type == UpDownType.DOWN ? 100 : 0;
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert UpDownType to " + value.getClass());
        }
    } else {
        throw new NumberFormatException("Can't convert " + type.getClass().toString());
    }
    return value;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) DateTimeType(org.openhab.core.library.types.DateTimeType) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) WSDateValue(org.openhab.binding.ihc.ws.datatypes.WSDateValue) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Example 82 with PercentType

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

the class IhcDataConverter method convertResourceValueToState.

/**
     * Convert IHC data type to openHAB data type.
     *
     * @param itemType
     *            OpenHAB data type class
     * @param value
     *            IHC data value
     *
     * @return openHAB {@link State}
     */
public static State convertResourceValueToState(Class<? extends Item> itemType, WSResourceValue value) throws NumberFormatException {
    org.openhab.core.types.State state = UnDefType.UNDEF;
    if (itemType == NumberItem.class) {
        if (value.getClass() == WSFloatingPointValue.class) {
            // state = new
            // DecimalType(((WSFloatingPointValue)value).getFloatingPointValue());
            // Controller might send floating point value with >10 decimals
            // (22.299999237060546875), so round value to have max 2
            // decimals
            double d = ((WSFloatingPointValue) value).getFloatingPointValue();
            BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
            state = new DecimalType(bd);
        } else if (value.getClass() == WSBooleanValue.class) {
            state = new DecimalType(((WSBooleanValue) value).isValue() ? 1 : 0);
        } else if (value.getClass() == WSIntegerValue.class) {
            state = new DecimalType(((WSIntegerValue) value).getInteger());
        } else if (value.getClass() == WSTimerValue.class) {
            state = new DecimalType(((WSTimerValue) value).getMilliseconds());
        } else if (value.getClass() == WSEnumValue.class) {
            state = new DecimalType(((WSEnumValue) value).getEnumValueID());
        } else if (value.getClass() == WSWeekdayValue.class) {
            state = new DecimalType(((WSWeekdayValue) value).getWeekdayNumber());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    } else if (itemType == DimmerItem.class) {
        if (value.getClass() == WSIntegerValue.class) {
            state = new PercentType(((WSIntegerValue) value).getInteger());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    } else if (itemType == SwitchItem.class) {
        if (value.getClass() == WSBooleanValue.class) {
            if (((WSBooleanValue) value).isValue()) {
                state = OnOffType.ON;
            } else {
                state = OnOffType.OFF;
            }
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to SwitchItem");
        }
    } else if (itemType == ContactItem.class) {
        if (value.getClass() == WSBooleanValue.class) {
            if (((WSBooleanValue) value).isValue()) {
                state = OpenClosedType.OPEN;
            } else {
                state = OpenClosedType.CLOSED;
            }
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to ContactItem");
        }
    } else if (itemType == DateTimeItem.class) {
        if (value.getClass() == WSDateValue.class) {
            Calendar cal = WSDateTimeToCalendar((WSDateValue) value, null);
            state = new DateTimeType(cal);
        } else if (value.getClass() == WSTimeValue.class) {
            Calendar cal = WSDateTimeToCalendar(null, (WSTimeValue) value);
            state = new DateTimeType(cal);
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to DateTimeItem");
        }
    } else if (itemType == StringItem.class) {
        if (value.getClass() == WSEnumValue.class) {
            state = new StringType(((WSEnumValue) value).getEnumName());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to StringItem");
        }
    } else if (itemType == RollershutterItem.class) {
        if (value.getClass() == WSIntegerValue.class) {
            state = new PercentType(((WSIntegerValue) value).getInteger());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    }
    return state;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Example 83 with PercentType

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

the class HueBinding method computeCommandForItemOnBridge.

/**
     * Checks whether the command is for one of the configured Hue bulbs. If
     * this is the case, the command is translated to the corresponding action
     * which is then sent to the given bulb.
     *
     * @param command
     *            The command from the openHAB bus.
     * @param itemName
     *            The name of the targeted item.
     * @param bridge
     *            The Hue bridge the Hue bulb is connected to
     */
private void computeCommandForItemOnBridge(Command command, String itemName, HueBridge bridge) {
    HueBindingConfig deviceConfig = getConfigForItemName(itemName);
    if (deviceConfig == null) {
        return;
    }
    HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
    if (bulb == null) {
        bulb = new HueBulb(bridge, deviceConfig.getDeviceId());
        bulbCache.put(deviceConfig.getDeviceId(), bulb);
    }
    if (command instanceof OnOffType) {
        bulb.switchOn(OnOffType.ON.equals(command));
    }
    if (command instanceof HSBType) {
        HSBType hsbCommand = (HSBType) command;
        DecimalType hue = hsbCommand.getHue();
        PercentType sat = hsbCommand.getSaturation();
        PercentType bri = hsbCommand.getBrightness();
        bulb.colorizeByHSB(hue.doubleValue() / 360, sat.doubleValue() / 100, bri.doubleValue() / 100);
    }
    if (deviceConfig.getType().equals(BindingType.brightness) || deviceConfig.getType().equals(BindingType.rgb)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            int resultingValue = bulb.increaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            int resultingValue = bulb.decreaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if ((command instanceof PercentType) && !(command instanceof HSBType)) {
            bulb.setBrightness((int) Math.round((double) HueBulb.MAX_BRIGHTNESS / (double) 100 * ((PercentType) command).intValue()));
        }
    }
    if (deviceConfig.getType().equals(BindingType.colorTemperature)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            bulb.increaseColorTemperature(deviceConfig.getStepSize());
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            bulb.decreaseColorTemperature(deviceConfig.getStepSize());
        } else if (command instanceof PercentType) {
            bulb.setColorTemperature((int) Math.round((((double) 346 / (double) 100) * ((PercentType) command).intValue()) + 154));
        }
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) HueBulb(org.openhab.binding.hue.internal.hardware.HueBulb) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 84 with PercentType

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

the class LgtvBinding method convertDeviceValueToOpenHabState.

// function
/**
     * Convert receiver value to OpenHAB state.
     *
     * @param itemType
     * @param data
     *
     * @return
     */
private State convertDeviceValueToOpenHabState(Class<? extends Item> itemType, String data) {
    State state = UnDefType.UNDEF;
    try {
        int index;
        if (itemType == SwitchItem.class) {
            index = Integer.parseInt(data);
            state = index == 0 ? OnOffType.OFF : OnOffType.ON;
        } else if (itemType == NumberItem.class) {
            index = Integer.parseInt(data);
            state = new DecimalType(index);
        } else if (itemType == DimmerItem.class) {
            index = Integer.parseInt(data);
            state = new PercentType(index);
        } else if (itemType == RollershutterItem.class) {
            index = Integer.parseInt(data);
            state = new PercentType(index);
        } else if (itemType == StringItem.class) {
            // s = data.substring(3, data.length());
            state = new StringType(data);
        }
    } catch (Exception e) {
        logger.debug("Cannot convert value '{}' to data type {}", data, itemType);
    }
    // + itemType.toString() + " val=" + state.toString());
    return state;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 85 with PercentType

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

the class LightwaverfConvertorTest method testConvertToLightwaveRfMessageDimCommand.

@Test
public void testConvertToLightwaveRfMessageDimCommand() throws Exception {
    LightwaverfConvertor convertor = new LightwaverfConvertor();
    LightwaveRFCommand command = convertor.convertToLightwaveRfMessage("2", "3", LightwaveRfType.DIMMER, new PercentType(75));
    LightwaveRFCommand expected = new LightwaveRfDimCommand("200,!R2D3FdP24");
    assertEquals(expected.getLightwaveRfCommandString(), command.getLightwaveRfCommandString());
}
Also used : PercentType(org.openhab.core.library.types.PercentType) LightwaveRFCommand(org.openhab.binding.lightwaverf.internal.command.LightwaveRFCommand) LightwaveRfDimCommand(org.openhab.binding.lightwaverf.internal.command.LightwaveRfDimCommand) Test(org.junit.Test)

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