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.pioneeravr.internal.handler;
15 import java.util.Collections;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.pioneeravr.internal.PioneerAvrBindingConstants;
23 import org.openhab.core.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.ComponentContext;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link AvrHandlerFactory} is responsible for creating things and thing handlers.
37 * @author Antoine Besnard - Initial contribution
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.pioneeravr")
41 public class AvrHandlerFactory extends BaseThingHandlerFactory {
43 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
44 Stream.of(PioneerAvrBindingConstants.IP_AVR_THING_TYPE, PioneerAvrBindingConstants.IP_AVR_THING_TYPE2014,
45 PioneerAvrBindingConstants.IP_AVR_THING_TYPE2015, PioneerAvrBindingConstants.IP_AVR_THING_TYPE2016,
46 PioneerAvrBindingConstants.IP_AVR_THING_TYPE2017, PioneerAvrBindingConstants.IP_AVR_THING_TYPE2018,
47 PioneerAvrBindingConstants.IP_AVR_THING_TYPE2019, PioneerAvrBindingConstants.IP_AVR_THING_TYPE2020,
48 PioneerAvrBindingConstants.IP_AVR_UNSUPPORTED_THING_TYPE,
49 PioneerAvrBindingConstants.SERIAL_AVR_THING_TYPE).collect(Collectors.toSet()));
51 private SerialPortManager serialPortManager;
54 public AvrHandlerFactory(ComponentContext componentContext, final @Reference SerialPortManager serialPortManager) {
55 super.activate(componentContext);
56 this.serialPortManager = serialPortManager;
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65 protected @Nullable ThingHandler createHandler(Thing thing) {
66 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68 if (thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE)
69 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2014)
70 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2015)
71 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2016)
72 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2017)
73 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2018)
74 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2019)
75 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_THING_TYPE2020)
76 || thingTypeUID.equals(PioneerAvrBindingConstants.IP_AVR_UNSUPPORTED_THING_TYPE)) {
77 return new IpAvrHandler(thing);
78 } else if (thingTypeUID.equals(PioneerAvrBindingConstants.SERIAL_AVR_THING_TYPE)) {
79 return new SerialAvrHandler(thing, serialPortManager);