]> git.basschouten.com Git - openhab-addons.git/blob
fd2283389a3ea57289844f5d4231e290aa2eb53d
[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.phc.internal;
14
15 import static org.openhab.binding.phc.internal.PHCBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.phc.internal.handler.PHCBridgeHandler;
25 import org.openhab.binding.phc.internal.handler.PHCHandler;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.io.transport.serial.SerialPortManager;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link PHCHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Jonas Hohaus - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.phc")
46 public class PHCHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
49             .unmodifiableSet(Stream.of(THING_TYPE_BRIDGE, THING_TYPE_AM, THING_TYPE_EM, THING_TYPE_JRM, THING_TYPE_DIM)
50                     .collect(Collectors.toSet()));
51
52     private @NonNullByDefault({}) SerialPortManager serialPortManager;
53
54     @Reference
55     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
56         this.serialPortManager = serialPortManager;
57     }
58
59     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
60         this.serialPortManager = null;
61     }
62
63     @Override
64     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66     }
67
68     @Override
69     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
70             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
71         Thing thing;
72
73         if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
74             if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
75                 thing = super.createThing(thingTypeUID, configuration, thingUID, null);
76             } else {
77                 ThingUID phcThingUID = new ThingUID(thingTypeUID, configuration.get(ADDRESS).toString());
78                 thing = super.createThing(thingTypeUID, configuration, phcThingUID, bridgeUID);
79             }
80         } else {
81             throw new IllegalArgumentException(
82                     "The thing type " + thingTypeUID + " is not supported by the phc binding.");
83         }
84
85         return thing;
86     }
87
88     @Override
89     protected @Nullable ThingHandler createHandler(Thing thing) {
90         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91
92         ThingHandler handler = null;
93
94         if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
95             handler = new PHCBridgeHandler((Bridge) thing, serialPortManager);
96         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
97             handler = new PHCHandler(thing);
98         }
99
100         return handler;
101     }
102 }