]> git.basschouten.com Git - openhab-addons.git/blob
4227bc53e5fb0ec2a8214688f04d2afb0d8e266c
[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.millheat.internal.discovery;
14
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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;
35
36 /**
37  * This class does discovery of discoverable things
38  *
39  * @author Arne Seime - Initial contribution
40  */
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;
49
50     public MillheatDiscoveryService(final MillheatAccountHandler accountHandler) {
51         super(DISCOVERABLE_THING_TYPES_UIDS, 10);
52         this.accountHandler = accountHandler;
53     }
54
55     @Override
56     protected void startBackgroundDiscovery() {
57         discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
58     }
59
60     @Override
61     protected synchronized void startScan() {
62         try {
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);
74
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);
90                     }
91                 }
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);
99                 }
100             }
101         } finally {
102             removeOlderResults(getTimestampOfLastScan(), null, accountHandler.getThing().getUID());
103         }
104     }
105
106     @Override
107     protected void stopBackgroundDiscovery() {
108         stopScan();
109         if (discoveryJob != null && !discoveryJob.isCancelled()) {
110             discoveryJob.cancel(true);
111             discoveryJob = null;
112         }
113     }
114 }