2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.minecraft.internal.discovery;
15 import java.util.HashMap;
19 import javax.jmdns.ServiceInfo;
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;
30 * The {@link MinecraftMDNSDiscoveryParticipant} is responsible for discovering Minecraft servers
31 * {@link MDNSDiscoveryService}.
33 * @author Mattias Markehed - Initial contribution
36 public class MinecraftMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
39 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
40 return Set.of(MinecraftBindingConstants.THING_TYPE_SERVER);
44 public String getServiceType() {
45 return "_http._tcp.local.";
49 public DiscoveryResult createResult(ServiceInfo service) {
50 if ("wc-minecraft".equals(service.getName())) {
51 ThingUID uid = getThingUID(service);
54 Map<String, Object> properties = new HashMap<>();
55 int port = service.getPort();
56 String host = service.getInetAddresses()[0].getHostAddress();
58 properties.put(MinecraftBindingConstants.PARAMETER_HOSTNAME, host);
59 properties.put(MinecraftBindingConstants.PARAMETER_PORT, port);
61 return DiscoveryResultBuilder.create(uid).withProperties(properties)
62 .withRepresentationProperty(uid.getId()).withLabel("Minecraft Server (" + host + ")").build();
69 * Check if service is a minecraft server.
71 * @param service the service to check.
72 * @return true if minecraft server, else false.
74 private boolean isMinecraftServer(ServiceInfo service) {
75 return (service != null && service.getType() != null && service.getType().equals(getServiceType()));
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('.', '_');
84 return new ThingUID(MinecraftBindingConstants.THING_TYPE_SERVER, host);