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.upb.internal;
15 import java.math.BigDecimal;
16 import java.util.Dictionary;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.upb.internal.handler.SerialPIMHandler;
21 import org.openhab.binding.upb.internal.handler.UPBThingHandler;
22 import org.openhab.binding.upb.internal.handler.VirtualThingHandler;
23 import org.openhab.core.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.osgi.service.component.ComponentContext;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Factory for UPB handlers.
40 * @author Marcus Better - Initial contribution
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.upb")
44 public class UPBHandlerFactory extends BaseThingHandlerFactory {
45 private final Logger logger = LoggerFactory.getLogger(UPBHandlerFactory.class);
46 private final SerialPortManager serialPortManager;
48 private @Nullable Byte networkId;
51 public UPBHandlerFactory(@Reference SerialPortManager serialPortManager) {
52 this.serialPortManager = serialPortManager;
57 protected void activate(final ComponentContext componentContext) {
58 super.activate(componentContext);
59 final Dictionary<String, Object> config = componentContext.getProperties();
60 final BigDecimal nid = (BigDecimal) config.get(Constants.CONFIGURATION_NETWORK_ID);
62 if (nid.compareTo(BigDecimal.ZERO) < 0 || nid.compareTo(BigDecimal.valueOf(255)) > 0) {
63 logger.warn("invalid network ID {}", nid);
64 throw new IllegalArgumentException("network ID out of range");
66 networkId = nid.byteValue();
71 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
72 return Constants.BINDING_ID.equals(thingTypeUID.getBindingId());
76 protected @Nullable ThingHandler createHandler(final Thing thing) {
77 logger.debug("Creating thing {}", thing.getUID());
78 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (thingTypeUID.equals(Constants.PIM_UID)) {
80 assert serialPortManager != null;
81 return new SerialPIMHandler((Bridge) thing, serialPortManager);
82 } else if (thingTypeUID.equals(Constants.VIRTUAL_DEVICE_UID)) {
83 return new VirtualThingHandler(thing, networkId);
84 } else if (thingTypeUID.equals(Constants.GENERIC_DEVICE_UID)
85 || thingTypeUID.equals(Constants.LEVITON_38A00_DEVICE_UID)) {
86 // generic UPB thing handler
87 return new UPBThingHandler(thing, networkId);