]> git.basschouten.com Git - openhab-addons.git/blob
c3d071d7e16182290e666b2abce13764656ee93d
[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.ThingTypeUID;
25 import org.openhab.core.thing.binding.ThingTypeProvider;
26 import org.openhab.core.thing.type.ThingType;
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 ThingTypes from all Homematic bridges. Getters will exclude
34  * ThingTypes which occur in any registered {@link HomematicThingTypeExcluder},
35  * which allows external injection of customized thing-types at runtime.
36  *
37  * @author Gerhard Riegler - Initial contribution
38  * @author Michael Reitler - Added HomematicThingTypeExcluder
39  */
40 @Component(service = { HomematicThingTypeProvider.class, ThingTypeProvider.class })
41 public class HomematicThingTypeProviderImpl implements HomematicThingTypeProvider {
42     private Map<ThingTypeUID, ThingType> thingTypesByUID = new HashMap<>();
43     protected List<HomematicThingTypeExcluder> homematicThingTypeExcluders = new CopyOnWriteArrayList<>();
44
45     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
46     protected void addHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
47         if (homematicThingTypeExcluders != null) {
48             homematicThingTypeExcluders.add(homematicThingTypeExcluder);
49         }
50     }
51
52     protected void removeHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
53         if (homematicThingTypeExcluders != null) {
54             homematicThingTypeExcluders.remove(homematicThingTypeExcluder);
55         }
56     }
57
58     private Collection<ThingTypeUID> getExcludedThingTypes() {
59         Collection<ThingTypeUID> thingTypes = new ArrayList<>();
60         for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
61             thingTypes.addAll(excluder.getExcludedThingTypes());
62         }
63         return thingTypes;
64     }
65
66     private boolean isThingTypeExcluded(ThingTypeUID thingType) {
67         // delegate to excluders
68         for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
69             if (excluder.isThingTypeExcluded(thingType)) {
70                 return true;
71             }
72         }
73         return false;
74     }
75
76     @Override
77     public Collection<ThingType> getThingTypes(Locale locale) {
78         Map<ThingTypeUID, ThingType> copy = new HashMap<>(thingTypesByUID);
79         copy.keySet().removeAll(getExcludedThingTypes());
80         return copy.values();
81     }
82
83     @Override
84     public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
85         return isThingTypeExcluded(thingTypeUID) ? null : thingTypesByUID.get(thingTypeUID);
86     }
87
88     @Override
89     public ThingType getInternalThingType(ThingTypeUID thingTypeUID) {
90         return thingTypesByUID.get(thingTypeUID);
91     }
92
93     @Override
94     public void addThingType(ThingType thingType) {
95         thingTypesByUID.put(thingType.getUID(), thingType);
96     }
97 }