Search in sources :

Example 1 with EventDescription

use of org.openhab.core.types.EventDescription in project openhab-core by openhab.

the class ChannelTypeConverter method unmarshalType.

@SuppressWarnings("deprecation")
@Override
protected ChannelTypeXmlResult unmarshalType(HierarchicalStreamReader reader, UnmarshallingContext context, Map<String, String> attributes, NodeIterator nodeIterator) throws ConversionException {
    boolean advanced = readBoolean(attributes, "advanced", false);
    boolean system = readBoolean(attributes, "system", false);
    String uid = system ? XmlHelper.getSystemUID(super.getID(attributes)) : super.getUID(attributes, context);
    ChannelTypeUID channelTypeUID = new ChannelTypeUID(uid);
    String itemType = readItemType(nodeIterator);
    String kind = readKind(nodeIterator);
    String label = super.readLabel(nodeIterator);
    String description = super.readDescription(nodeIterator);
    String category = readCategory(nodeIterator);
    Set<String> tags = readTags(nodeIterator);
    StateDescription stateDescription = readStateDescription(nodeIterator);
    StateDescriptionFragment stateDescriptionFragment = stateDescription != null ? StateDescriptionFragmentBuilder.create(stateDescription).build() : null;
    CommandDescription commandDescription = readCommandDescription(nodeIterator);
    EventDescription eventDescription = readEventDescription(nodeIterator);
    AutoUpdatePolicy autoUpdatePolicy = readAutoUpdatePolicy(nodeIterator);
    Object[] configDescriptionObjects = super.getConfigDescriptionObjects(nodeIterator);
    if (kind == null) {
        // Default for kind is 'state'
        kind = "state";
    }
    ChannelKind cKind = ChannelKind.parse(kind);
    URI configDescriptionURI = (URI) configDescriptionObjects[0];
    final ChannelTypeBuilder<?> builder;
    if (cKind == ChannelKind.STATE) {
        builder = ChannelTypeBuilder.state(channelTypeUID, label, itemType).isAdvanced(advanced).withCategory(category).withConfigDescriptionURI(configDescriptionURI).withStateDescriptionFragment(stateDescriptionFragment).withAutoUpdatePolicy(autoUpdatePolicy).withCommandDescription(commandDescription);
    } else if (cKind == ChannelKind.TRIGGER) {
        builder = ChannelTypeBuilder.trigger(channelTypeUID, label).isAdvanced(advanced).withCategory(category).withConfigDescriptionURI(configDescriptionURI).withEventDescription(eventDescription);
    } else {
        throw new IllegalArgumentException(String.format("Unknown channel kind: '%s'", cKind));
    }
    if (description != null) {
        builder.withDescription(description);
    }
    if (tags != null) {
        builder.withTags(tags);
    }
    ChannelType channelType = builder.build();
    ChannelTypeXmlResult channelTypeXmlResult = new ChannelTypeXmlResult(channelType, (ConfigDescription) configDescriptionObjects[1], system);
    return channelTypeXmlResult;
}
Also used : CommandDescription(org.openhab.core.types.CommandDescription) ChannelKind(org.openhab.core.thing.type.ChannelKind) URI(java.net.URI) AutoUpdatePolicy(org.openhab.core.thing.type.AutoUpdatePolicy) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) EventDescription(org.openhab.core.types.EventDescription) ChannelType(org.openhab.core.thing.type.ChannelType) StateDescription(org.openhab.core.types.StateDescription)

Example 2 with EventDescription

use of org.openhab.core.types.EventDescription in project openhab-core by openhab.

the class EventDescriptionConverter method unmarshal.

@Override
public final Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    List<EventOption> eventOptions = null;
    NodeList nodes = (NodeList) context.convertAnother(context, NodeList.class);
    NodeIterator nodeIterator = new NodeIterator(nodes.getList());
    NodeList optionNodes = (NodeList) nodeIterator.next();
    if (optionNodes != null) {
        eventOptions = toListOfEventOptions(optionNodes);
    }
    nodeIterator.assertEndOfType();
    EventDescription eventDescription = new EventDescription(eventOptions);
    return eventDescription;
}
Also used : NodeIterator(org.openhab.core.config.xml.util.NodeIterator) NodeList(org.openhab.core.config.xml.util.NodeList) EventDescription(org.openhab.core.types.EventDescription) EventOption(org.openhab.core.types.EventOption)

Example 3 with EventDescription

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

the class HomematicTypeGeneratorImpl method createChannelType.

/**
 * Creates the ChannelType for the given datapoint.
 */
