Search in sources :

Example 36 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class DeviceHandler method channelLinked.

@Override
public void channelLinked(ChannelUID channelUID) {
    if (device != null) {
        SensorEnum sensorType = getSensorEnum(channelUID.getId());
        if (sensorType != null) {
            Float val = device.getFloatSensorValue(sensorType);
            if (val != null) {
                updateState(channelUID, new DecimalType(val));
            }
        }
        Short val = device.getBinaryInputState(getBinaryInput(channelUID.getId()));
        if (val != null) {
            if (val == 1) {
                updateState(channelUID, OnOffType.ON);
            } else {
                updateState(channelUID, OnOffType.OFF);
            }
        }
        if (channelUID.getId().contains(DsChannelTypeProvider.DIMMER)) {
            if (device.isOn()) {
                updateState(channelUID, new PercentType(fromValueToPercent(device.getOutputValue(), device.getMaxOutputValue())));
            } else {
                updateState(channelUID, new PercentType(0));
            }
            return;
        }
        if (channelUID.getId().contains(DsChannelTypeProvider.SWITCH)) {
            if (device.isOn()) {
                updateState(channelUID, OnOffType.ON);
            } else {
                updateState(channelUID, OnOffType.OFF);
            }
            return;
        }
        if (channelUID.getId().contains(DsChannelTypeProvider.SHADE)) {
            updateState(channelUID, new PercentType(fromValueToPercent(device.getSlatPosition(), device.getMaxSlatPosition())));
            return;
        }
        if (channelUID.getId().contains(DsChannelTypeProvider.ANGLE)) {
            updateState(channelUID, new PercentType(fromValueToPercent(device.getAnglePosition(), device.getMaxSlatAngle())));
            return;
        }
        if (channelUID.getId().contains(DsChannelTypeProvider.STAGE)) {
            if (channelUID.getId().contains(TWO_STAGE_SWITCH_IDENTICATOR)) {
                updateState(channelUID, new StringType(convertStageValue((short) 2, device.getOutputValue())));
                return;
            }
            if (channelUID.getId().contains(THREE_STAGE_SWITCH_IDENTICATOR)) {
                updateState(channelUID, new StringType(convertStageValue((short) 3, device.getOutputValue())));
                return;
            }
        }
    }
}
Also used : SensorEnum(org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum) StringType(org.eclipse.smarthome.core.library.types.StringType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Example 37 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class ZoneTemperatureControlHandler method configChanged.

@Override
public synchronized void configChanged(TemperatureControlStatus tempControlStatus) {
    if (tempControlStatus != null && tempControlStatus.getIsConfigured()) {
        ControlModes controlMode = ControlModes.getControlMode(tempControlStatus.getControlMode());
        ControlStates controlState = ControlStates.getControlState(tempControlStatus.getControlState());
        if (controlMode != null && controlState != null) {
            logger.debug("config changed: {}", tempControlStatus.toString());
            if (controlMode.equals(ControlModes.OFF) && currentChannelID != null) {
                currentChannelID = null;
                loadChannel();
            } else if (controlMode.equals(ControlModes.PID_CONTROL) && (currentChannelID == null || !currentChannelID.contains(DsChannelTypeProvider.TEMPERATURE_CONTROLLED)) && !controlState.equals(ControlStates.EMERGENCY)) {
                currentChannelID = DsChannelTypeProvider.getOutputChannelTypeID(FunctionalColorGroupEnum.BLUE, OutputModeEnum.TEMPRETURE_PWM);
                loadChannel();
                currentValue = tempControlStatus.getNominalValue();
                updateState(currentChannelID, new DecimalType(currentValue.doubleValue()));
            } else if (!controlMode.equals(ControlModes.PID_CONTROL) && !controlMode.equals(ControlModes.OFF)) {
                currentChannelID = DsChannelTypeProvider.getOutputChannelTypeID(FunctionalColorGroupEnum.BLUE, OutputModeEnum.HEATING_PWM);
                loadChannel();
                currentValue = tempControlStatus.getControlValue();
                updateState(currentChannelID, new PercentType(fixPercent(currentValue.intValue())));
                if (controlState.equals(ControlStates.EMERGENCY)) {
                    updateStatus(ThingStatus.ONLINE, ThingStatusDetail.COMMUNICATION_ERROR, "The communication with temperation sensor fails. Temperature control state emergency (temperature control though the control value) is active.");
                }
            }
            // TODO: in case of control-mode zone-follower it is maybe useful to add the followed zone-id, but this
            // info is not in the control-status
            Map<String, String> properties = editProperties();
            properties.put("controlDSUID", tempControlStatus.getControlDSUID());
            properties.put("controlMode", controlMode.getKey());
            properties.put("controlState", controlState.getKey());
            updateProperties(properties);
        }
    } else {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "digitalSTROM temperature control is for this zone not configured in.");
    }
}
Also used : ControlStates(org.eclipse.smarthome.binding.digitalstrom.internal.lib.climate.constants.ControlStates) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) ControlModes(org.eclipse.smarthome.binding.digitalstrom.internal.lib.climate.constants.ControlModes)

