]> git.basschouten.com Git - openhab-addons.git/blob
23e3ff639e84ecdd77f98c530a8b9c180579dee4
[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.network.internal;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.network.internal.handler.NetworkHandler;
20 import org.openhab.binding.network.internal.handler.SpeedTestHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.thing.binding.ThingHandlerFactory;
27 import org.osgi.service.component.ComponentContext;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Deactivate;
31 import org.osgi.service.component.annotations.Modified;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The handler factory retrieves the binding configuration and is responsible for creating
37  * PING_DEVICE and SERVICE_DEVICE handlers.
38  *
39  * @author David Graeff - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.network")
43 public class NetworkHandlerFactory extends BaseThingHandlerFactory {
44     final NetworkBindingConfiguration configuration = new NetworkBindingConfiguration();
45
46     private final Logger logger = LoggerFactory.getLogger(NetworkHandlerFactory.class);
47
48     @Override
49     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
50         return NetworkBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
51     }
52
53     // The activate component call is used to access the bindings configuration
54     @Activate
55     protected void activate(ComponentContext componentContext, Map<String, Object> config) {
56         super.activate(componentContext);
57         modified(config);
58     }
59
60     @Override
61     @Deactivate
62     protected void deactivate(ComponentContext componentContext) {
63         super.deactivate(componentContext);
64     }
65
66     @Modified
67     protected void modified(Map<String, Object> config) {
68         // We update instead of replace the configuration object, so that if the user updates the
69         // configuration, the values are automatically available in all handlers. Because they all
70         // share the same instance.
71         configuration.update(new Configuration(config).as(NetworkBindingConfiguration.class));
72         logger.debug("Updated binding configuration to {}", configuration);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         if (thingTypeUID.equals(NetworkBindingConstants.PING_DEVICE)
80                 || thingTypeUID.equals(NetworkBindingConstants.BACKWARDS_COMPATIBLE_DEVICE)) {
81             return new NetworkHandler(thing, false, configuration);
82         } else if (thingTypeUID.equals(NetworkBindingConstants.SERVICE_DEVICE)) {
83             return new NetworkHandler(thing, true, configuration);
84         } else if (thingTypeUID.equals(NetworkBindingConstants.SPEEDTEST_DEVICE)) {
85             return new SpeedTestHandler(thing);
86         }
87         return null;
88     }
89 }