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.semsportal.internal.discovery;
15 import static org.openhab.binding.semsportal.internal.SEMSPortalBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.semsportal.internal.PortalHandler;
24 import org.openhab.binding.semsportal.internal.dto.Station;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingUID;
32 * The discovery service can discover the power stations that are registered to the portal that it belongs to. It will
33 * find unique power stations and add them as a discovery result;
35 * @author Iwan Bron - Initial contribution
39 public class StationDiscoveryService extends AbstractDiscoveryService {
41 private static final int DISCOVERY_TIME = 10;
42 private PortalHandler portal;
43 private ThingUID bridgeUID;
45 public StationDiscoveryService(PortalHandler bridgeHandler) {
46 super(Set.of(THING_TYPE_STATION), DISCOVERY_TIME);
47 this.portal = bridgeHandler;
48 this.bridgeUID = bridgeHandler.getThing().getUID();
52 protected void startScan() {
53 for (Station station : portal.getAllStations()) {
54 DiscoveryResult discovery = DiscoveryResultBuilder.create(createThingUUID(station)).withBridge(bridgeUID)
55 .withProperties(buildProperties(station))
56 .withRepresentationProperty(STATION_REPRESENTATION_PROPERTY)
57 .withLabel(String.format(STATION_LABEL_FORMAT, station.getName())).withThingType(THING_TYPE_STATION)
59 thingDiscovered(discovery);
64 private ThingUID createThingUUID(Station station) {
65 return new ThingUID(THING_TYPE_STATION, station.getStationId(), bridgeUID.getId());
68 private @Nullable Map<String, Object> buildProperties(Station station) {
69 Map<String, Object> properties = new HashMap<>();
70 properties.put(Thing.PROPERTY_MODEL_ID, station.getType());
71 properties.put(Thing.PROPERTY_SERIAL_NUMBER, station.getSerialNumber());
72 properties.put(STATION_NAME, station.getName());
73 properties.put(STATION_CAPACITY, station.getCapacity());
74 properties.put(STATION_UUID, station.getStationId());
75 properties.put(STATION_CAPACITY, station.getCapacity());