]> git.basschouten.com Git - openhab-addons.git/blob
9bbc62d32f41912a38027d93c020bb5d6343185c
[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.upb.internal;
14
15 import java.math.BigDecimal;
16 import java.util.Dictionary;
17
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;
36
37 /**
38  * Factory for UPB handlers.
39  *
40  * @author Marcus Better - Initial contribution
41  */
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.upb")
43 @NonNullByDefault
44 public class UPBHandlerFactory extends BaseThingHandlerFactory {
45     private final Logger logger = LoggerFactory.getLogger(UPBHandlerFactory.class);
46     private final SerialPortManager serialPortManager;
47
48     private @Nullable Byte networkId;
49
50     @Activate
51     public UPBHandlerFactory(@Reference SerialPortManager serialPortManager) {
52         this.serialPortManager = serialPortManager;
53     }
54
55     @Override
56     @NonNullByDefault({})
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);
61         if (nid != null) {
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");
65             }
66             networkId = nid.byteValue();
67         }
68     }
69
70     @Override
71     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
72         return Constants.BINDING_ID.equals(thingTypeUID.getBindingId());
73     }
74
75     @Override
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);
88         }
89         return null;
90     }
91 }