Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Friedrich <[email protected]>
  • Loading branch information
holgerfriedrich committed Nov 3, 2024
1 parent 9ce8381 commit c9485fa
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected boolean initializeSecurity(String cKeyringFile, String cKeyringPasswor
} catch (KNXMLException e) {
throw new KnxSecureException("keyring file configured, but loading failed: ", e);
}
if (!keyring.isPresent()) {
if (keyring.isEmpty()) {
throw new KnxSecureException("keyring file configured, but loading failed: " + keyringUri);
}

Expand Down Expand Up @@ -262,7 +262,7 @@ protected boolean initializeSecurity(String cKeyringFile, String cKeyringPasswor
// step 6: tunnel: load data from keyring
if (secureTunnelSourceAddr != null) {
// requires a valid keyring
if (!keyring.isPresent()) {
if (keyring.isEmpty()) {
throw new KnxSecureException("valid keyring specification required for secure tunnel mode");
}
// other parameters will not be accepted, since all is read from keyring in this case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public void onStateUpdateFromItem(State state) {
ChannelUID linkedChannelUID = callback.getItemChannelLink().getLinkedUID();
logger.trace("onStateUpdateFromItem({}) to {}", state.toString(), linkedChannelUID);

if (!(state instanceof Command)) {
if (!(state instanceof Command command)) {
logger.debug("The given state {} could not be transformed to a command", state);
return;
}
Command command = (Command) state;

// this does not have effect for contact items
// callback.handleCommand(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
private @Nullable ProfileTypeUID getSuggestedProfileTypeUID(@Nullable ChannelTypeUID channelTypeUID,
@Nullable String itemType) {
if (KNXBindingConstants.CHANNEL_CONTACT_CONTROL_UID.equals(channelTypeUID) && itemType != null) {
switch (itemType) {
case CoreItemFactory.CONTACT:
return UID_CONTACT_CONTROL;
default:
return null;
}
return switch (itemType) {
case CoreItemFactory.CONTACT -> UID_CONTACT_CONTROL;
default -> null;
};
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.knx.internal.dpt;

import static java.lang.Double.*;
import static org.junit.jupiter.api.Assertions.*;

import java.text.DecimalFormat;
Expand Down Expand Up @@ -148,7 +149,7 @@ void testToDPT8ValueFromQuantityType() {
void testToDPT9ValueFromQuantityType() {
assertEquals("23.1", ValueEncoder.encode(new QuantityType<>("23.1 °C"), "9.001"));
assertEquals(5.0,
Double.parseDouble(Objects.requireNonNull(ValueEncoder.encode(new QuantityType<>("41 °F"), "9.001"))));
parseDouble(Objects.requireNonNull(ValueEncoder.encode(new QuantityType<>("41 °F"), "9.001"))));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("274.15 K"), "9.001"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 K"), "9.002"));
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 mK"), "9.002"));
Expand All @@ -163,8 +164,8 @@ void testToDPT9ValueFromQuantityType() {
assertEquals("1", ValueEncoder.encode(new QuantityType<>("1 m/s"), "9.005"));
assertTrue(Objects.requireNonNullElse(ValueEncoder.encode(new QuantityType<>("1.94 kn"), "9.005"), "")
.startsWith("0.99"));
assertEquals(1.0, Double
.parseDouble(Objects.requireNonNull(ValueEncoder.encode(new QuantityType<>("3.6 km/h"), "9.005"))));
assertEquals(1.0,
parseDouble(Objects.requireNonNull(ValueEncoder.encode(new QuantityType<>("3.6 km/h"), "9.005"))));
assertEquals("456", ValueEncoder.encode(new QuantityType<>("456 Pa"), "9.006"));
assertEquals("70", ValueEncoder.encode(new QuantityType<>("70 %"), "9.007"));
assertEquals("8", ValueEncoder.encode(new QuantityType<>("8 ppm"), "9.008"));
Expand Down Expand Up @@ -457,9 +458,9 @@ public void dpt251White() {
String[] parts = enc.split(" ");
assertEquals(5, parts.length);
int[] rgb = ColorUtil.hsbToRgb(hsbType);
assertEquals(rgb[0] * 100d / 255, Double.valueOf(parts[0].replace(',', '.')), 1);
assertEquals(rgb[1] * 100d / 255, Double.valueOf(parts[1].replace(',', '.')), 1);
assertEquals(rgb[2] * 100d / 255, Double.valueOf(parts[2].replace(',', '.')), 1);
assertEquals(rgb[0] * 100d / 255, valueOf(parts[0].replace(',', '.')), 1);
assertEquals(rgb[1] * 100d / 255, valueOf(parts[1].replace(',', '.')), 1);
assertEquals(rgb[2] * 100d / 255, valueOf(parts[2].replace(',', '.')), 1);
}

@Test
Expand All @@ -479,9 +480,9 @@ public void dpt251Value() {
String[] parts = enc.split(" ");
assertEquals(5, parts.length);
int[] rgb = ColorUtil.hsbToRgb(hsbType);
assertEquals(rgb[0] * 100d / 255, Double.valueOf(parts[0].replace(',', '.')), 1);
assertEquals(rgb[1] * 100d / 255, Double.valueOf(parts[1].replace(',', '.')), 1);
assertEquals(rgb[2] * 100d / 255, Double.valueOf(parts[2].replace(',', '.')), 1);
assertEquals(rgb[0] * 100d / 255, valueOf(parts[0].replace(',', '.')), 1);
assertEquals(rgb[1] * 100d / 255, valueOf(parts[1].replace(',', '.')), 1);
assertEquals(rgb[2] * 100d / 255, valueOf(parts[2].replace(',', '.')), 1);
}

// This test checks all our overrides for units. It allows to detect unnecessary overrides when we
Expand Down Expand Up @@ -594,6 +595,7 @@ public void dpt242BackToBackTest(byte[] value) {

// encoding will return a String in notation defined by Calimero: "(x,xxxx y,yyyy) YY,Y %"
String result = ValueEncoder.encode(hsb, dpt);
assertNotNull(result);

// for back to back test, compare numerical values to allow tolerances
double dx = (((value[0] & 0xff) << 8) | (value[1] & 0xff)) / 65535.0;
Expand All @@ -608,9 +610,9 @@ public void dpt242BackToBackTest(byte[] value) {
Assertions.assertNotNull(stringx);
Assertions.assertNotNull(stringy);
Assertions.assertNotNull(stringY);
double rx = Double.parseDouble(stringx.replace(',', '.'));
double ry = Double.parseDouble(stringy.replace(',', '.'));
double rY = Double.parseDouble(stringY.replace(',', '.'));
double rx = parseDouble(stringx.replace(',', '.'));
double ry = parseDouble(stringy.replace(',', '.'));
double rY = parseDouble(stringY.replace(',', '.'));

final double tolerance = 0.001;
if ((Math.abs(dx - rx) > tolerance) || (Math.abs(dy - ry) > tolerance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ void testDpt18() {
helper("18.001", new byte[] { 42 }, new DecimalType(42));
helper("18.001", new byte[] { 63 }, new DecimalType(63));
// scene, learn += 0x80
helper("18.001", new byte[] { (byte) (0x80 + 0) }, new DecimalType(0x80));
helper("18.001", new byte[] { (byte) (0x80) }, new DecimalType(0x80));
helper("18.001", new byte[] { (byte) (0x80 + 42) }, new DecimalType(0x80 + 42));
helper("18.001", new byte[] { (byte) (0x80 + 63) }, new DecimalType(0x80 + 63));
}
Expand All @@ -786,7 +786,7 @@ void testDpt19() {
new DateTimeType("2019-07-15T17:30:00"), new byte[0], new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 });
helper("19.001", new byte[] { (byte) (2019 - 1900), 7, 15, 17, 30, 0, (byte) 0x24, (byte) 0x00 },
new DateTimeType("2019-07-15T17:30:00"), new byte[0], new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 });
// TODO add tests for incompletly filled frames (e.g. containing only date or time)
// TODO add tests for incompletely filled frames (e.g. containing only date or time)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,15 @@ public void testSecurityHelperEmpty() {
Security openhabSecurity = Security.newSecurity();
openhabSecurity.useKeyring(keys, password);

assertThrows(KnxSecureException.class, () -> {
KNXBridgeBaseThingHandler.secHelperReadBackboneKey(Optional.empty(), passwordString);
});
assertThrows(KnxSecureException.class,
() -> KNXBridgeBaseThingHandler.secHelperReadBackboneKey(Optional.empty(), passwordString));
assertTrue(KNXBridgeBaseThingHandler.secHelperReadBackboneKey(Optional.ofNullable(keys), passwordString)
.isEmpty());

// now check tunnel (expected to fail, not included)
IndividualAddress secureTunnelSourceAddr = new IndividualAddress(2, 8, 20);
assertThrows(KnxSecureException.class, () -> {
KNXBridgeBaseThingHandler.secHelperReadTunnelConfig(Optional.empty(), passwordString,
secureTunnelSourceAddr);
});
assertThrows(KnxSecureException.class, () -> KNXBridgeBaseThingHandler
.secHelperReadTunnelConfig(Optional.empty(), passwordString, secureTunnelSourceAddr));
assertTrue(KNXBridgeBaseThingHandler
.secHelperReadTunnelConfig(Optional.ofNullable(keys), passwordString, secureTunnelSourceAddr)
.isEmpty());
Expand Down

0 comments on commit c9485fa

Please sign in to comment.