Example 38 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class ZoneTemperatureControlHandler method sendCommandAndUpdateChannel.

private void sendCommandAndUpdateChannel(Float newValue) {
    if (isTemperature()) {
        if (temperatureSensorTransmitter.pushTargetTemperature(zoneID, newValue)) {
            currentValue = newValue;
            updateState(currentChannelID, new DecimalType(newValue));
        }
    } else {
        if (temperatureSensorTransmitter.pushControlValue(zoneID, newValue)) {
            currentValue = newValue;
            updateState(currentChannelID, new PercentType(newValue.intValue()));
        }
    }
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Example 39 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class ItemUIRegistryImpl method convertState.

/**
 * Converts an item state to the type the widget supports (if possible)
 *
 * @param w Widget in sitemap that shows the state
 * @param i item
 * @return the converted state or the original if conversion was not possible
 */
private State convertState(Widget w, Item i) {
    State returnState = null;
    State itemState = i.getState();
    if (itemState instanceof QuantityType) {
        itemState = convertStateToWidgetUnit((QuantityType<?>) itemState, w);
    }
    if (w instanceof Switch && i instanceof RollershutterItem) {
        // RollerShutter are represented as Switch in a Sitemap but need a PercentType state
        returnState = itemState.as(PercentType.class);
    } else if (w instanceof Slider) {
        if (i.getAcceptedDataTypes().contains(PercentType.class)) {
            returnState = itemState.as(PercentType.class);
        } else {
            returnState = itemState.as(DecimalType.class);
        }
    } else if (w instanceof Switch) {
        Switch sw = (Switch) w;
        if (sw.getMappings().size() == 0) {
            returnState = itemState.as(OnOffType.class);
        }
    }
    // if returnState is null, a conversion was not possible
    if (returnState == null) {
        // we return the original state to not break anything
        returnState = itemState;
    }
    return returnState;
}
Also used : Switch(org.eclipse.smarthome.model.sitemap.Switch) Slider(org.eclipse.smarthome.model.sitemap.Slider) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) State(org.eclipse.smarthome.core.types.State) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Example 40 with PercentType

use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.

the class ItemUIRegistryImpl method getLabel.

