]> git.basschouten.com Git - openhab-addons.git/blob
63e847068dc5da17deba2355ab154a4dd1f91dbd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nikohomecontrol.internal.discovery;
14
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlBridgeHandler;
23 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcAction;
24 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcEnergyMeter;
25 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcThermostat;
26 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * If a Niko Home Control bridge is added or if the user scans manually for things this
35  * {@link NikoHomeControlDiscoveryService} is used to return Niko Home Control Actions as things to the framework.
36  *
37  * @author Mark Herwege - Initial Contribution
38  */
39 @NonNullByDefault
40 public class NikoHomeControlDiscoveryService extends AbstractDiscoveryService {
41
42     private final Logger logger = LoggerFactory.getLogger(NikoHomeControlDiscoveryService.class);
43
44     private static final int TIMEOUT = 5;
45
46     private ThingUID bridgeUID;
47     private NikoHomeControlBridgeHandler handler;
48
49     public NikoHomeControlDiscoveryService(NikoHomeControlBridgeHandler handler) {
50         super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT, false);
51         logger.debug("discovery service {}", handler);
52         bridgeUID = handler.getThing().getUID();
53         this.handler = handler;
54     }
55
56     public void activate() {
57         handler.setNhcDiscovery(this);
58     }
59
60     @Override
61     public void deactivate() {
62         removeOlderResults(new Date().getTime());
63         handler.setNhcDiscovery(null);
64     }
65
66     /**
67      * Discovers devices connected to a Niko Home Control controller
68      */
69     public void discoverDevices() {
70         NikoHomeControlCommunication nhcComm = handler.getCommunication();
71
72         if ((nhcComm == null) || !nhcComm.communicationActive()) {
73             logger.warn("not connected");
74             return;
75         }
76         logger.debug("getting devices on {}", handler.getThing().getUID().getId());
77
78         Map<String, NhcAction> actions = nhcComm.getActions();
79
80         actions.forEach((actionId, nhcAction) -> {
81             String thingName = nhcAction.getName();
82             String thingLocation = nhcAction.getLocation();
83
84             switch (nhcAction.getType()) {
85                 case TRIGGER:
86                     addActionDevice(new ThingUID(THING_TYPE_PUSHBUTTON, handler.getThing().getUID(), actionId),
87                             actionId, thingName, thingLocation);
88                     break;
89                 case RELAY:
90                     addActionDevice(new ThingUID(THING_TYPE_ON_OFF_LIGHT, handler.getThing().getUID(), actionId),
91                             actionId, thingName, thingLocation);
92                     break;
93                 case DIMMER:
94                     addActionDevice(new ThingUID(THING_TYPE_DIMMABLE_LIGHT, handler.getThing().getUID(), actionId),
95                             actionId, thingName, thingLocation);
96                     break;
97                 case ROLLERSHUTTER:
98                     addActionDevice(new ThingUID(THING_TYPE_BLIND, handler.getThing().getUID(), actionId), actionId,
99                             thingName, thingLocation);
100                     break;
101                 default:
102                     logger.debug("unrecognized action type {} for {} {}", nhcAction.getType(), actionId, thingName);
103             }
104         });
105
106         Map<String, NhcThermostat> thermostats = nhcComm.getThermostats();
107
108         thermostats.forEach((thermostatId, nhcThermostat) -> {
109             String thingName = nhcThermostat.getName();
110             String thingLocation = nhcThermostat.getLocation();
111             addThermostatDevice(new ThingUID(THING_TYPE_THERMOSTAT, handler.getThing().getUID(), thermostatId),
112                     thermostatId, thingName, thingLocation);
113         });
114
115         Map<String, NhcEnergyMeter> energyMeters = nhcComm.getEnergyMeters();
116
117         energyMeters.forEach((energyMeterId, nhcEnergyMeter) -> {
118             String thingName = nhcEnergyMeter.getName();
119             addEnergyMeterDevice(new ThingUID(THING_TYPE_ENERGYMETER, handler.getThing().getUID(), energyMeterId),
120                     energyMeterId, thingName);
121         });
122     }
123
124     private void addActionDevice(ThingUID uid, String actionId, String thingName, @Nullable String thingLocation) {
125         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
126                 .withLabel(thingName).withProperty(CONFIG_ACTION_ID, actionId);
127         if (thingLocation != null) {
128             discoveryResultBuilder.withProperty("Location", thingLocation);
129         }
130         thingDiscovered(discoveryResultBuilder.build());
131     }
132
133     private void addThermostatDevice(ThingUID uid, String thermostatId, String thingName,
134             @Nullable String thingLocation) {
135         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
136                 .withLabel(thingName).withProperty(CONFIG_THERMOSTAT_ID, thermostatId);
137         if (thingLocation != null) {
138             discoveryResultBuilder.withProperty("Location", thingLocation);
139         }
140         thingDiscovered(discoveryResultBuilder.build());
141     }
142
143     private void addEnergyMeterDevice(ThingUID uid, String energyMeterId, String thingName) {
144         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
145                 .withLabel(thingName).withProperty(CONFIG_ENERGYMETER_ID, energyMeterId);
146         thingDiscovered(discoveryResultBuilder.build());
147     }
148
149     @Override
150     protected void startScan() {
151         discoverDevices();
152     }
153
154     @Override
155     protected synchronized void stopScan() {
156         super.stopScan();
157         removeOlderResults(getTimestampOfLastScan());
158     }
159 }