]> git.basschouten.com Git - openhab-addons.git/blob
cd57ccab1d8e8f6cde35519a8786982b98cbc317
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.remoteopenhab.internal;
14
15 import static org.openhab.binding.remoteopenhab.internal.RemoteopenhabBindingConstants.*;
16
17 import javax.ws.rs.client.ClientBuilder;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabBridgeHandler;
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.binding.BaseThingHandlerFactory;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerFactory;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
32
33 import com.google.gson.FieldNamingPolicy;
34 import com.google.gson.Gson;
35 import com.google.gson.GsonBuilder;
36
37 /**
38  * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Laurent Garnier - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
45 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
46
47     private final ClientBuilder clientBuilder;
48     private final SseEventSourceFactory eventSourceFactory;
49     private final RemoteopenhabChannelTypeProvider channelTypeProvider;
50     private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
51     private final Gson jsonParser;
52
53     @Activate
54     public RemoteopenhabHandlerFactory(final @Reference ClientBuilder clientBuilder,
55             final @Reference SseEventSourceFactory eventSourceFactory,
56             final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
57             final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider) {
58         this.clientBuilder = clientBuilder;
59         this.eventSourceFactory = eventSourceFactory;
60         this.channelTypeProvider = channelTypeProvider;
61         this.stateDescriptionProvider = stateDescriptionProvider;
62         jsonParser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
63     }
64
65     /**
66      * The things this factory supports creating.
67      */
68     @Override
69     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
70         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
71     }
72
73     /**
74      * Creates a handler for the specific thing.
75      */
76     @Override
77     protected @Nullable ThingHandler createHandler(Thing thing) {
78         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79
80         return BRIDGE_TYPE_SERVER.equals(thingTypeUID)
81                 ? new RemoteopenhabBridgeHandler((Bridge) thing, clientBuilder, eventSourceFactory, channelTypeProvider,
82                         stateDescriptionProvider, jsonParser)
83                 : null;
84     }
85 }