public static ChannelType createChannelType(HmDatapoint dp, ChannelTypeUID channelTypeUID) {
    ChannelType channelType;
    if (dp.getName().equals(DATAPOINT_NAME_LOWBAT) || dp.getName().equals(DATAPOINT_NAME_LOWBAT_IP)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_LOW_BATTERY;
    } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_SIGNAL_STRENGTH;
    } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_BUTTON)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_BUTTON;
    } else {
        String itemType = MetadataUtils.getItemType(dp);
        String category = MetadataUtils.getCategory(dp, itemType);
        String label = MetadataUtils.getLabel(dp);
        String description = MetadataUtils.getDatapointDescription(dp);
        List<StateOption> options = null;
        if (dp.isEnumType()) {
            options = MetadataUtils.generateOptions(dp, new OptionsBuilder<StateOption>() {

                @Override
                public StateOption createOption(String value, String description) {
                    return new StateOption(value, description);
                }
            });
        }
        StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create();
        if (dp.isNumberType()) {
            BigDecimal min = MetadataUtils.createBigDecimal(dp.getMinValue());
            BigDecimal max = MetadataUtils.createBigDecimal(dp.getMaxValue());
            if (ITEM_TYPE_DIMMER.equals(itemType) && (max.compareTo(new BigDecimal("1.0")) == 0 || max.compareTo(new BigDecimal("1.01")) == 0)) {
                // For dimmers with a max value of 1.01 or 1.0 the values must be corrected
                min = MetadataUtils.createBigDecimal(0);
                max = MetadataUtils.createBigDecimal(100);
            }
            stateFragment.withMinimum(min).withMaximum(max).withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
        } else {
            stateFragment.withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
        }
        if (options != null) {
            stateFragment.withOptions(options);
        }
        ChannelTypeBuilder channelTypeBuilder;
        EventDescription eventDescription = null;
        if (dp.isTrigger()) {
            eventDescription = new EventDescription(MetadataUtils.generateOptions(dp, new OptionsBuilder<EventOption>() {

                @Override
                public EventOption createOption(String value, String description) {
                    return new EventOption(value, description);
                }
            }));
            channelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label).withEventDescription(eventDescription);
        } else {
            channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, label, itemType).withStateDescriptionFragment(stateFragment.build());
        }
        channelType = channelTypeBuilder.isAdvanced(!MetadataUtils.isStandard(dp)).withDescription(description).withCategory(category).withConfigDescriptionURI(configDescriptionUriChannel).build();
    }
    return channelType;
}
Also used : StateDescriptionFragmentBuilder(org.openhab.core.types.StateDescriptionFragmentBuilder) ChannelType(org.openhab.core.thing.type.ChannelType) EventDescription(org.openhab.core.types.EventDescription) StateOption(org.openhab.core.types.StateOption) OptionsBuilder(org.openhab.binding.homematic.internal.type.MetadataUtils.OptionsBuilder) BigDecimal(java.math.BigDecimal) EventOption(org.openhab.core.types.EventOption) ChannelTypeBuilder(org.openhab.core.thing.type.ChannelTypeBuilder)

Example 4 with EventDescription

use of org.openhab.core.types.EventDescription in project openhab-core by openhab.

the class ChannelTypeI18nLocalizationService method createLocalizedChannelType.

public ChannelType createLocalizedChannelType(Bundle bundle, ChannelType channelType, @Nullable Locale locale) {
    ChannelTypeUID channelTypeUID = channelType.getUID();
    String defaultLabel = channelType.getLabel();
    String label = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, defaultLabel, locale);
    String description = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID, channelType.getDescription(), locale);
    final ChannelTypeBuilder<?> builder;
    switch(channelType.getKind()) {
        case STATE:
            StateDescriptionFragment stateDescriptionFragment = createLocalizedStateDescriptionFragment(bundle, channelType.getState(), channelTypeUID, locale);
            CommandDescription command = createLocalizedCommandDescription(bundle, channelType.getCommandDescription(), channelTypeUID, locale);
            String itemType = channelType.getItemType();
            if (itemType == null || itemType.isBlank()) {
                throw new IllegalArgumentException("If the kind is 'state', the item type must be set!");
            }
            builder = ChannelTypeBuilder.state(channelTypeUID, label == null ? defaultLabel : label, itemType).withStateDescriptionFragment(stateDescriptionFragment).withAutoUpdatePolicy(channelType.getAutoUpdatePolicy()).withCommandDescription(command);
            break;
        case TRIGGER:
            EventDescription eventDescription = channelType.getEvent();
            TriggerChannelTypeBuilder triggerBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label == null ? defaultLabel : label);
            if (eventDescription != null) {
                triggerBuilder.withEventDescription(eventDescription);
            }
            builder = triggerBuilder;
            break;
        default:
            throw new IllegalArgumentException("Kind must not be null or empty!");
    }
    if (description != null) {
        builder.withDescription(description);
    }
    String category = channelType.getCategory();
    if (category != null) {
        builder.withCategory(category);
    }
    URI configDescriptionURI = channelType.getConfigDescriptionURI();
    if (configDescriptionURI != null) {
        builder.withConfigDescriptionURI(configDescriptionURI);
    }
    return builder.isAdvanced(channelType.isAdvanced()).withTags(channelType.getTags()).build();
}
Also used : TriggerChannelTypeBuilder(org.openhab.core.thing.type.TriggerChannelTypeBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) CommandDescription(org.openhab.core.types.CommandDescription) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) EventDescription(org.openhab.core.types.EventDescription) URI(java.net.URI)

Aggregations

EventDescription (org.openhab.core.types.EventDescription)4 URI (java.net.URI)2 ChannelType (org.openhab.core.thing.type.ChannelType)2 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)2 CommandDescription (org.openhab.core.types.CommandDescription)2 EventOption (org.openhab.core.types.EventOption)2 StateDescriptionFragment (org.openhab.core.types.StateDescriptionFragment)2 BigDecimal (java.math.BigDecimal)1 OptionsBuilder (org.openhab.binding.homematic.internal.type.MetadataUtils.OptionsBuilder)1 NodeIterator (org.openhab.core.config.xml.util.NodeIterator)1 NodeList (org.openhab.core.config.xml.util.NodeList)1 AutoUpdatePolicy (org.openhab.core.thing.type.AutoUpdatePolicy)1 ChannelKind (org.openhab.core.thing.type.ChannelKind)1 ChannelTypeBuilder (org.openhab.core.thing.type.ChannelTypeBuilder)1 TriggerChannelTypeBuilder (org.openhab.core.thing.type.TriggerChannelTypeBuilder)1 StateDescription (org.openhab.core.types.StateDescription)1 StateDescriptionFragmentBuilder (org.openhab.core.types.StateDescriptionFragmentBuilder)1 StateOption (org.openhab.core.types.StateOption)1