]> git.basschouten.com Git - openhab-addons.git/blob
8883fbfec2c9f0850b49706ef8b9c12cf0b66354
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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("Niko Home Control: 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("Niko Home Control: not connected.");
74             return;
75         }
76         logger.debug("Niko Home Control: 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("Niko Home Control: unrecognized action type {} for {} {}", nhcAction.getType(),
103                             actionId, thingName);
104             }
105         });
106
107         Map<String, NhcThermostat> thermostats = nhcComm.getThermostats();
108
109         thermostats.forEach((thermostatId, nhcThermostat) -> {
110             String thingName = nhcThermostat.getName();
111             String thingLocation = nhcThermostat.getLocation();
112             addThermostatDevice(new ThingUID(THING_TYPE_THERMOSTAT, handler.getThing().getUID(), thermostatId),
113                     thermostatId, thingName, thingLocation);
114         });
115
116         Map<String, NhcEnergyMeter> energyMeters = nhcComm.getEnergyMeters();
117
118         energyMeters.forEach((energyMeterId, nhcEnergyMeter) -> {
119             String thingName = nhcEnergyMeter.getName();
120             addEnergyMeterDevice(new ThingUID(THING_TYPE_ENERGYMETER, handler.getThing().getUID(), energyMeterId),
121                     energyMeterId, thingName);
122         });
123     }
124
125     private void addActionDevice(ThingUID uid, String actionId, String thingName, @Nullable String thingLocation) {
126         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
127                 .withLabel(thingName).withProperty(CONFIG_ACTION_ID, actionId);
128         if (thingLocation != null) {
129             discoveryResultBuilder.withProperty("Location", thingLocation);
130         }
131         thingDiscovered(discoveryResultBuilder.build());
132     }
133
134     private void addThermostatDevice(ThingUID uid, String thermostatId, String thingName,
135             @Nullable String thingLocation) {
136         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
137                 .withLabel(thingName).withProperty(CONFIG_THERMOSTAT_ID, thermostatId);
138         if (thingLocation != null) {
139             discoveryResultBuilder.withProperty("Location", thingLocation);
140         }
141         thingDiscovered(discoveryResultBuilder.build());
142     }
143
144     private void addEnergyMeterDevice(ThingUID uid, String energyMeterId, String thingName) {
145         DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
146                 .withLabel(thingName).withProperty(CONFIG_ENERGYMETER_ID, energyMeterId);
147         thingDiscovered(discoveryResultBuilder.build());
148     }
149
150     @Override
151     protected void startScan() {
152         discoverDevices();
153     }
154
155     @Override
156     protected synchronized void stopScan() {
157         super.stopScan();
158         removeOlderResults(getTimestampOfLastScan());
159     }
160 }