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.silvercrestwifisocket.internal;
15 import java.util.Collections;
18 import org.openhab.binding.silvercrestwifisocket.internal.exceptions.MacAddressNotValidException;
19 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketHandler;
20 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketMediator;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingTypeUID;
23 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.openhab.core.thing.binding.ThingHandlerFactory;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link SilvercrestWifiSocketHandlerFactory} is responsible for creating things and thing
35 * @author Jaime Vaz - Initial contribution
37 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.silvercrestwifisocket")
38 public class SilvercrestWifiSocketHandlerFactory extends BaseThingHandlerFactory {
40 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
41 .singleton(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET);
43 private final Logger logger = LoggerFactory.getLogger(SilvercrestWifiSocketHandlerFactory.class);
45 private SilvercrestWifiSocketMediator mediator;
48 * Used by OSGI to inject the mediator in the handler factory.
50 * @param mediator the mediator
53 public void setMediator(final SilvercrestWifiSocketMediator mediator) {
54 logger.debug("Mediator has been injected on handler factory service.");
55 this.mediator = mediator;
59 * Used by OSGI to unsets the mediator from the handler factory.
61 * @param mediator the mediator
63 public void unsetMediator(final SilvercrestWifiSocketMediator mitsubishiMediator) {
64 logger.debug("Mediator has been unsetted from discovery service.");
69 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
70 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74 protected ThingHandler createHandler(final Thing thing) {
75 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
77 if (thingTypeUID.equals(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET)) {
78 SilvercrestWifiSocketHandler handler;
79 logger.debug("Creating a new SilvercrestWifiSocketHandler...");
81 handler = new SilvercrestWifiSocketHandler(thing);
82 logger.debug("SilvercrestWifiSocketMediator will register the handler.");
83 if (this.mediator != null) {
84 this.mediator.registerThingAndWifiSocketHandler(thing, handler);
87 "The mediator is missing on Handler factory. Without one mediator the handler cannot work!");
91 } catch (MacAddressNotValidException e) {
92 logger.debug("The mac address passed to WifiSocketHandler by configurations is not valid.");
99 public void unregisterHandler(final Thing thing) {
100 if (this.mediator != null) {
101 this.mediator.unregisterWifiSocketHandlerByThing(thing);
103 super.unregisterHandler(thing);