]> git.basschouten.com Git - openhab-addons.git/blob
0df704092aedfb5fe7ac43fe81377de11bab8736
[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.plugwiseha.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHAApplianceHandler;
19 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHABridgeHandler;
20 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHAZoneHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.io.net.http.HttpClientFactory;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
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.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * The {@link PlugwiseHAHandlerFactory} is responsible for creating things and
36  * thing handlers.
37  *
38  * @author Bas van Wetten - Initial contribution
39  * @author Leo Siepel - finish initial contribution
40  * 
41  */
42 @NonNullByDefault
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.plugwiseha")
44 public class PlugwiseHAHandlerFactory extends BaseThingHandlerFactory {
45
46     private final HttpClient httpClient;
47
48     // Constructor
49
50     @Activate
51     public PlugwiseHAHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
52         this.httpClient = httpClientFactory.getCommonHttpClient();
53     }
54
55     // Public methods
56
57     /**
58      * Returns whether the handler is able to create a thing or register a thing
59      * handler for the given type.
60      *
61      * @param thingTypeUID the thing type UID
62      * @return true, if the handler supports the thing type, false otherwise
63      */
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)
67                 || PlugwiseHAZoneHandler.supportsThingType(thingTypeUID))
68                 || PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID);
69     }
70
71     /**
72      * Creates a thing for given arguments.
73      *
74      * @param thingTypeUID thing type uid (not null)
75      * @param configuration configuration
76      * @param thingUID thing uid, which can be null
77      * @param bridgeUID bridge uid, which can be null
78      * @return created thing
79      */
80     @Override
81     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
82             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
83         if (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)) {
84             return super.createThing(thingTypeUID, configuration, thingUID, null);
85         } else if (PlugwiseHAZoneHandler.supportsThingType(thingTypeUID)) {
86             return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
87         } else if (PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID)) {
88             return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
89         }
90
91         throw new IllegalArgumentException(
92                 "The thing type " + thingTypeUID + " is not supported by the plugwiseha binding.");
93     }
94
95     // Protected and private methods
96
97     /**
98      * Creates a {@link ThingHandler} for the given thing.
99      *
100      * @param thing the thing
101      * @return thing the created handler
102      */
103     @Override
104     protected @Nullable ThingHandler createHandler(Thing thing) {
105         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
106
107         if (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)) {
108             return new PlugwiseHABridgeHandler((Bridge) thing, this.httpClient);
109         } else if (PlugwiseHAZoneHandler.supportsThingType(thingTypeUID)) {
110             return new PlugwiseHAZoneHandler(thing);
111         } else if (PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID)) {
112             return new PlugwiseHAApplianceHandler(thing);
113         }
114         return null;
115     }
116 }