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