]> git.basschouten.com Git - openhab-addons.git/blob
afb726aa1ce6956bcc114022f60198aa70060097
[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.homematic.internal.type;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.concurrent.CopyOnWriteArrayList;
22
23 import org.openhab.binding.homematic.type.HomematicThingTypeExcluder;
24 import org.openhab.core.thing.type.ChannelType;
25 import org.openhab.core.thing.type.ChannelTypeProvider;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29 import org.osgi.service.component.annotations.ReferenceCardinality;
30 import org.osgi.service.component.annotations.ReferencePolicy;
31
32 /**
33  * Provides all ChannelTypes from all Homematic bridges.
34  *
35  * @author Gerhard Riegler - Initial contribution
36  * @author Michael Reitler - Added HomematicThingTypeExcluder
37  */
38 @Component(service = { HomematicChannelTypeProvider.class, ChannelTypeProvider.class })
39 public class HomematicChannelTypeProviderImpl implements HomematicChannelTypeProvider {
40     private final Map<ChannelTypeUID, ChannelType> channelTypesByUID = new HashMap<>();
41     protected List<HomematicThingTypeExcluder> homematicThingTypeExcluders = new CopyOnWriteArrayList<>();
42
43     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
44     protected void addHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
45         if (homematicThingTypeExcluders != null) {
46             homematicThingTypeExcluders.add(homematicThingTypeExcluder);
47         }
48     }
49
50     protected void removeHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
51         if (homematicThingTypeExcluders != null) {
52             homematicThingTypeExcluders.remove(homematicThingTypeExcluder);
53         }
54     }
55
56     private boolean isChannelTypeExcluded(ChannelTypeUID channelTypeUID) {
57         // delegate to excluders
58         for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
59             if (excluder.isChannelTypeExcluded(channelTypeUID)) {
60                 return true;
61             }
62         }
63         return false;
64     }
65
66     @Override
67     public Collection<ChannelType> getChannelTypes(Locale locale) {
68         Collection<ChannelType> result = new ArrayList<>();
69         for (ChannelTypeUID uid : channelTypesByUID.keySet()) {
70             if (!isChannelTypeExcluded(uid)) {
71                 result.add(channelTypesByUID.get(uid));
72             }
73         }
74         return result;
75     }
76
77     @Override
78     public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
79         return isChannelTypeExcluded(channelTypeUID) ? null : channelTypesByUID.get(channelTypeUID);
80     }
81
82     @Override
83     public ChannelType getInternalChannelType(ChannelTypeUID channelTypeUID) {
84         return channelTypesByUID.get(channelTypeUID);
85     }
86
87     @Override
88     public void addChannelType(ChannelType channelType) {
89         channelTypesByUID.put(channelType.getUID(), channelType);
90     }
91 }