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;
}
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;
}
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;
}
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();
}
Aggregations