2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.phc.internal;
15 import static org.openhab.binding.phc.internal.PHCBindingConstants.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
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;
39 * The {@link PHCHandlerFactory} is responsible for creating things and thing
42 * @author Jonas Hohaus - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.phc")
46 public class PHCHandlerFactory extends BaseThingHandlerFactory {
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()));
52 private @NonNullByDefault({}) SerialPortManager serialPortManager;
55 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
56 this.serialPortManager = serialPortManager;
59 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
60 this.serialPortManager = null;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
70 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
73 if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
74 if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
75 thing = super.createThing(thingTypeUID, configuration, thingUID, null);
77 ThingUID phcThingUID = new ThingUID(thingTypeUID, configuration.get(ADDRESS).toString());
78 thing = super.createThing(thingTypeUID, configuration, phcThingUID, bridgeUID);
81 throw new IllegalArgumentException(
82 "The thing type " + thingTypeUID + " is not supported by the phc binding.");
89 protected @Nullable ThingHandler createHandler(Thing thing) {
90 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92 ThingHandler handler = null;
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);