]> git.basschouten.com Git - openhab-addons.git/blob
4ef3f082454ef525669100641488a9e2cb8b0ee4
[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.minecraft.internal;
14
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.openhab.binding.minecraft.internal.handler.MinecraftPlayerHandler;
21 import org.openhab.binding.minecraft.internal.handler.MinecraftServerHandler;
22 import org.openhab.binding.minecraft.internal.handler.MinecraftSignHandler;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link MinecraftHandlerFactory} is responsible for creating things and thing
35  * handlers.
36  *
37  * @author Mattias Markehed - Initial contribution
38  */
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.minecraft")
40 public class MinecraftHandlerFactory extends BaseThingHandlerFactory {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(MinecraftHandlerFactory.class);
43
44     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>();
45
46     static {
47         SUPPORTED_THING_TYPES_UIDS.add(MinecraftBindingConstants.THING_TYPE_SERVER);
48         SUPPORTED_THING_TYPES_UIDS.add(MinecraftBindingConstants.THING_TYPE_PLAYER);
49         SUPPORTED_THING_TYPES_UIDS.add(MinecraftBindingConstants.THING_TYPE_SIGN);
50     }
51
52     private static List<MinecraftServerHandler> minecraftServers = new ArrayList<>();
53
54     @Override
55     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
56         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
57     }
58
59     @Override
60     protected ThingHandler createHandler(Thing thing) {
61         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
62
63         if (thingTypeUID.equals(MinecraftBindingConstants.THING_TYPE_SERVER)) {
64             MinecraftServerHandler serverHandler = new MinecraftServerHandler((Bridge) thing);
65             minecraftServers.add(serverHandler);
66             return serverHandler;
67         } else if (thingTypeUID.equals(MinecraftBindingConstants.THING_TYPE_PLAYER)) {
68             return new MinecraftPlayerHandler(thing);
69         } else if (thingTypeUID.equals(MinecraftBindingConstants.THING_TYPE_SIGN)) {
70             return new MinecraftSignHandler(thing);
71         }
72
73         return null;
74     }
75
76     @Override
77     protected void removeHandler(ThingHandler thingHandler) {
78         minecraftServers.remove(thingHandler);
79         super.removeHandler(thingHandler);
80     }
81
82     /**
83      * Get all Minecraft handlers created.
84      *
85      * @return the Minecraft handlers created,
86      */
87     public static List<MinecraftServerHandler> getMinecraftServers() {
88         LOGGER.debug("getMinecraftServers {}", minecraftServers.size());
89         return new ArrayList<>(minecraftServers);
90     }
91 }