]> git.basschouten.com Git - openhab-addons.git/blob
190c0e02a8477802297a400de67891a86cb8cbce
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tr064.internal;
14
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.CHANNEL_TYPES;
16
17 import java.util.Collection;
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
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;
27
28 /**
29  * The {@link Tr064ChannelTypeProvider} is used for providing dynamic channel types
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @NonNullByDefault
34 @Component(service = { ChannelTypeProvider.class, Tr064ChannelTypeProvider.class })
35 public class Tr064ChannelTypeProvider implements ChannelTypeProvider {
36     private final Map<ChannelTypeUID, ChannelType> channelTypeMap = new ConcurrentHashMap<>();
37
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());
47             }
48
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());
57             }
58
59             channelTypeMap.put(channelTypeUID, channelTypeBuilder.build());
60         });
61     }
62
63     @Override
64     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
65         return channelTypeMap.values();
66     }
67
68     @Override
69     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
70         return channelTypeMap.get(channelTypeUID);
71     }
72 }