@Override
public String getLabel(Widget w) {
    String label = getLabelFromWidget(w);
    String itemName = w.getItem();
    if (StringUtils.isBlank(itemName)) {
        return transform(label, null);
    }
    String labelMappedOption = null;
    State state = null;
    StateDescription stateDescription = null;
    String formatPattern = getFormatPattern(label);
    // (i.e. it contains at least a %)
    try {
        final Item item = getItem(itemName);
        // There is a known issue in the implementation of the method getStateDescription() of class Item
        // in the following case:
        // - the item provider returns as expected a state description without pattern but with for
        // example a min value because a min value is set in the item definition but no label with
        // pattern is set.
        // - the channel state description provider returns as expected a state description with a pattern
        // In this case, the result is no display of value by UIs because no pattern is set in the
        // returned StateDescription. What is expected is the display of a value using the pattern
        // provided by the channel state description provider.
        stateDescription = item.getStateDescription();
        if (formatPattern == null && stateDescription != null && stateDescription.getPattern() != null) {
            label = label + " [" + stateDescription.getPattern() + "]";
        }
        String updatedPattern = getFormatPattern(label);
        if (updatedPattern != null) {
            formatPattern = updatedPattern;
            // a number is requested, PercentType must not be converted to DecimalType:
            if (formatPattern.contains("%d") && !(item.getState() instanceof PercentType)) {
                state = item.getStateAs(DecimalType.class);
            } else {
                state = item.getState();
            }
        }
    } catch (ItemNotFoundException e) {
        logger.error("Cannot retrieve item for widget {}", w.eClass().getInstanceTypeName());
    }
    if (formatPattern != null) {
        if (formatPattern.isEmpty()) {
            label = label.substring(0, label.indexOf("[")).trim();
        } else {
            if (state == null || state instanceof UnDefType) {
                formatPattern = formatUndefined(formatPattern);
            } else if (state instanceof Type) {
                // if the channel contains options, we build a label with the mapped option value
                if (stateDescription != null && stateDescription.getOptions() != null) {
                    for (StateOption option : stateDescription.getOptions()) {
                        if (option.getValue().equals(state.toString()) && option.getLabel() != null) {
                            State stateOption = new StringType(option.getLabel());
                            try {
                                String formatPatternOption = stateOption.format(formatPattern);
                                labelMappedOption = label.trim();
                                labelMappedOption = labelMappedOption.substring(0, labelMappedOption.indexOf("[") + 1) + formatPatternOption + "]";
                            } catch (IllegalArgumentException e) {
                                logger.debug("Mapping option value '{}' for item {} using format '{}' failed ({}); mapping is ignored", stateOption, itemName, formatPattern, e.getMessage());
                                labelMappedOption = null;
                            }
                            break;
                        }
                    }
                }
                if (state instanceof QuantityType) {
                    QuantityType<?> quantityState = (QuantityType<?>) state;
                    // sanity convert current state to the item state description unit in case it was updated in the
                    // meantime. The item state is still in the "original" unit while the state description will
                    // display the new unit:
                    Unit<?> patternUnit = UnitUtils.parseUnit(formatPattern);
                    if (patternUnit != null && !quantityState.getUnit().equals(patternUnit)) {
                        quantityState = quantityState.toUnit(patternUnit);
                    }
                    // The widget may define its own unit in the widget label. Convert to this unit:
                    quantityState = convertStateToWidgetUnit(quantityState, w);
                    state = quantityState;
                }
                // This also handles IllegalFormatConversionException, which is a subclass of IllegalArgument.
                try {
                    formatPattern = fillFormatPattern(formatPattern, state);
                } catch (IllegalArgumentException e) {
                    logger.warn("Exception while formatting value '{}' of item {} with format '{}': {}", state, itemName, formatPattern, e.getMessage());
                    formatPattern = new String("Err");
                }
            }
            label = label.trim();
            label = label.substring(0, label.indexOf("[") + 1) + formatPattern + "]";
        }
    }
    return transform(label, labelMappedOption);
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Unit(javax.measure.Unit) ChronoUnit(java.time.temporal.ChronoUnit) StateOption(org.eclipse.smarthome.core.types.StateOption) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) PlayPauseType(org.eclipse.smarthome.core.library.types.PlayPauseType) StringType(org.eclipse.smarthome.core.library.types.StringType) NextPreviousType(org.eclipse.smarthome.core.library.types.NextPreviousType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Type(org.eclipse.smarthome.core.types.Type) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) StateDescription(org.eclipse.smarthome.core.types.StateDescription) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

PercentType (org.eclipse.smarthome.core.library.types.PercentType)63 Test (org.junit.Test)25 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)17 State (org.eclipse.smarthome.core.types.State)17 HSBType (org.eclipse.smarthome.core.library.types.HSBType)16 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)13 LifxMessageUtil.increaseDecreasePercentType (org.eclipse.smarthome.binding.lifx.internal.util.LifxMessageUtil.increaseDecreasePercentType)9 StringType (org.eclipse.smarthome.core.library.types.StringType)9 IncreaseDecreaseType (org.eclipse.smarthome.core.library.types.IncreaseDecreaseType)7 RefreshType (org.eclipse.smarthome.core.types.RefreshType)7 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)4 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)4 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)4 BigDecimal (java.math.BigDecimal)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 FadeAction (org.eclipse.smarthome.binding.dmx.internal.action.FadeAction)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)3 LinkedList (java.util.LinkedList)2