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.millheat.internal.discovery;
15 import java.util.Collections;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.openhab.binding.millheat.internal.MillheatBindingConstants;
23 import org.openhab.binding.millheat.internal.handler.MillheatAccountHandler;
24 import org.openhab.binding.millheat.internal.model.Heater;
25 import org.openhab.binding.millheat.internal.model.Home;
26 import org.openhab.binding.millheat.internal.model.MillheatModel;
27 import org.openhab.binding.millheat.internal.model.Room;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class does discovery of discoverable things
39 * @author Arne Seime - Initial contribution
41 public class MillheatDiscoveryService extends AbstractDiscoveryService {
42 private static final long REFRESH_INTERVAL_MINUTES = 60;
43 public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections.unmodifiableSet(
44 Stream.of(MillheatBindingConstants.THING_TYPE_HEATER, MillheatBindingConstants.THING_TYPE_ROOM,
45 MillheatBindingConstants.THING_TYPE_HOME).collect(Collectors.toSet()));
46 private final Logger logger = LoggerFactory.getLogger(MillheatDiscoveryService.class);
47 private ScheduledFuture<?> discoveryJob;
48 private final MillheatAccountHandler accountHandler;
50 public MillheatDiscoveryService(final MillheatAccountHandler accountHandler) {
51 super(DISCOVERABLE_THING_TYPES_UIDS, 10);
52 this.accountHandler = accountHandler;
56 protected void startBackgroundDiscovery() {
57 discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
61 protected synchronized void startScan() {
63 final ThingUID accountUID = accountHandler.getThing().getUID();
64 logger.debug("Start scan for Millheat devices on account {}", accountUID.toString());
65 accountHandler.updateModelFromServerWithRetry(false);
66 final MillheatModel model = accountHandler.getModel();
67 for (final Home home : model.getHomes()) {
68 final ThingUID homeUID = new ThingUID(MillheatBindingConstants.THING_TYPE_HOME, accountUID,
69 String.valueOf(home.getId()));
70 final DiscoveryResult discoveryResultHome = DiscoveryResultBuilder.create(homeUID)
71 .withBridge(accountUID).withLabel(home.getName()).withProperty("homeId", home.getId())
72 .withRepresentationProperty("homeId").build();
73 thingDiscovered(discoveryResultHome);
75 for (final Room room : home.getRooms()) {
76 final ThingUID roomUID = new ThingUID(MillheatBindingConstants.THING_TYPE_ROOM, accountUID,
77 String.valueOf(room.getId()));
78 final DiscoveryResult discoveryResultRoom = DiscoveryResultBuilder.create(roomUID)
79 .withBridge(accountUID).withLabel(room.getName()).withProperty("roomId", room.getId())
80 .withRepresentationProperty("roomId").build();
81 thingDiscovered(discoveryResultRoom);
82 for (final Heater heater : room.getHeaters()) {
83 final ThingUID heaterUID = new ThingUID(MillheatBindingConstants.THING_TYPE_HEATER, accountUID,
84 String.valueOf(heater.getId()));
85 final DiscoveryResult discoveryResultHeater = DiscoveryResultBuilder.create(heaterUID)
86 .withBridge(accountUID).withLabel(heater.getName())
87 .withProperty("heaterId", heater.getId()).withRepresentationProperty("macAddress")
88 .withProperty("macAddress", heater.getMacAddress()).build();
89 thingDiscovered(discoveryResultHeater);
92 for (final Heater heater : home.getIndependentHeaters()) {
93 final ThingUID heaterUID = new ThingUID(MillheatBindingConstants.THING_TYPE_HEATER, accountUID,
94 String.valueOf(heater.getId()));
95 final DiscoveryResult discoveryResultHeater = DiscoveryResultBuilder.create(heaterUID)
96 .withBridge(accountUID).withLabel(heater.getName()).withRepresentationProperty("heaterId")
97 .withProperty("heaterId", heater.getId()).build();
98 thingDiscovered(discoveryResultHeater);
102 removeOlderResults(getTimestampOfLastScan(), null, accountHandler.getThing().getUID());
107 protected void stopBackgroundDiscovery() {
109 if (discoveryJob != null && !discoveryJob.isCancelled()) {
110 discoveryJob.cancel(true);