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