2 * Copyright (c) 2010-2022 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.knx.internal.client;
15 import java.time.Duration;
17 import java.util.concurrent.CopyOnWriteArraySet;
18 import java.util.concurrent.LinkedBlockingQueue;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.function.Consumer;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.knx.internal.KNXTypeMapper;
27 import org.openhab.binding.knx.internal.dpt.KNXCoreTypeMapper;
28 import org.openhab.binding.knx.internal.handler.GroupAddressListener;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.types.Type;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import tuwien.auto.calimero.CloseEvent;
37 import tuwien.auto.calimero.DetachEvent;
38 import tuwien.auto.calimero.FrameEvent;
39 import tuwien.auto.calimero.GroupAddress;
40 import tuwien.auto.calimero.IndividualAddress;
41 import tuwien.auto.calimero.KNXException;
42 import tuwien.auto.calimero.datapoint.CommandDP;
43 import tuwien.auto.calimero.datapoint.Datapoint;
44 import tuwien.auto.calimero.device.ProcessCommunicationResponder;
45 import tuwien.auto.calimero.link.KNXNetworkLink;
46 import tuwien.auto.calimero.link.NetworkLinkListener;
47 import tuwien.auto.calimero.mgmt.Destination;
48 import tuwien.auto.calimero.mgmt.ManagementClient;
49 import tuwien.auto.calimero.mgmt.ManagementClientImpl;
50 import tuwien.auto.calimero.mgmt.ManagementProcedures;
51 import tuwien.auto.calimero.mgmt.ManagementProceduresImpl;
52 import tuwien.auto.calimero.process.ProcessCommunication;
53 import tuwien.auto.calimero.process.ProcessCommunicator;
54 import tuwien.auto.calimero.process.ProcessCommunicatorImpl;
55 import tuwien.auto.calimero.process.ProcessEvent;
56 import tuwien.auto.calimero.process.ProcessListener;
57 import tuwien.auto.calimero.secure.SecureApplicationLayer;
58 import tuwien.auto.calimero.secure.Security;
61 * KNX Client which encapsulates the communication with the KNX bus via the calimero libary.
63 * @author Simon Kaufmann - initial contribution and API.
67 public abstract class AbstractKNXClient implements NetworkLinkListener, KNXClient {
69 private static final int MAX_SEND_ATTEMPTS = 2;
71 private final Logger logger = LoggerFactory.getLogger(AbstractKNXClient.class);
72 private final KNXTypeMapper typeHelper = new KNXCoreTypeMapper();
74 private final ThingUID thingUID;
75 private final int responseTimeout;
76 private final int readingPause;
77 private final int autoReconnectPeriod;
78 private final int readRetriesLimit;
79 private final StatusUpdateCallback statusUpdateCallback;
80 private final ScheduledExecutorService knxScheduler;
82 private @Nullable ProcessCommunicator processCommunicator;
83 private @Nullable ProcessCommunicationResponder responseCommunicator;
84 private @Nullable ManagementProcedures managementProcedures;
85 private @Nullable ManagementClient managementClient;
86 private @Nullable KNXNetworkLink link;
87 private @Nullable DeviceInfoClient deviceInfoClient;
88 private @Nullable ScheduledFuture<?> busJob;
89 private @Nullable ScheduledFuture<?> connectJob;
91 private final Set<GroupAddressListener> groupAddressListeners = new CopyOnWriteArraySet<>();
92 private final LinkedBlockingQueue<ReadDatapoint> readDatapoints = new LinkedBlockingQueue<>();
95 private interface ListenerNotification {
96 void apply(BusMessageListener listener, IndividualAddress source, GroupAddress destination, byte[] asdu);
100 private final ProcessListener processListener = new ProcessListener() {
103 public void detached(DetachEvent e) {
104 logger.debug("The KNX network link was detached from the process communicator");
108 public void groupWrite(ProcessEvent e) {
109 processEvent("Group Write", e, (listener, source, destination, asdu) -> {
110 listener.onGroupWrite(AbstractKNXClient.this, source, destination, asdu);
115 public void groupReadRequest(ProcessEvent e) {
116 processEvent("Group Read Request", e, (listener, source, destination, asdu) -> {
117 listener.onGroupRead(AbstractKNXClient.this, source, destination, asdu);
122 public void groupReadResponse(ProcessEvent e) {
123 processEvent("Group Read Response", e, (listener, source, destination, asdu) -> {
124 listener.onGroupReadResponse(AbstractKNXClient.this, source, destination, asdu);
129 public AbstractKNXClient(int autoReconnectPeriod, ThingUID thingUID, int responseTimeout, int readingPause,
130 int readRetriesLimit, ScheduledExecutorService knxScheduler, StatusUpdateCallback statusUpdateCallback) {
131 this.autoReconnectPeriod = autoReconnectPeriod;
132 this.thingUID = thingUID;
133 this.responseTimeout = responseTimeout;
134 this.readingPause = readingPause;
135 this.readRetriesLimit = readRetriesLimit;
136 this.knxScheduler = knxScheduler;
137 this.statusUpdateCallback = statusUpdateCallback;
140 public void initialize() {
141 if (!scheduleReconnectJob()) {
146 private boolean scheduleReconnectJob() {
147 if (autoReconnectPeriod > 0) {
148 connectJob = knxScheduler.schedule(this::connect, autoReconnectPeriod, TimeUnit.SECONDS);
155 private void cancelReconnectJob() {
156 ScheduledFuture<?> currentReconnectJob = connectJob;
157 if (currentReconnectJob != null) {
158 currentReconnectJob.cancel(true);
163 protected abstract KNXNetworkLink establishConnection() throws KNXException, InterruptedException;
165 private synchronized boolean connectIfNotAutomatic() {
166 if (!isConnected()) {
167 return connectJob != null ? false : connect();
172 private synchronized boolean connect() {
179 logger.debug("Bridge {} is connecting to the KNX bus", thingUID);
181 KNXNetworkLink link = establishConnection();
184 managementProcedures = new ManagementProceduresImpl(link);
186 ManagementClient managementClient = new ManagementClientImpl(link);
187 managementClient.responseTimeout(Duration.ofSeconds(responseTimeout));
188 this.managementClient = managementClient;
190 deviceInfoClient = new DeviceInfoClientImpl(managementClient);
192 ProcessCommunicator processCommunicator = new ProcessCommunicatorImpl(link);
193 processCommunicator.responseTimeout(Duration.ofSeconds(responseTimeout));
194 processCommunicator.addProcessListener(processListener);
195 this.processCommunicator = processCommunicator;
197 ProcessCommunicationResponder responseCommunicator = new ProcessCommunicationResponder(link,
198 new SecureApplicationLayer(link, Security.defaultInstallation()));
199 this.responseCommunicator = responseCommunicator;
201 link.addLinkListener(this);
203 busJob = knxScheduler.scheduleWithFixedDelay(() -> readNextQueuedDatapoint(), 0, readingPause,
204 TimeUnit.MILLISECONDS);
206 statusUpdateCallback.updateStatus(ThingStatus.ONLINE);
209 } catch (KNXException | InterruptedException e) {
210 logger.debug("Error connecting to the bus: {}", e.getMessage(), e);
212 scheduleReconnectJob();
217 private void disconnect(@Nullable Exception e) {
220 String message = e.getLocalizedMessage();
221 statusUpdateCallback.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
222 message != null ? message : "");
224 statusUpdateCallback.updateStatus(ThingStatus.OFFLINE);
228 @SuppressWarnings("null")
229 private void releaseConnection() {
230 logger.debug("Bridge {} is disconnecting from the KNX bus", thingUID);
231 readDatapoints.clear();
232 busJob = nullify(busJob, j -> j.cancel(true));
233 deviceInfoClient = null;
234 managementProcedures = nullify(managementProcedures, mp -> mp.detach());
235 managementClient = nullify(managementClient, mc -> mc.detach());
236 link = nullify(link, l -> l.close());
237 processCommunicator = nullify(processCommunicator, pc -> {
238 pc.removeProcessListener(processListener);
241 responseCommunicator = nullify(responseCommunicator, rc -> {
242 rc.removeProcessListener(processListener);
247 private <T> T nullify(T target, @Nullable Consumer<T> lastWill) {
248 if (target != null && lastWill != null) {
249 lastWill.accept(target);
254 private void processEvent(String task, ProcessEvent event, ListenerNotification action) {
255 GroupAddress destination = event.getDestination();
256 IndividualAddress source = event.getSourceAddr();
257 byte[] asdu = event.getASDU();
258 logger.trace("Received a {} telegram from '{}' to '{}' with value '{}'", task, source, destination, asdu);
259 for (GroupAddressListener listener : groupAddressListeners) {
260 if (listener.listensTo(destination)) {
261 knxScheduler.schedule(() -> action.apply(listener, source, destination, asdu), 0, TimeUnit.SECONDS);
267 * Transforms a {@link Type} into a datapoint type value for the KNX bus.
269 * @param type the {@link Type} to transform
270 * @param dpt the datapoint type to which should be converted
271 * @return the corresponding KNX datapoint type value as a string
274 private String toDPTValue(Type type, String dpt) {
275 return typeHelper.toDPTValue(type, dpt);
278 @SuppressWarnings("null")
279 private void readNextQueuedDatapoint() {
280 if (!connectIfNotAutomatic()) {
283 ProcessCommunicator processCommunicator = this.processCommunicator;
284 if (processCommunicator == null) {
287 ReadDatapoint datapoint = readDatapoints.poll();
288 if (datapoint != null) {
289 datapoint.incrementRetries();
291 logger.trace("Sending a Group Read Request telegram for {}", datapoint.getDatapoint().getMainAddress());
292 processCommunicator.read(datapoint.getDatapoint());
293 } catch (KNXException e) {
294 if (datapoint.getRetries() < datapoint.getLimit()) {
295 readDatapoints.add(datapoint);
296 logger.debug("Could not read value for datapoint {}: {}. Going to retry.",
297 datapoint.getDatapoint().getMainAddress(), e.getMessage());
299 logger.warn("Giving up reading datapoint {}, the number of maximum retries ({}) is reached.",
300 datapoint.getDatapoint().getMainAddress(), datapoint.getLimit());
302 } catch (InterruptedException e) {
303 logger.debug("Interrupted sending KNX read request");
309 public void dispose() {
310 cancelReconnectJob();
315 public void linkClosed(@Nullable CloseEvent closeEvent) {
316 KNXNetworkLink link = this.link;
317 if (link == null || closeEvent == null) {
320 if (!link.isOpen() && CloseEvent.USER_REQUEST != closeEvent.getInitiator()) {
321 statusUpdateCallback.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
322 closeEvent.getReason());
323 logger.debug("KNX link has been lost (reason: {} on object {})", closeEvent.getReason(),
324 closeEvent.getSource().toString());
325 scheduleReconnectJob();
330 public void indication(@Nullable FrameEvent e) {
335 public void confirmation(@Nullable FrameEvent e) {
340 public final synchronized boolean isReachable(@Nullable IndividualAddress address) throws KNXException {
341 ManagementProcedures managementProcedures = this.managementProcedures;
342 if (managementProcedures == null || address == null) {
346 return managementProcedures.isAddressOccupied(address);
347 } catch (InterruptedException e) {
348 logger.debug("Interrupted pinging KNX device '{}'", address);
354 public final synchronized void restartNetworkDevice(@Nullable IndividualAddress address) {
355 ManagementClient managementClient = this.managementClient;
356 if (address == null || managementClient == null) {
359 Destination destination = null;
361 destination = managementClient.createDestination(address, true);
362 managementClient.restart(destination);
363 } catch (KNXException e) {
364 logger.warn("Could not reset device with address '{}': {}", address, e.getMessage());
365 } catch (InterruptedException e) { // ignored as in Calimero pre-2.4.0
367 if (destination != null) {
368 destination.destroy();
374 public void readDatapoint(Datapoint datapoint) {
375 synchronized (this) {
376 ReadDatapoint retryDatapoint = new ReadDatapoint(datapoint, readRetriesLimit);
377 if (!readDatapoints.contains(retryDatapoint)) {
378 readDatapoints.add(retryDatapoint);
384 public final boolean registerGroupAddressListener(GroupAddressListener listener) {
385 return groupAddressListeners.add(listener);
389 public final boolean unregisterGroupAddressListener(GroupAddressListener listener) {
390 return groupAddressListeners.remove(listener);
394 public boolean isConnected() {
395 return link != null && link.isOpen();
399 public DeviceInfoClient getDeviceInfoClient() {
400 DeviceInfoClient deviceInfoClient = this.deviceInfoClient;
401 if (deviceInfoClient != null) {
402 return deviceInfoClient;
404 throw new IllegalStateException();
409 public void writeToKNX(OutboundSpec commandSpec) throws KNXException {
410 ProcessCommunicator processCommunicator = this.processCommunicator;
411 KNXNetworkLink link = this.link;
412 if (processCommunicator == null || link == null) {
413 logger.debug("Cannot write to the KNX bus (processCommuicator: {}, link: {})",
414 processCommunicator == null ? "Not OK" : "OK",
415 link == null ? "Not OK" : (link.isOpen() ? "Open" : "Closed"));
418 GroupAddress groupAddress = commandSpec.getGroupAddress();
420 logger.trace("writeToKNX groupAddress '{}', commandSpec '{}'", groupAddress, commandSpec);
422 if (groupAddress != null) {
423 sendToKNX(processCommunicator, link, groupAddress, commandSpec.getDPT(), commandSpec.getType());
428 public void respondToKNX(OutboundSpec responseSpec) throws KNXException {
429 ProcessCommunicationResponder responseCommunicator = this.responseCommunicator;
430 KNXNetworkLink link = this.link;
431 if (responseCommunicator == null || link == null) {
432 logger.debug("Cannot write to the KNX bus (responseCommunicator: {}, link: {})",
433 responseCommunicator == null ? "Not OK" : "OK",
434 link == null ? "Not OK" : (link.isOpen() ? "Open" : "Closed"));
437 GroupAddress groupAddress = responseSpec.getGroupAddress();
439 logger.trace("respondToKNX groupAddress '{}', responseSpec '{}'", groupAddress, responseSpec);
441 if (groupAddress != null) {
442 sendToKNX(responseCommunicator, link, groupAddress, responseSpec.getDPT(), responseSpec.getType());
446 private void sendToKNX(ProcessCommunication communicator, KNXNetworkLink link, GroupAddress groupAddress,
447 String dpt, Type type) throws KNXException {
448 if (!connectIfNotAutomatic()) {
452 Datapoint datapoint = new CommandDP(groupAddress, thingUID.toString(), 0, dpt);
453 String mappedValue = toDPTValue(type, dpt);
455 logger.trace("sendToKNX mappedValue: '{}' groupAddress: '{}'", mappedValue, groupAddress);
457 if (mappedValue == null) {
458 logger.debug("Value '{}' cannot be mapped to datapoint '{}'", type, datapoint);
461 for (int i = 0; i < MAX_SEND_ATTEMPTS; i++) {
463 communicator.write(datapoint, mappedValue);
464 logger.debug("Wrote value '{}' to datapoint '{}' ({}. attempt).", type, datapoint, i);
466 } catch (KNXException e) {
467 if (i < MAX_SEND_ATTEMPTS - 1) {
468 logger.debug("Value '{}' could not be sent to the KNX bus using datapoint '{}': {}. Will retry.",
469 type, datapoint, e.getLocalizedMessage());
471 logger.warn("Value '{}' could not be sent to the KNX bus using datapoint '{}': {}. Giving up now.",
472 type, datapoint, e.getLocalizedMessage());