]> git.basschouten.com Git - openhab-addons.git/blob
22fa2624ad5fd293dfc240a991658f7a0bad31f0
[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.ChannelGroupType;
25 import org.openhab.core.thing.type.ChannelGroupTypeProvider;
26 import org.openhab.core.thing.type.ChannelGroupTypeUID;
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 ChannelGroupTypes from all Homematic bridges.
34  *
35  * @author Michael Reitler - Initial contribution
36  */
37 @Component(service = { HomematicChannelGroupTypeProvider.class, ChannelGroupTypeProvider.class })
38 public class HomematicChannelGroupTypeProviderImpl implements HomematicChannelGroupTypeProvider {
39     private final Map<ChannelGroupTypeUID, ChannelGroupType> channelGroupTypesByUID = new HashMap<>();
40     protected List<HomematicThingTypeExcluder> homematicThingTypeExcluders = new CopyOnWriteArrayList<>();
41
42     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
43     protected void addHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
44         if (homematicThingTypeExcluders != null) {
45             homematicThingTypeExcluders.add(homematicThingTypeExcluder);
46         }
47     }
48
49     protected void removeHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
50         if (homematicThingTypeExcluders != null) {
51             homematicThingTypeExcluders.remove(homematicThingTypeExcluder);
52         }
53     }
54
55     private boolean isChannelGroupTypeExcluded(ChannelGroupTypeUID channelGroupTypeUID) {
56         // delegate to excluders
57         for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
58             if (excluder.isChannelGroupTypeExcluded(channelGroupTypeUID)) {
59                 return true;
60             }
61         }
62         return false;
63     }
64
65     @Override
66     public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
67         return isChannelGroupTypeExcluded(channelGroupTypeUID) ? null : channelGroupTypesByUID.get(channelGroupTypeUID);
68     }
69
70     @Override
71     public ChannelGroupType getInternalChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID) {
72         return channelGroupTypesByUID.get(channelGroupTypeUID);
73     }
74
75     @Override
76     public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
77         Collection<ChannelGroupType> result = new ArrayList<>();
78         for (ChannelGroupTypeUID uid : channelGroupTypesByUID.keySet()) {
79             if (!isChannelGroupTypeExcluded(uid)) {
80                 result.add(channelGroupTypesByUID.get(uid));
81             }
82         }
83         return result;
84     }
85
86     @Override
87     public void addChannelGroupType(ChannelGroupType channelGroupType) {
88         channelGroupTypesByUID.put(channelGroupType.getUID(), channelGroupType);
89     }
90 }