]> git.basschouten.com Git - openhab-addons.git/blob
ad84118719e23d59cccafc90fecf1cdc47a0a16f
[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.discovery;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.jmdns.ServiceInfo;
21
22 import org.openhab.binding.minecraft.internal.MinecraftBindingConstants;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29
30 /**
31  * The {@link MinecraftMDNSDiscoveryParticipant} is responsible for discovering Minecraft servers
32  * {@link MDNSDiscoveryService}.
33  *
34  * @author Mattias Markehed - Initial contribution
35  */
36 @Component
37 public class MinecraftMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
38
39     @Override
40     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
41         return Collections.singleton(MinecraftBindingConstants.THING_TYPE_SERVER);
42     }
43
44     @Override
45     public String getServiceType() {
46         return "_http._tcp.local.";
47     }
48
49     @Override
50     public DiscoveryResult createResult(ServiceInfo service) {
51         if (service.getName().equals("wc-minecraft")) {
52             ThingUID uid = getThingUID(service);
53
54             if (uid != null) {
55                 Map<String, Object> properties = new HashMap<>();
56                 int port = service.getPort();
57                 String host = service.getInetAddresses()[0].getHostAddress();
58
59                 properties.put(MinecraftBindingConstants.PARAMETER_HOSTNAME, host);
60                 properties.put(MinecraftBindingConstants.PARAMETER_PORT, port);
61
62                 return DiscoveryResultBuilder.create(uid).withProperties(properties)
63                         .withRepresentationProperty(uid.getId()).withLabel("Minecraft Server (" + host + ")").build();
64             }
65         }
66         return null;
67     }
68
69     /**
70      * Check if service is a minecraft server.
71      *
72      * @param service the service to check.
73      * @return true if minecraft server, else false.
74      */
75     private boolean isMinecraftServer(ServiceInfo service) {
76         return (service != null && service.getType() != null && service.getType().equals(getServiceType()));
77     }
78
79     @Override
80     public ThingUID getThingUID(ServiceInfo service) {
81         if (isMinecraftServer(service) && service.getInetAddresses().length > 0) {
82             String host = service.getInetAddresses()[0].getHostAddress();
83             host = host.replace('.', '_');
84
85             return new ThingUID(MinecraftBindingConstants.THING_TYPE_SERVER, host);
86         }
87
88         return null;
89     }
90 }