]> git.basschouten.com Git - openhab-addons.git/blob
a92c233820de79b13aa939280fa54543e1167bdc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.folding.internal.discovery;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18
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;
26
27 /**
28  * Discovery service implementation.
29  *
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.
33  *
34  * @author Marius Bjoernstad - Initial contribution
35  */
36 @Component(service = DiscoveryService.class, configurationPid = "discovery.folding")
37 public class FoldingSlotDiscoveryService extends AbstractDiscoveryService {
38
39     public FoldingSlotDiscoveryService() {
40         super(Collections.singleton(FoldingBindingConstants.THING_TYPE_SLOT), 10, true);
41         FoldingDiscoveryProxy.getInstance().setService(this);
42     }
43
44     @Override
45     protected void startScan() {
46     }
47
48     protected String getLabel(String host, String description) {
49         if (description == null) {
50             description = "slot";
51         }
52         int endOfLabel = description.indexOf(' ');
53         if (endOfLabel > 0) {
54             description = description.substring(0, endOfLabel);
55         }
56         endOfLabel = description.indexOf(':');
57         if (endOfLabel > 0) {
58             description = description.substring(0, endOfLabel);
59         }
60         return description + " @ " + host;
61     }
62
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);
71         }
72     }
73 }