]> git.basschouten.com Git - openhab-addons.git/blob
f9a974ad1ad5c7ea5dc829e439e75f8ee812f2e4
[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.powermax.internal;
14
15 import static org.openhab.binding.powermax.internal.PowermaxBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.powermax.internal.handler.PowermaxBridgeHandler;
20 import org.openhab.binding.powermax.internal.handler.PowermaxThingHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.i18n.TimeZoneProvider;
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.ThingUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
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
35 /**
36  * The {@link PowermaxHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Laurent Garnier - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.powermax")
43 public class PowermaxHandlerFactory extends BaseThingHandlerFactory {
44
45     private final SerialPortManager serialPortManager;
46     private final TimeZoneProvider timeZoneProvider;
47
48     @Activate
49     public PowermaxHandlerFactory(final @Reference SerialPortManager serialPortManager,
50             final @Reference TimeZoneProvider timeZoneProvider) {
51         this.serialPortManager = serialPortManager;
52         this.timeZoneProvider = timeZoneProvider;
53     }
54
55     @Override
56     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID);
58     }
59
60     @Override
61     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
62             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
63         if (SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID)) {
64             return super.createThing(thingTypeUID, configuration, thingUID, null);
65         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
66             ThingUID newThingUID;
67             if (bridgeUID != null && thingUID != null) {
68                 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
69             } else {
70                 newThingUID = thingUID;
71             }
72             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
73         }
74         throw new IllegalArgumentException(
75                 "The thing type " + thingTypeUID + " is not supported by the Powermax binding.");
76     }
77
78     @Override
79     protected @Nullable ThingHandler createHandler(Thing thing) {
80         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81
82         if (SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID)) {
83             return new PowermaxBridgeHandler((Bridge) thing, serialPortManager, timeZoneProvider);
84         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
85             return new PowermaxThingHandler(thing);
86         }
87
88         return null;
89     }
90 }