]> git.basschouten.com Git - openhab-addons.git/blob
8d16fcd4ca85f879d70f912f63b7214ae9375cbc
[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.semsportal.internal.discovery;
14
15 import static org.openhab.binding.semsportal.internal.SEMSPortalBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
30
31 /**
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;
34  *
35  * @author Iwan Bron - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class StationDiscoveryService extends AbstractDiscoveryService {
40
41     private static final int DISCOVERY_TIME = 10;
42     private PortalHandler portal;
43     private ThingUID bridgeUID;
44
45     public StationDiscoveryService(PortalHandler bridgeHandler) {
46         super(Set.of(THING_TYPE_STATION), DISCOVERY_TIME);
47         this.portal = bridgeHandler;
48         this.bridgeUID = bridgeHandler.getThing().getUID();
49     }
50
51     @Override
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)
58                     .build();
59             thingDiscovered(discovery);
60         }
61         stopScan();
62     }
63
64     private ThingUID createThingUUID(Station station) {
65         return new ThingUID(THING_TYPE_STATION, station.getStationId(), bridgeUID.getId());
66     }
67
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());
76         return properties;
77     }
78 }