2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tr064.internal;
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.CHANNEL_TYPES;
17 import java.util.Collection;
18 import java.util.Locale;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.thing.type.*;
25 import org.openhab.core.types.StateDescriptionFragmentBuilder;
26 import org.osgi.service.component.annotations.Component;
29 * The {@link Tr064ChannelTypeProvider} is used for providing dynamic channel types
31 * @author Jan N. Klug - Initial contribution
34 @Component(service = { ChannelTypeProvider.class, Tr064ChannelTypeProvider.class })
35 public class Tr064ChannelTypeProvider implements ChannelTypeProvider {
36 private final Map<ChannelTypeUID, ChannelType> channelTypeMap = new ConcurrentHashMap<>();
38 public Tr064ChannelTypeProvider() {
39 CHANNEL_TYPES.forEach(channelTypeDescription -> {
40 ChannelTypeUID channelTypeUID = new ChannelTypeUID(Tr064BindingConstants.BINDING_ID,
41 channelTypeDescription.getName());
42 // create state description
43 StateDescriptionFragmentBuilder stateDescriptionFragmentBuilder = StateDescriptionFragmentBuilder.create()
44 .withReadOnly(channelTypeDescription.getSetAction() == null);
45 if (channelTypeDescription.getItem().getStatePattern() != null) {
46 stateDescriptionFragmentBuilder.withPattern(channelTypeDescription.getItem().getStatePattern());
49 // create channel type
50 ChannelTypeBuilder<StateChannelTypeBuilder> channelTypeBuilder = ChannelTypeBuilder
51 .state(channelTypeUID, channelTypeDescription.getLabel(),
52 channelTypeDescription.getItem().getType())
53 .withStateDescriptionFragment(stateDescriptionFragmentBuilder.build())
54 .isAdvanced(channelTypeDescription.isAdvanced());
55 if (channelTypeDescription.getDescription() != null) {
56 channelTypeBuilder.withDescription(channelTypeDescription.getDescription());
59 channelTypeMap.put(channelTypeUID, channelTypeBuilder.build());
64 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
65 return channelTypeMap.values();
69 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
70 return channelTypeMap.get(channelTypeUID);