2 * Copyright (c) 2010-2023 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;
16 import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.UNCATEGORIZED;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Locale;
22 import java.util.concurrent.ConcurrentHashMap;
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;
37 * @author Daniel Demus - Initial contribution
39 @Component(service = { ChannelTypeProvider.class, WundergroundUpdateReceiverUnknownChannelTypeProvider.class })
41 public class WundergroundUpdateReceiverUnknownChannelTypeProvider implements ChannelTypeProvider {
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);
48 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
49 return channelTypes.values();
53 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
54 return channelTypes.get(channelTypeUID);
57 public ChannelType getOrCreateChannelType(String parameterName, String value) {
58 ChannelTypeUID typeUid = new ChannelTypeUID(THING_TYPE_UPDATE_RECEIVER.getBindingId(), parameterName);
60 ChannelType type = getChannelType(typeUid, 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);
70 private static String guessItemType(String value) {
71 if (BOOLEAN_STRINGS.contains(value.toLowerCase())) {
72 return CoreItemFactory.SWITCH;
76 return CoreItemFactory.NUMBER;
77 } catch (NumberFormatException ignored) {
79 return CoreItemFactory.STRING;
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);