]> git.basschouten.com Git - openhab-addons.git/blob
45672253a095532d867d372e82703b4243d0a82d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
31
32 /**
33  * The {@link Tr064ChannelTypeProvider} is used for providing dynamic channel types
34  *
35  * @author Jan N. Klug - Initial contribution
36  */
37 @NonNullByDefault
38 @Component(service = { ChannelTypeProvider.class, Tr064ChannelTypeProvider.class })
39 public class Tr064ChannelTypeProvider implements ChannelTypeProvider {
40     private final Map<ChannelTypeUID, ChannelType> channelTypeMap = new ConcurrentHashMap<>();
41
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());
51             }
52
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());
61             }
62
63             channelTypeMap.put(channelTypeUID, channelTypeBuilder.build());
64         });
65     }
66
67     @Override
68     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
69         return channelTypeMap.values();
70     }
71
72     @Override
73     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
74         return channelTypeMap.get(channelTypeUID);
75     }
76 }