]> git.basschouten.com Git - openhab-addons.git/blob
3ba0dad5dc818e8b165ce6705ef4721a46237be2
[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.wundergroundupdatereceiver.internal;
14
15 import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER;
16 import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.UNCATEGORIZED;
17
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.library.CoreItemFactory;
27 import org.openhab.core.thing.type.AutoUpdatePolicy;
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.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Daniel Demus - Initial contribution
38  */
39 @Component(service = { ChannelTypeProvider.class, WundergroundUpdateReceiverUnknownChannelTypeProvider.class })
40 @NonNullByDefault
41 public class WundergroundUpdateReceiverUnknownChannelTypeProvider implements ChannelTypeProvider {
42
43     private static final List<String> BOOLEAN_STRINGS = List.of("1", "0", "true", "false", "yes", "no", "on", "off");
44     private final Map<ChannelTypeUID, ChannelType> channelTypes = new ConcurrentHashMap<>();
45     private final Logger logger = LoggerFactory.getLogger(WundergroundUpdateReceiverUnknownChannelTypeProvider.class);
46
47     @Override
48     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
49         return channelTypes.values();
50     }
51
52     @Override
53     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
54         return channelTypes.get(channelTypeUID);
55     }
56
57     public ChannelType getOrCreateChannelType(String parameterName, String value) {
58         ChannelTypeUID typeUid = new ChannelTypeUID(THING_TYPE_UPDATE_RECEIVER.getBindingId(), parameterName);
59         @Nullable
60         ChannelType type = getChannelType(typeUid, null);
61         if (type == null) {
62             String itemType = guessItemType(value);
63             type = ChannelTypeBuilder.state(typeUid, parameterName + " channel type", itemType).isAdvanced(true)
64                     .withCategory(UNCATEGORIZED).withAutoUpdatePolicy(AutoUpdatePolicy.DEFAULT).build();
65             return addChannelType(typeUid, type);
66         }
67         return type;
68     }
69
70     private static String guessItemType(String value) {
71         if (BOOLEAN_STRINGS.contains(value.toLowerCase())) {
72             return CoreItemFactory.SWITCH;
73         }
74         try {
75             Float.valueOf(value);
76             return CoreItemFactory.NUMBER;
77         } catch (NumberFormatException ignored) {
78         }
79         return CoreItemFactory.STRING;
80     }
81
82     private ChannelType addChannelType(ChannelTypeUID channelTypeUID, ChannelType channelType) {
83         logger.warn("Adding new synthetic channelType {} for unrecognised parameter", channelTypeUID.getAsString());
84         this.channelTypes.put(channelTypeUID, channelType);
85         return channelType;
86     }
87 }