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