2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.network.internal;
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;
36 * The handler factory retrieves the binding configuration and is responsible for creating
37 * PING_DEVICE and SERVICE_DEVICE handlers.
39 * @author David Graeff - Initial contribution
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.network")
43 public class NetworkHandlerFactory extends BaseThingHandlerFactory {
44 final NetworkBindingConfiguration configuration = new NetworkBindingConfiguration();
46 private final Logger logger = LoggerFactory.getLogger(NetworkHandlerFactory.class);
49 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
50 return NetworkBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
53 // The activate component call is used to access the bindings configuration
55 protected void activate(ComponentContext componentContext, Map<String, Object> config) {
56 super.activate(componentContext);
62 protected void deactivate(ComponentContext componentContext) {
63 super.deactivate(componentContext);
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);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);