]> git.basschouten.com Git - openhab-addons.git/blob
4c0dd77d66311ce7272ded6b5ce8e3510e6396b0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.miio.internal.basic;
14
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_ID;
16
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.thing.type.ChannelType;
29 import org.openhab.core.thing.type.ChannelTypeBuilder;
30 import org.openhab.core.thing.type.ChannelTypeProvider;
31 import org.openhab.core.thing.type.ChannelTypeUID;
32 import org.openhab.core.thing.type.StateChannelTypeBuilder;
33 import org.openhab.core.types.StateDescriptionFragmentBuilder;
34 import org.openhab.core.types.StateOption;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Provide channelTypes for Mi IO Basic devices
41  *
42  * @author Marcel Verpaalen - Initial contribution
43  */
44 @Component(service = { ChannelTypeProvider.class, BasicChannelTypeProvider.class })
45 @NonNullByDefault
46 public class BasicChannelTypeProvider implements ChannelTypeProvider {
47     private final Map<String, ChannelType> channelTypes = new ConcurrentHashMap<>();
48     private final Logger logger = LoggerFactory.getLogger(BasicChannelTypeProvider.class);
49
50     @Override
51     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
52         return channelTypes.values();
53     }
54
55     @Override
56     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
57         if (channelTypes.containsKey(channelTypeUID.getAsString())) {
58             return channelTypes.get(channelTypeUID.getAsString());
59         }
60         return null;
61     }
62
63     public void addChannelType(MiIoBasicChannel miChannel, String model) {
64         ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID,
65                 model.toUpperCase().replace(".", "_") + "_" + miChannel.getChannel());
66         logger.debug("Adding channel definitions for {} -> {}", channelTypeUID, miChannel.getFriendlyName());
67         try {
68             final StateDescriptionDTO stateDescription = miChannel.getStateDescription();
69             StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID,
70                     miChannel.getFriendlyName(), miChannel.getType()); //
71             if (stateDescription != null) {
72                 StateDescriptionFragmentBuilder sdf = StateDescriptionFragmentBuilder.create();
73                 final BigDecimal maximum = stateDescription.getMaximum();
74                 if (maximum != null) {
75                     sdf.withMaximum(maximum);
76                 }
77                 final BigDecimal minimum = stateDescription.getMinimum();
78                 if (minimum != null) {
79                     sdf.withMinimum(minimum);
80                 }
81                 final BigDecimal step = stateDescription.getStep();
82                 if (step != null) {
83                     sdf.withStep(step);
84                 }
85                 final String pattern = stateDescription.getPattern();
86                 if (pattern != null) {
87                     sdf.withPattern(pattern);
88                 }
89                 final Boolean readOnly = stateDescription.getReadOnly();
90                 if (readOnly != null) {
91                     sdf.withReadOnly(readOnly);
92                 }
93                 List<OptionsValueListDTO> optionList = stateDescription.getOptions();
94                 if (optionList != null) {
95                     List<StateOption> options = new ArrayList<>();
96                     for (OptionsValueListDTO option : optionList) {
97                         String value = option.getValue();
98                         if (value != null) {
99                             options.add(new StateOption(value, option.getLabel()));
100                         }
101                     }
102                     sdf.withOptions(options);
103                 }
104                 channelTypeBuilder.withStateDescriptionFragment(sdf.build());
105                 logger.debug("added stateDescription: {}", sdf);
106             }
107             final String category = miChannel.getCategory();
108             if (category != null) {
109                 channelTypeBuilder.withCategory(category);
110             }
111             final Set<String> tags = miChannel.getTags();
112             if (tags != null && !tags.isEmpty()) {
113                 channelTypeBuilder.withTags(tags);
114             }
115             channelTypes.put(channelTypeUID.getAsString(), channelTypeBuilder.build());
116         } catch (Exception e) {
117             logger.warn("Failed creating channelType {}: {} ", channelTypeUID, e.getMessage());
118         }
119     }
120 }