]> git.basschouten.com Git - openhab-addons.git/blob
822c675dd988252b64bbd3d4e6df9d42a55ae190
[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.remoteopenhab.internal;
14
15 import static org.openhab.binding.remoteopenhab.internal.RemoteopenhabBindingConstants.BINDING_ID;
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 import java.util.concurrent.CopyOnWriteArrayList;
23
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;
31
32 /**
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.
35  *
36  * @author Laurent Garnier - Initial contribution
37  */
38 @Component(service = { ChannelTypeProvider.class, RemoteopenhabChannelTypeProvider.class })
39 @NonNullByDefault
40 public class RemoteopenhabChannelTypeProvider implements ChannelTypeProvider {
41     private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
42     private final Map<String, List<ChannelType>> channelTypesForItemTypes = new ConcurrentHashMap<>();
43
44     @Override
45     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
46         return channelTypes;
47     }
48
49     @Override
50     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
51         for (ChannelType channelType : channelTypes) {
52             if (channelType.getUID().equals(channelTypeUID)) {
53                 return channelType;
54             }
55         }
56         return null;
57     }
58
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();
69                 }
70                 if (channelTypePattern == null) {
71                     channelTypePattern = "";
72                 }
73                 if (channelTypeReadOnly == readOnly && channelTypePattern.equals(pattern)) {
74                     return channelType;
75                 }
76             }
77         }
78         return null;
79     }
80
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));
85     }
86
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);
93         }
94     }
95
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);
101         }
102     }
103 }