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.folding.internal.discovery;
15 import java.util.HashMap;
19 import org.openhab.binding.folding.internal.FoldingBindingConstants;
20 import org.openhab.core.config.discovery.AbstractDiscoveryService;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.ThingUID;
25 import org.osgi.service.component.annotations.Component;
28 * Discovery service implementation.
30 * The Client handler has to be configured manually, but once connected,
31 * it will publish discovered slots to this service. This service converts
32 * the internal representation to discovery results.
34 * @author Marius Bjoernstad - Initial contribution
36 @Component(service = DiscoveryService.class, configurationPid = "discovery.folding")
37 public class FoldingSlotDiscoveryService extends AbstractDiscoveryService {
39 public FoldingSlotDiscoveryService() {
40 super(Set.of(FoldingBindingConstants.THING_TYPE_SLOT), 10, true);
41 FoldingDiscoveryProxy.getInstance().setService(this);
45 protected void startScan() {
48 protected String getLabel(String host, String description) {
49 if (description == null) {
52 int endOfLabel = description.indexOf(' ');
54 description = description.substring(0, endOfLabel);
56 endOfLabel = description.indexOf(':');
58 description = description.substring(0, endOfLabel);
60 return description + " @ " + host;
63 public void newSlot(ThingUID bridgeUID, String host, String id, String description) {
64 if (isBackgroundDiscoveryEnabled() && id != null) {
65 Map<String, Object> properties = new HashMap<>(1);
66 properties.put(FoldingBindingConstants.PARAM_SLOT_ID, id);
67 ThingUID thingUID = new ThingUID(FoldingBindingConstants.THING_TYPE_SLOT, bridgeUID, id);
68 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
69 .withBridge(bridgeUID).withLabel(getLabel(host, description)).build();
70 thingDiscovered(discoveryResult);