Skip to content

Commit 0fccb2c

Browse files
holgerfriedrichjoni1993
authored andcommitted
[knx] Code rework (openhab#17420)
* [knx] Code rework Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
1 parent 9b625cf commit 0fccb2c

24 files changed

Lines changed: 125 additions & 239 deletions

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/channel/KNXChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public final List<GroupAddress> getWriteAddresses() {
131131
return new WriteSpecImpl(entry.getValue(), dpt, command);
132132
}
133133
}
134-
// if we didn't find a match, check if we find a sub-type match
134+
// if we didn't find a match, check if we find a subtype match
135135
for (Map.Entry<String, GroupAddressConfiguration> entry : groupAddressConfigurations.entrySet()) {
136136
String dpt = Objects.requireNonNullElse(entry.getValue().getDPT(), getDefaultDPT(entry.getKey()));
137137
Set<Class<? extends Type>> expectedTypeClasses = DPTUtil.getAllowedTypes(dpt);

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/channel/KNXChannelFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static KNXChannel createKnxChannel(Channel channel) throws IllegalArgumen
5656
.map(Map.Entry::getValue).findFirst()
5757
.orElseThrow(() -> new IllegalArgumentException(channelTypeUID + " is not a valid channel type ID"));
5858

59-
// typecast to avoid warning about unsafe return type; we know that the lookup returns non null values
59+
// typecast to avoid warning about unsafe return type; we know that the lookup returns non-null values
6060
return (KNXChannel) supplier.apply(channel);
6161
}
6262
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/channel/TypeColor.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,12 @@ class TypeColor extends KNXChannel {
4646

4747
@Override
4848
protected String getDefaultDPT(String gaConfigKey) {
49-
if (gaConfigKey.equals(HSB_GA)) {
50-
return DPTXlatorRGB.DPT_RGB.getID();
51-
}
52-
if (gaConfigKey.equals(INCREASE_DECREASE_GA)) {
53-
return DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID();
54-
}
55-
if (gaConfigKey.equals(SWITCH_GA)) {
56-
return DPTXlatorBoolean.DPT_SWITCH.getID();
57-
}
58-
if (gaConfigKey.equals(POSITION_GA)) {
59-
return DPTXlator8BitUnsigned.DPT_SCALING.getID();
60-
}
61-
throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
49+
return switch (gaConfigKey) {
50+
case HSB_GA -> DPTXlatorRGB.DPT_RGB.getID();
51+
case INCREASE_DECREASE_GA -> DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID();
52+
case SWITCH_GA -> DPTXlatorBoolean.DPT_SWITCH.getID();
53+
case POSITION_GA -> DPTXlator8BitUnsigned.DPT_SCALING.getID();
54+
default -> throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
55+
};
6256
}
6357
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/channel/TypeDimmer.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.openhab.binding.knx.internal.KNXBindingConstants.*;
1616

1717
import java.util.List;
18-
import java.util.Objects;
1918
import java.util.Set;
2019

2120
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -45,15 +44,11 @@ class TypeDimmer extends KNXChannel {
4544

4645
@Override
4746
protected String getDefaultDPT(String gaConfigKey) {
48-
if (Objects.equals(gaConfigKey, INCREASE_DECREASE_GA)) {
49-
return DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID();
50-
}
51-
if (Objects.equals(gaConfigKey, SWITCH_GA)) {
52-
return DPTXlatorBoolean.DPT_SWITCH.getID();
53-
}
54-
if (Objects.equals(gaConfigKey, POSITION_GA)) {
55-
return DPTXlator8BitUnsigned.DPT_SCALING.getID();
56-
}
57-
throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
47+
return switch (gaConfigKey) {
48+
case INCREASE_DECREASE_GA -> DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID();
49+
case SWITCH_GA -> DPTXlatorBoolean.DPT_SWITCH.getID();
50+
case POSITION_GA -> DPTXlator8BitUnsigned.DPT_SCALING.getID();
51+
default -> throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
52+
};
5853
}
5954
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/channel/TypeRollershutter.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.openhab.binding.knx.internal.KNXBindingConstants.*;
1616

1717
import java.util.List;
18-
import java.util.Objects;
1918
import java.util.Set;
2019

2120
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -45,15 +44,11 @@ class TypeRollershutter extends KNXChannel {
4544

4645
@Override
4746
protected String getDefaultDPT(String gaConfigKey) {
48-
if (Objects.equals(gaConfigKey, UP_DOWN_GA)) {
49-
return DPTXlatorBoolean.DPT_UPDOWN.getID();
50-
}
51-
if (Objects.equals(gaConfigKey, STOP_MOVE_GA)) {
52-
return DPTXlatorBoolean.DPT_START.getID();
53-
}
54-
if (Objects.equals(gaConfigKey, POSITION_GA)) {
55-
return DPTXlator8BitUnsigned.DPT_SCALING.getID();
56-
}
57-
throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
47+
return switch (gaConfigKey) {
48+
case UP_DOWN_GA -> DPTXlatorBoolean.DPT_UPDOWN.getID();
49+
case STOP_MOVE_GA -> DPTXlatorBoolean.DPT_START.getID();
50+
case POSITION_GA -> DPTXlator8BitUnsigned.DPT_SCALING.getID();
51+
default -> throw new IllegalArgumentException("GA configuration '" + gaConfigKey + "' is not supported");
52+
};
5853
}
5954
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/client/AbstractKNXClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private synchronized boolean connect() {
234234
// Protected ctor using given ManagementClientImpl is available (custom class to be inherited)
235235
managementProcedures = new CustomManagementProceduresImpl(managementClient, tl);
236236

237-
// OH helper for reading device info, based on managementClient above
237+
// OpenHab helper for reading device info, based on managementClient above
238238
deviceInfoClient = new DeviceInfoClientImpl(managementClient);
239239

240240
// ProcessCommunicator provides main KNX communication (Calimero).

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/client/DeviceInspector.java

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,7 @@ public class DeviceInspector {
5151
private final DeviceInfoClient client;
5252
private final IndividualAddress address;
5353

54-
public static class Result {
55-
private final Map<String, String> properties;
56-
private final Set<GroupAddress> groupAddresses;
57-
58-
public Result(Map<String, String> properties, Set<GroupAddress> groupAddresses) {
59-
super();
60-
this.properties = properties;
61-
this.groupAddresses = groupAddresses;
62-
}
63-
64-
public Map<String, String> getProperties() {
65-
return properties;
66-
}
67-
68-
public Set<GroupAddress> getGroupAddresses() {
69-
return groupAddresses;
70-
}
54+
public record Result(Map<String, String> properties, Set<GroupAddress> groupAddresses) {
7155
}
7256

7357
public DeviceInspector(DeviceInfoClient client, IndividualAddress address) {
@@ -114,7 +98,7 @@ public Result readDeviceInfo() {
11498
* task immediately on connection loss or thing deconstruction.
11599
*
116100
* @param address Individual address of KNX device
117-
* @return List of device properties
101+
* @return Map of device properties
118102
* @throws InterruptedException
119103
*/
120104
private Map<String, String> readDeviceProperties(IndividualAddress address) throws InterruptedException {
@@ -179,7 +163,7 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
179163
if (!maxApdu.isEmpty()) {
180164
logger.trace("Max APDU of device {} is {} bytes (routing)", address, maxApdu);
181165
} else {
182-
// fallback: MAX_APDU_LENGTH; if availble set the default is 14 according to spec
166+
// fallback: MAX_APDU_LENGTH; if available set the default is 14 according to spec
183167
Thread.sleep(OPERATION_INTERVAL);
184168
try {
185169
byte[] result = getClient().readDeviceProperties(address, ADDRESS_TABLE_OBJECT,
@@ -247,7 +231,7 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
247231
logger.debug("Identified device {} as \"{}\"", address, result);
248232
ret.put(FRIENDLY_NAME, result);
249233
} else {
250-
// this is due to devices which have a buggy implememtation (and show a broken string also
234+
// this is due to devices which have a buggy implementation (and show a broken string also
251235
// in ETS tool)
252236
logger.debug("Ignoring FRIENDLY_NAME of device {} as it contains non-printable characters",
253237
address);
@@ -288,7 +272,7 @@ private Map<String, String> readDeviceProperties(IndividualAddress address) thro
288272
* Currently only data from DD0 is returned; DD2 is just logged in debug mode.
289273
*
290274
* @param address Individual address of KNX device
291-
* @return List of device properties
275+
* @return Map of device properties
292276
* @throws InterruptedException
293277
*/
294278
private Map<String, String> readDeviceDescription(IndividualAddress address) throws InterruptedException {
@@ -315,7 +299,7 @@ private Map<String, String> readDeviceDescription(IndividualAddress address) thr
315299
if (data != null) {
316300
try {
317301
final DD2 dd = DeviceDescriptor.DD2.from(data);
318-
logger.debug("The device with address {} is has DD2 {}", address, dd.toString());
302+
logger.debug("The device with address {} is has DD2 {}", address, dd);
319303
} catch (KNXIllegalArgumentException e) {
320304
logger.warn("Can not parse device descriptor 2 of device with address {}: {}", address,
321305
e.getMessage());
@@ -342,21 +326,14 @@ private int toUnsigned(final byte @Nullable [] data) {
342326
}
343327

344328
private static String getMediumType(int type) {
345-
switch (type) {
346-
case 0:
347-
return "TP";
348-
case 1:
349-
return "PL";
350-
case 2:
351-
return "RF";
352-
case 3:
353-
return "TP0 (deprecated)";
354-
case 4:
355-
return "PL123 (deprecated)";
356-
case 5:
357-
return "IP";
358-
default:
359-
return "unknown (" + type + ")";
360-
}
329+
return switch (type) {
330+
case 0 -> "TP";
331+
case 1 -> "PL";
332+
case 2 -> "RF";
333+
case 3 -> "TP0 (deprecated)";
334+
case 4 -> "PL123 (deprecated)";
335+
case 5 -> "IP";
336+
default -> "unknown (" + type + ")";
337+
};
361338
}
362339
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/client/InboundSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface InboundSpec {
3737
/**
3838
* Get the affected group addresses.
3939
*
40-
* @return a list of group addresses.
40+
* @return a Set of group addresses.
4141
*/
4242
Set<GroupAddress> getGroupAddresses();
4343
}

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/client/SerialClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SerialClient(int autoReconnectPeriod, ThingUID thingUID, int responseTime
6666
/**
6767
* try automatic detection of cEMI devices via the PEI identification frame
6868
*
69-
* @implNote This is based on an vendor specific extension and may not work for other devices.
69+
* @implNote This is based on a vendor specific extension and may not work for other devices.
7070
*/
7171
protected boolean detectCemi() throws InterruptedException {
7272
final byte[] peiIdentifyReqFrame = { (byte) 0xa7 };

bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/client/SerialTransportAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* {@literal @}ServiceProvider annotation (biz.aQute.bnd.annotation) automatically creates the file
4343
* /META-INF/services/tuwien.auto.calimero.serial.spi.SerialCom
4444
* to register SerialTransportAdapter to the service loader.
45-
* Additional attributes for SerialTansportAdapter can be specified as well, e.g.
45+
* Additional attributes for SerialTransportAdapter can be specified as well, e.g.
4646
* attribute = { "position=1" }
4747
* and will be part of MANIFEST.MF
4848
*

0 commit comments

Comments
 (0)