2 * Copyright (c) 2010-2023 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.ChannelType;
25 import org.openhab.core.thing.type.ChannelTypeBuilder;
26 import org.openhab.core.thing.type.ChannelTypeProvider;
27 import org.openhab.core.thing.type.ChannelTypeUID;
28 import org.openhab.core.thing.type.StateChannelTypeBuilder;
29 import org.openhab.core.types.StateDescriptionFragmentBuilder;
30 import org.osgi.service.component.annotations.Component;
33 * The {@link Tr064ChannelTypeProvider} is used for providing dynamic channel types
35 * @author Jan N. Klug - Initial contribution
38 @Component(service = { ChannelTypeProvider.class, Tr064ChannelTypeProvider.class })
39 public class Tr064ChannelTypeProvider implements ChannelTypeProvider {
40 private final Map<ChannelTypeUID, ChannelType> channelTypeMap = new ConcurrentHashMap<>();
42 public Tr064ChannelTypeProvider() {
43 CHANNEL_TYPES.forEach(channelTypeDescription -> {
44 ChannelTypeUID channelTypeUID = new ChannelTypeUID(Tr064BindingConstants.BINDING_ID,
45 channelTypeDescription.getName());
46 // create state description
47 StateDescriptionFragmentBuilder stateDescriptionFragmentBuilder = StateDescriptionFragmentBuilder.create()
48 .withReadOnly(channelTypeDescription.getSetAction() == null);
49 if (channelTypeDescription.getItem().getStatePattern() != null) {
50 stateDescriptionFragmentBuilder.withPattern(channelTypeDescription.getItem().getStatePattern());
53 // create channel type
54 ChannelTypeBuilder<StateChannelTypeBuilder> channelTypeBuilder = ChannelTypeBuilder
55 .state(channelTypeUID, channelTypeDescription.getLabel(),
56 channelTypeDescription.getItem().getType())
57 .withStateDescriptionFragment(stateDescriptionFragmentBuilder.build())
58 .isAdvanced(channelTypeDescription.isAdvanced());
59 if (channelTypeDescription.getDescription() != null) {
60 channelTypeBuilder.withDescription(channelTypeDescription.getDescription());
63 channelTypeMap.put(channelTypeUID, channelTypeBuilder.build());
68 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
69 return channelTypeMap.values();
73 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
74 return channelTypeMap.get(channelTypeUID);