2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wundergroundupdatereceiver.internal;
15 import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
21 import java.util.concurrent.ConcurrentHashMap;
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;
34 * @author Daniel Demus - Initial contribution
36 @Component(service = { ChannelTypeProvider.class, WundergroundUpdateReceiverUnknownChannelTypeProvider.class })
38 public class WundergroundUpdateReceiverUnknownChannelTypeProvider implements ChannelTypeProvider {
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);
45 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
46 return channelTypes.values();
50 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
51 return channelTypes.get(channelTypeUID);
54 public ChannelType getOrCreateChannelType(String parameterName, String value) {
55 ChannelTypeUID typeUid = new ChannelTypeUID(THING_TYPE_UPDATE_RECEIVER.getBindingId(), parameterName);
57 ChannelType type = getChannelType(typeUid, null);
59 String itemType = guessItemType(value);
60 type = ChannelTypeBuilder.state(typeUid, parameterName + " channel type", itemType).build();
61 return addChannelType(typeUid, type);
66 private static String guessItemType(String value) {
67 if (BOOLEAN_STRINGS.contains(value.toLowerCase())) {
73 } catch (NumberFormatException ignored) {
78 private ChannelType addChannelType(ChannelTypeUID channelTypeUID, ChannelType channelType) {
79 logger.warn("Adding channelType {} for unknown parameter", channelTypeUID.getAsString());
80 this.channelTypes.put(channelTypeUID, channelType);