]> git.basschouten.com Git - openhab-addons.git/blob
1874962808fd10518e84a21a1b4e4eaea77709e2
[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.silvercrestwifisocket.internal;
14
15 import java.util.Set;
16
17 import org.openhab.binding.silvercrestwifisocket.internal.exceptions.MacAddressNotValidException;
18 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketHandler;
19 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketMediator;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingTypeUID;
22 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.openhab.core.thing.binding.ThingHandlerFactory;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Reference;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link SilvercrestWifiSocketHandlerFactory} is responsible for creating things and thing
32  * handlers.
33  *
34  * @author Jaime Vaz - Initial contribution
35  */
36 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.silvercrestwifisocket")
37 public class SilvercrestWifiSocketHandlerFactory extends BaseThingHandlerFactory {
38
39     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
40             .of(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET);
41
42     private final Logger logger = LoggerFactory.getLogger(SilvercrestWifiSocketHandlerFactory.class);
43
44     private SilvercrestWifiSocketMediator mediator;
45
46     /**
47      * Used by OSGI to inject the mediator in the handler factory.
48      *
49      * @param mediator the mediator
50      */
51     @Reference
52     public void setMediator(final SilvercrestWifiSocketMediator mediator) {
53         logger.debug("Mediator has been injected on handler factory service.");
54         this.mediator = mediator;
55     }
56
57     /**
58      * Used by OSGI to unsets the mediator from the handler factory.
59      *
60      * @param mediator the mediator
61      */
62     public void unsetMediator(final SilvercrestWifiSocketMediator mitsubishiMediator) {
63         logger.debug("Mediator has been unsetted from discovery service.");
64         this.mediator = null;
65     }
66
67     @Override
68     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected ThingHandler createHandler(final Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (thingTypeUID.equals(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET)) {
77             SilvercrestWifiSocketHandler handler;
78             logger.debug("Creating a new SilvercrestWifiSocketHandler...");
79             try {
80                 handler = new SilvercrestWifiSocketHandler(thing);
81                 logger.debug("SilvercrestWifiSocketMediator will register the handler.");
82                 if (this.mediator != null) {
83                     this.mediator.registerThingAndWifiSocketHandler(thing, handler);
84                 } else {
85                     logger.error(
86                             "The mediator is missing on Handler factory. Without one mediator the handler cannot work!");
87                     return null;
88                 }
89                 return handler;
90             } catch (MacAddressNotValidException e) {
91                 logger.debug("The mac address passed to WifiSocketHandler by configurations is not valid.");
92             }
93         }
94         return null;
95     }
96
97     @Override
98     public void unregisterHandler(final Thing thing) {
99         if (this.mediator != null) {
100             this.mediator.unregisterWifiSocketHandlerByThing(thing);
101         }
102         super.unregisterHandler(thing);
103     }
104 }