]> git.basschouten.com Git - openhab-addons.git/blob
01c37048ef17816e878425be02ed870b893da5f0
[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.knx.internal.client;
14
15 import java.nio.ByteBuffer;
16 import java.text.MessageFormat;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import tuwien.auto.calimero.IndividualAddress;
25 import tuwien.auto.calimero.KNXException;
26 import tuwien.auto.calimero.mgmt.Destination;
27 import tuwien.auto.calimero.mgmt.ManagementClient;
28
29 /**
30  * Client for retrieving additional device descriptions.
31  *
32  * @author Simon Kaufmann - initial contribution and API.
33  *
34  */
35 @NonNullByDefault
36 public class DeviceInfoClientImpl implements DeviceInfoClient {
37
38     private final Logger logger = LoggerFactory.getLogger(DeviceInfoClientImpl.class);
39     private final ManagementClient managementClient;
40
41     DeviceInfoClientImpl(ManagementClient managementClient) {
42         this.managementClient = managementClient;
43     }
44
45     @FunctionalInterface
46     private interface ReadFunction<T, R> {
47         @Nullable
48         R apply(T t) throws KNXException, InterruptedException;
49     }
50
51     private byte @Nullable [] readFromManagementClient(String task, long timeout, IndividualAddress address,
52             ReadFunction<Destination, byte[]> function) {
53         final long start = System.nanoTime();
54         while ((System.nanoTime() - start) < TimeUnit.MILLISECONDS.toNanos(timeout)) {
55             Destination destination = null;
56             try {
57                 logger.trace("Going to {} of {} ", task, address);
58                 destination = managementClient.createDestination(address, true);
59                 byte[] result = function.apply(destination);
60                 logger.trace("Finished to {} of {}, result: {}", task, address, result == null ? null : result.length);
61                 return result;
62             } catch (KNXException e) {
63                 logger.debug("Could not {} of {}: {}", task, address, e.getMessage());
64             } catch (InterruptedException e) {
65                 logger.trace("Interrupted to {}", task);
66                 return null;
67             } finally {
68                 if (destination != null) {
69                     destination.destroy();
70                 }
71             }
72         }
73         return null;
74     }
75
76     private void authorize(boolean authenticate, Destination destination) throws KNXException, InterruptedException {
77         if (authenticate) {
78             managementClient.authorize(destination, (ByteBuffer.allocate(4)).put((byte) 0xFF).put((byte) 0xFF)
79                     .put((byte) 0xFF).put((byte) 0xFF).array());
80         }
81     }
82
83     @Override
84     public synchronized byte @Nullable [] readDeviceDescription(IndividualAddress address, int descType,
85             boolean authenticate, long timeout) {
86         String task = "read the device description";
87         return readFromManagementClient(task, timeout, address, destination -> {
88             authorize(authenticate, destination);
89             return managementClient.readDeviceDesc(destination, descType);
90         });
91     }
92
93     @Override
94     public synchronized byte @Nullable [] readDeviceMemory(IndividualAddress address, int startAddress, int bytes,
95             boolean authenticate, long timeout) {
96         String task = MessageFormat.format("read {0} bytes at memory location {1}", bytes, startAddress);
97         return readFromManagementClient(task, timeout, address, destination -> {
98             authorize(authenticate, destination);
99             return managementClient.readMemory(destination, startAddress, bytes);
100         });
101     }
102
103     @Override
104     public synchronized byte @Nullable [] readDeviceProperties(IndividualAddress address,
105             final int interfaceObjectIndex, final int propertyId, final int start, final int elements,
106             boolean authenticate, long timeout) {
107         String task = MessageFormat.format("read device property {0} at index {1}", propertyId, interfaceObjectIndex);
108         return readFromManagementClient(task, timeout, address, destination -> {
109             authorize(authenticate, destination);
110             return managementClient.readProperty(destination, interfaceObjectIndex, propertyId, start, elements);
111         });
112     }
113
114     @Override
115     public boolean isConnected() {
116         return managementClient.isOpen();
117     }
118 }