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.remoteopenhab.internal;
15 import static org.openhab.binding.remoteopenhab.internal.RemoteopenhabBindingConstants.BINDING_ID;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.CopyOnWriteArrayList;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.thing.type.ChannelType;
27 import org.openhab.core.thing.type.ChannelTypeProvider;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.StateDescription;
30 import org.osgi.service.component.annotations.Component;
33 * Channel type provider used for all the channel types built by the binding when building dynamically the channels.
34 * One different channel type is built for each different item type found on the remote openHAB server.
36 * @author Laurent Garnier - Initial contribution
38 @Component(service = { ChannelTypeProvider.class, RemoteopenhabChannelTypeProvider.class })
40 public class RemoteopenhabChannelTypeProvider implements ChannelTypeProvider {
41 private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
42 private final Map<String, List<ChannelType>> channelTypesForItemTypes = new ConcurrentHashMap<>();
45 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
50 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
51 for (ChannelType channelType : channelTypes) {
52 if (channelType.getUID().equals(channelTypeUID)) {
59 public @Nullable ChannelType getChannelType(String itemType, boolean readOnly, String pattern) {
60 List<ChannelType> channelTypesForItemType = channelTypesForItemTypes.get(itemType);
61 if (channelTypesForItemType != null) {
62 for (ChannelType channelType : channelTypesForItemType) {
63 boolean channelTypeReadOnly = false;
64 String channelTypePattern = null;
65 StateDescription stateDescription = channelType.getState();
66 if (stateDescription != null) {
67 channelTypeReadOnly = stateDescription.isReadOnly();
68 channelTypePattern = stateDescription.getPattern();
70 if (channelTypePattern == null) {
71 channelTypePattern = "";
73 if (channelTypeReadOnly == readOnly && channelTypePattern.equals(pattern)) {
81 public ChannelTypeUID buildNewChannelTypeUID(String itemType) {
82 List<ChannelType> channelTypesForItemType = channelTypesForItemTypes.get(itemType);
83 int nb = channelTypesForItemType == null ? 0 : channelTypesForItemType.size();
84 return new ChannelTypeUID(BINDING_ID, String.format("item%s%d", itemType.replace(":", ""), nb + 1));
87 public void addChannelType(String itemType, ChannelType channelType) {
88 channelTypes.add(channelType);
89 List<ChannelType> channelTypesForItemType = channelTypesForItemTypes.computeIfAbsent(itemType,
90 type -> new CopyOnWriteArrayList<>());
91 if (channelTypesForItemType != null) {
92 channelTypesForItemType.add(channelType);
96 public void removeChannelType(String itemType, ChannelType channelType) {
97 channelTypes.remove(channelType);
98 List<ChannelType> channelTypesForItemType = channelTypesForItemTypes.get(itemType);
99 if (channelTypesForItemType != null) {
100 channelTypesForItemType.remove(channelType);