]> git.basschouten.com Git - openhab-addons.git/blob
a303c2063d0bbc4726620086fae12ad521bc69c9
[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.max.internal.factory;
14
15 import static org.openhab.binding.max.internal.MaxBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
20 import org.openhab.binding.max.internal.handler.MaxDevicesHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
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.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link MaxCubeHandlerFactory} is responsible for creating things and
35  * thing handlers.
36  *
37  * @author Marcel Verpaalen - Initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.max")
41 public class MaxCubeHandlerFactory extends BaseThingHandlerFactory {
42
43     private final Logger logger = LoggerFactory.getLogger(MaxCubeHandlerFactory.class);
44
45     @Override
46     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
47             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
48         if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
49             ThingUID cubeBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
50             return super.createThing(thingTypeUID, configuration, cubeBridgeUID, null);
51         }
52         if (supportsThingType(thingTypeUID) && bridgeUID != null) {
53             ThingUID deviceUID = getMaxCubeDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
54             return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
55         }
56         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
57     }
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62     }
63
64     private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
65             Configuration configuration) {
66         if (thingUID == null) {
67             String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);
68             return new ThingUID(thingTypeUID, serialNumber);
69         }
70         return thingUID;
71     }
72
73     private ThingUID getMaxCubeDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
74             Configuration configuration, ThingUID bridgeUID) {
75         if (thingUID == null) {
76             String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);
77             return new ThingUID(thingTypeUID, serialNumber, bridgeUID.getId());
78         }
79         return thingUID;
80     }
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85         if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
86             return new MaxCubeBridgeHandler((Bridge) thing);
87         } else if (SUPPORTED_DEVICE_THING_TYPES_UIDS.contains(thingTypeUID)) {
88             return new MaxDevicesHandler(thing);
89         } else {
90             logger.debug("ThingHandler not found for {}", thingTypeUID);
91             return null;
92         }
93     }
94 }