]> git.basschouten.com Git - openhab-addons.git/blob
22156c7d5bccb3acc76c183a674a02d58230f931
[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.milight.internal;
14
15 import static org.openhab.binding.milight.internal.MilightBindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.milight.internal.handler.BridgeV3Handler;
26 import org.openhab.binding.milight.internal.handler.BridgeV6Handler;
27 import org.openhab.binding.milight.internal.handler.MilightV2RGBHandler;
28 import org.openhab.binding.milight.internal.handler.MilightV3RGBWHandler;
29 import org.openhab.binding.milight.internal.handler.MilightV3WhiteHandler;
30 import org.openhab.binding.milight.internal.handler.MilightV6RGBCWWWHandler;
31 import org.openhab.binding.milight.internal.handler.MilightV6RGBIBOXHandler;
32 import org.openhab.binding.milight.internal.handler.MilightV6RGBWHandler;
33 import org.openhab.binding.milight.internal.protocol.QueuedSend;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.ComponentContext;
41 import org.osgi.service.component.annotations.Component;
42
43 /**
44  * The {@link MilightHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author David Graeff - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.milight")
51 public class MilightHandlerFactory extends BaseThingHandlerFactory {
52     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
53             Stream.of(BRIDGEV3_THING_TYPE, BRIDGEV6_THING_TYPE, RGB_V2_THING_TYPE, RGB_THING_TYPE, WHITE_THING_TYPE,
54                     RGB_W_THING_TYPE, RGB_CW_WW_THING_TYPE, RGB_IBOX_THING_TYPE).collect(Collectors.toSet()));
55
56     // The UDP queue for bridge communication is a single instance for all bridges.
57     // This is because all bridges use the same radio frequency, and if multiple
58     // bridges would send a command at the same time, they would interfere with
59     // each other (user report!).
60     private @Nullable QueuedSend queuedSend;
61
62     private int bridgeOffset;
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
70     protected @Nullable ThingHandler createHandler(Thing thing) {
71         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72         final QueuedSend queuedSend = this.queuedSend;
73         if (queuedSend == null) {
74             return null;
75         }
76
77         if (thingTypeUID.equals(BRIDGEV3_THING_TYPE)) {
78             return new BridgeV3Handler((Bridge) thing, bridgeOffset += 100);
79         } else if (thingTypeUID.equals(BRIDGEV6_THING_TYPE)) {
80             return new BridgeV6Handler((Bridge) thing, bridgeOffset += 100);
81         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.RGB_IBOX_THING_TYPE)) {
82             return new MilightV6RGBIBOXHandler(thing, queuedSend);
83         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.RGB_CW_WW_THING_TYPE)) {
84             return new MilightV6RGBCWWWHandler(thing, queuedSend);
85         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.RGB_W_THING_TYPE)) {
86             return new MilightV6RGBWHandler(thing, queuedSend);
87         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.RGB_V2_THING_TYPE)) {
88             return new MilightV2RGBHandler(thing, queuedSend);
89         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.WHITE_THING_TYPE)) {
90             return new MilightV3WhiteHandler(thing, queuedSend);
91         } else if (thing.getThingTypeUID().equals(MilightBindingConstants.RGB_THING_TYPE)) {
92             return new MilightV3RGBWHandler(thing, queuedSend);
93         }
94
95         return null;
96     }
97
98     @Override
99     protected void activate(ComponentContext componentContext) {
100         super.activate(componentContext);
101         queuedSend = new QueuedSend();
102         queuedSend.start();
103         bridgeOffset = 0;
104     }
105
106     @Override
107     protected void deactivate(ComponentContext componentContext) {
108         QueuedSend queuedSend = this.queuedSend;
109         if (queuedSend != null) {
110             try {
111                 queuedSend.close();
112             } catch (IOException ignore) {
113             }
114         }
115         super.deactivate(componentContext);
116     }
117 }